#include <stdio.h>
#include <algorithm>
namespace IO {
const int BUF_SIZE = 1 << 15;
char in_buf[BUF_SIZE], out_buf[BUF_SIZE];
char *p_in_buf = in_buf + BUF_SIZE;
char *p_out_buf = out_buf;
inline char get_char() {
if (p_in_buf == in_buf + BUF_SIZE) {
fread(in_buf, 1, BUF_SIZE, stdin), p_in_buf = in_buf;
}
return *(p_in_buf++);
}
inline void put_char(char x) {
if (p_out_buf == out_buf + BUF_SIZE) {
fwrite(out_buf, 1, BUF_SIZE, stdout), p_out_buf = out_buf;
}
*(p_out_buf++) = x;
}
inline void flush() {
if (p_out_buf != out_buf) {
fwrite(out_buf, 1, p_out_buf - out_buf, stdout);
p_out_buf = out_buf;
}
}
}
#define getchar() IO::get_char()
#define putchar(x) IO::put_char(x)
inline int getint() {
int x = 0;
char c = getchar();
while (c <= 32) c = getchar();
int f = 1;
if (c == '-') f = -1, c = getchar();
while (c > 32) x = x * 10 + c - 48, c = getchar();
return x * f;
}
template <class T>
inline void _putint(T x) {
return x ? _putint(x / 10), putchar(48 + x % 10), void() : void();
}
template <class T>
inline void putint(T x) {
if (x < 0) return putchar('-'), putint(-x), void();
return x == 0 ? putchar('0'), void() : _putint(x), void();
}
const int MAXN = 500005;
int n, m;
struct edge {
int u, v, id;
};
bool operator < (const edge &a, const edge &b) {
return a.u < b.u || (a.u == b.u && a.v < b.v);
}
edge edges[MAXN * 2];
edge *first[MAXN];
int stack[MAXN];
int stack_eid[MAXN];
bool instack[MAXN];
int top;
bool found;
bool is_on_cycle[MAXN];
void dfs(int u, int last) {
stack[++top] = u;
instack[u] = true;
for (edge *e = first[u]; e->u == u; e++) {
int v = e->v;
if (v == last) continue;
if (instack[v]) {
found = true;
is_on_cycle[e->id] = true;
while (stack[top] != v) {
is_on_cycle[stack_eid[top - 1]] = true;
--top;
}
return;
}
stack_eid[top] = e->id;
dfs(v, u);
if (found) return;
}
instack[u] = false;
--top;
}
int ans_idx;
bool visit[MAXN];
bool del;
void dfs(int u) {
putint(u), putchar(" \n"[++ans_idx == n]);
visit[u] = true;
++top;
bool done = false;
for (edge *e = first[u]; e->u == u; e++) {
int v = e->v;
if (visit[v]) continue;
edge *e1 = e + 1;
while (e1->u == u && visit[e1->v]) ++e1;
if (e1->u == u) {
stack[top] = e1->v;
} else {
--top;
done = true;
}
if (!del && is_on_cycle[e->id] && v > stack[top]) {
del = true;
} else {
dfs(v);
}
}
if (!done) --top;
}
int main() {
n = getint(), m = getint();
if (n == 1) {
return putchar('1'), putchar('\n'), IO::flush(), 0;
}
for (int i = 0; i < m; i++) {
int u, v;
u = getint(), v = getint();
edges[i * 2] = (edge){u, v, i};
edges[i * 2 + 1] = (edge){v, u, i};
}
std::sort(edges, edges + 2 * m);
first[1] = edges;
for (int i = 1; i < 2 * m; i++) {
if (edges[i].u != edges[i - 1].u) {
first[edges[i].u] = edges + i;
}
}
dfs(1, 0);
top = 0;
stack[0] = n + 1;
dfs(1);
IO::flush();
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 9.56 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 11.55 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 26.09 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 23.39 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 883.05 us | 612 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 879.85 us | 580 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 25.162 ms | 9 MB + 500 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 25.115 ms | 7 MB + 476 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 232.614 ms | 47 MB + 904 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 236.327 ms | 53 MB + 168 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 234.775 ms | 37 MB + 848 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 237.711 ms | 43 MB + 548 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 924.59 us | 636 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 900.06 us | 744 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 24.448 ms | 10 MB + 748 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 24.735 ms | 11 MB + 916 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 231.229 ms | 77 MB + 216 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 231.453 ms | 77 MB + 216 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 213.393 ms | 49 MB + 820 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 226.004 ms | 65 MB + 96 KB | Accepted | Score: 5 | 显示更多 |