提交记录 32436
| 用户 | 题目 | 状态 | 得分 | 用时 | 内存 | 语言 | 代码长度 |
|---|---|---|---|---|---|---|---|
| saffah_dsh_260814 | noip18d. 【NOIP2018】旅行 | Accepted | 100 | 336.37 us | 552 KB | C | 6.40 KB |
| 提交时间 | 评测时间 |
|---|---|
| 2026-08-14 10:58:39 | 2026-08-14 10:58:44 |
/* NOIP2018 旅行 (noip18d) - base ring tree lexicographically smallest DFS order
* Dual-mode: DuckInfo direct-memory IO (submission) / stdio (LOCAL for testing).
*/
#include <stdint.h>
static char *OUT;
#ifdef LOCAL
#include <stdio.h>
static inline int rd() { int x; scanf("%d", &x); return x; }
#else
#include <sys/auxv.h>
struct DuckInfo {
uint64_t abi_version;
const char *stdin_ptr; uint64_t stdin_size;
char *stdout_ptr; uint64_t stdout_limit; uint64_t stdout_size;
char *stderr_ptr; uint64_t stderr_limit; uint64_t stderr_size;
const char *IB_ptr; uint64_t IB_limit;
char *OB_ptr; uint64_t OB_limit;
uint64_t tsc_frequency;
} __attribute__((packed));
static const char *inp;
static const char *inend;
static uint64_t *pout_size;
static inline int rd() {
int x = 0;
while (*inp < '0' || *inp > '9') inp++;
while (*inp >= '0' && *inp <= '9') { x = x * 10 + (*inp - '0'); inp++; }
return x;
}
#endif
#define MAXN 5005
#define MAXE 10010
static int n, m;
static int eu[MAXE], ev[MAXE]; /* directed edges */
static int E2; /* = 2*m */
static int deg[MAXN];
static int head[MAXN];
static int adj[MAXE];
static int ord[MAXE], tmp2[MAXE];
static int cnt[MAXN];
static char onring[MAXN];
static char vis[MAXN]; /* in-path for find_ring */
static int vstack[MAXN], vsp;
static int rstack[MAXN], rsp;
static int rhead, rtail;
static int brku, brkv;
static char used[MAXN];
/* radix sort directed edges by (u, v); values in [1,n] */
static void sort_edges(void) {
int i, e, lim = n + 1;
for (i = 0; i <= n; i++) cnt[i] = 0;
for (e = 0; e < E2; e++) cnt[ev[e]]++;
for (i = 1; i <= n; i++) cnt[i] += cnt[i - 1];
for (e = E2 - 1; e >= 0; e--) tmp2[--cnt[ev[e]]] = e;
for (i = 0; i <= n; i++) cnt[i] = 0;
for (e = 0; e < E2; e++) cnt[eu[tmp2[e]]]++;
for (i = 1; i <= n; i++) cnt[i] += cnt[i - 1];
for (e = E2 - 1; e >= 0; e--) ord[--cnt[eu[tmp2[e]]]] = tmp2[e];
{
int prev = -1;
for (e = 0; e < E2; e++) {
int u = eu[ord[e]];
if (u != prev) { head[u] = e; prev = u; }
adj[e] = ev[ord[e]];
}
}
}
/* find ring; returns 1 when found */
static int find_ring(int x, int fa) {
int e;
if (vis[x]) {
rhead = x;
onring[x] = 1;
rtail = vstack[vsp - 1];
while (vstack[vsp - 1] != x) {
onring[vstack[vsp - 1]] = 1;
rstack[rsp++] = vstack[vsp - 1];
vsp--;
}
return 1;
}
vis[x] = 1;
vstack[vsp++] = x;
for (e = head[x]; e < head[x] + deg[x]; e++) {
int y = adj[e];
if (y == fa) continue;
if (find_ring(y, x)) return 1;
}
vsp--;
vis[x] = 0;
return 0;
}
/* decide the break edge */
static void deal(void) {
int turn;
int e = rhead;
int c1 = rstack[rsp - 1];
int eparent = (vsp >= 2) ? vstack[vsp - 2] : 0;
/* turn[0] = smallest neighbor of entrance e that is > c1 and != eparent */
turn = 0;
{
int idx;
for (idx = head[e]; idx < head[e] + deg[e]; idx++) {
int y = adj[idx];
if (y > c1 && y != eparent) { turn = y; break; }
}
if (turn == 0) turn = rtail;
}
while (rsp > 0) {
int x = rstack[--rsp];
int i, d = deg[x];
int nxt;
if (x == rtail) { brku = x; brkv = rhead; return; }
nxt = rstack[rsp - 1];
{
int last = adj[head[x] + d - 1];
if (last > turn && last == nxt) { brku = x; brkv = nxt; return; }
if (last != nxt && onring[last]) {
int second = adj[head[x] + d - 2];
if (second > turn && second == nxt) { brku = x; brkv = nxt; return; }
}
if (last != nxt && d > 2) {
for (i = 0; i < d; i++) {
if (adj[head[x] + i] == nxt) {
if (onring[adj[head[x] + i + 1]]) {
if (d - i > 2) turn = adj[head[x] + i + 2];
} else {
turn = adj[head[x] + i + 1];
}
break;
}
}
}
}
}
brku = rtail; brkv = rhead;
}
static inline void wint(int x) {
char *p = OUT;
if (x >= 1000) {
int d = x / 1000; *p++ = (char)('0' + d); x -= d * 1000;
d = x / 100; *p++ = (char)('0' + d); x -= d * 100;
d = x / 10; *p++ = (char)('0' + d); x -= d * 10;
*p++ = (char)('0' + x);
} else if (x >= 100) {
int d = x / 100; *p++ = (char)('0' + d); x -= d * 100;
d = x / 10; *p++ = (char)('0' + d); x -= d * 10;
*p++ = (char)('0' + x);
} else if (x >= 10) {
int d = x / 10; *p++ = (char)('0' + d); x -= d * 10;
*p++ = (char)('0' + x);
} else {
*p++ = (char)('0' + x);
}
*p++ = ' ';
OUT = p;
}
static void dfs(int start) {
int sp = 0;
vstack[sp++] = start;
used[start] = 1;
while (sp) {
int x = vstack[--sp];
int e;
wint(x);
for (e = head[x] + deg[x] - 1; e >= head[x]; e--) {
int y = adj[e];
if (used[y]) continue;
if ((x == brku && y == brkv) || (x == brkv && y == brku)) continue;
used[y] = 1;
vstack[sp++] = y;
}
}
}
#ifndef LOCAL
int main(void) {
struct DuckInfo *d = (struct DuckInfo *)getauxval(0x6b637564);
inp = d->stdin_ptr;
inend = d->stdin_ptr + d->stdin_size;
OUT = d->stdout_ptr;
pout_size = &d->stdout_size;
{
int i;
n = rd(); m = rd();
E2 = 0;
for (i = 1; i <= n; i++) deg[i] = 0;
for (i = 0; i < m; i++) {
int u = rd(), v = rd();
eu[E2] = u; ev[E2] = v; E2++;
eu[E2] = v; ev[E2] = u; E2++;
deg[u]++; deg[v]++;
}
sort_edges();
if (m == n - 1) {
brku = brkv = 0;
dfs(1);
} else {
find_ring(1, 1);
deal();
dfs(1);
}
*(OUT - 1) = '\n';
*pout_size = (uint64_t)(OUT - d->stdout_ptr);
}
__asm__ volatile("mov $60,%eax; xor %edi,%edi; syscall");
__builtin_unreachable();
}
#else
int main(void) {
int i;
n = rd(); m = rd();
E2 = 0;
for (i = 1; i <= n; i++) deg[i] = 0;
for (i = 0; i < m; i++) {
int u = rd(), v = rd();
eu[E2] = u; ev[E2] = v; E2++;
eu[E2] = v; ev[E2] = u; E2++;
deg[u]++; deg[v]++;
}
sort_edges();
if (m == n - 1) {
brku = brkv = 0;
} else {
find_ring(1, 1);
deal();
}
{
static int order[MAXN]; int oc = 0;
int sp = 0;
vstack[sp++] = 1; used[1] = 1;
while (sp) {
int x = vstack[--sp];
order[oc++] = x;
for (i = head[x] + deg[x] - 1; i >= head[x]; i--) {
int y = adj[i];
if (used[y]) continue;
if ((x == brku && y == brkv) || (x == brkv && y == brku)) continue;
used[y] = 1;
vstack[sp++] = y;
}
}
for (i = 0; i < oc; i++) printf("%d%c", order[i], i + 1 < oc ? ' ' : '\n');
}
return 0;
}
#endif
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 8.42 us | 56 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #2 | 7.85 us | 56 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #3 | 7.54 us | 56 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #4 | 12.02 us | 60 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #5 | 11.83 us | 60 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #6 | 40.45 us | 108 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #7 | 38.93 us | 108 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #8 | 39.05 us | 108 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #9 | 47.09 us | 108 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #10 | 47.59 us | 108 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #11 | 267.82 us | 300 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #12 | 257.9 us | 300 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #13 | 245.57 us | 300 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #14 | 259.49 us | 300 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #15 | 246.64 us | 300 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #16 | 9.28 us | 64 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #17 | 8.79 us | 64 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #18 | 14.69 us | 72 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #19 | 14.15 us | 68 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #20 | 55.2 us | 184 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #21 | 55.64 us | 184 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #22 | 59.2 us | 184 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #23 | 333.29 us | 552 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #24 | 333.7 us | 536 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #25 | 336.37 us | 520 KB | Accepted | Score: 4 | 显示更多 |
Judge Duck Online | 评测鸭在线
Server Time: 2026-09-11 21:14:06 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠