#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e4 + 5;
template <typename T> void read(T &x) {
x = 0; int f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -f;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
x *= f;
}
struct edge {int dest, nxt, num; };
int n, m, head[MAXN], ans[MAXN];
int tot, dis, cnt, now[MAXN]; edge a[MAXN * 2];
vector <int> circle;
vector <pair <int, int> > b[MAXN];
int ftype[MAXN], father[MAXN], depth[MAXN];
void work(int pos, int fa) {
now[++cnt] = pos;
for (int i = head[pos]; i != 0; i = a[i].nxt)
if (a[i].num != dis && a[i].dest != fa) work(a[i].dest, pos);
}
void checkans(int x) {
dis = x, cnt = 0;
work(1, 0);
bool flg = false;
for (int i = 1; i <= n; i++)
if (now[i] < ans[i]) {
flg = true;
break;
} else if (now[i] > ans[i]) break;
if (flg) {
for (int i = 1; i <= n; i++)
ans[i] = now[i];
}
}
void dfs(int pos, int fa, int type) {
if (circle.size() != 0) return;
depth[pos] = depth[fa] + 1;
ftype[pos] = type;
father[pos] = fa;
for (int i = head[pos]; i != 0 && circle.size() == 0; i = a[i].nxt)
if (a[i].dest != fa) {
if (depth[a[i].dest]) {
circle.push_back(a[i].num);
int tmp = pos;
while (tmp != a[i].dest) {
circle.push_back(ftype[tmp]);
tmp = father[tmp];
}
return;
}
dfs(a[i].dest, pos, a[i].num);
}
}
bool cmp(pair <int, int> x, pair <int, int> y) {
if (x.first == y.first) return x.second < y.second;
else return x.first > y.first;
}
int main() {
read(n), read(m);
for (int i = 1; i <= m; i++) {
int x, y; read(x), read(y);
b[x].push_back(make_pair(y, i));
b[y].push_back(make_pair(x, i));
}
for (int i = 1; i <= n; i++) {
sort(b[i].begin(), b[i].end(), cmp);
for (unsigned j = 0; j < b[i].size(); j++)
a[++tot] = (edge) {b[i][j].first, head[i], b[i][j].second}, head[i] = tot;
}
ans[1] = n + 1;
if (m == n) {
dfs(1, 0, 0);
for (unsigned i = 0; i < circle.size(); i++)
checkans(circle[i]);
} else checkans(0);
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
printf("\n");
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 72.59 us | 292 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 80 us | 300 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 98.8 us | 296 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 97.61 us | 296 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 1.328 ms | 776 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 1.322 ms | 760 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 113.34 us | 740 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #8 | 164.16 us | 1 MB + 144 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #9 | 157.63 us | 1 MB + 76 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #10 | 132.1 us | 888 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #11 | 309.98 us | 2 MB + 320 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #12 | 392.25 us | 2 MB + 992 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #13 | 265.204 ms | 948 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 330.44 ms | 1000 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 132.7 us | 904 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #16 | 56.45 us | 280 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #17 | 230.58 us | 1 MB + 668 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #18 | 378.18 us | 2 MB + 816 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #19 | 395.74 us | 2 MB + 1008 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #20 | 1.302 ms | 3 MB + 28 KB | Runtime Error | Score: 0 | 显示更多 |