#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = array<lint, 2>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define cr(v, n) (v).clear(), (v).resize(n);
const int MAXN = 105;
struct disj {
vector<int> pa;
void init(int n) {
pa.clear();
pa.resize(n);
iota(all(pa), 0);
}
int find(int x) { return pa[x] = (pa[x] == x ? x : find(pa[x])); }
bool uni(int p, int q) {
p = find(p);
q = find(q);
if (p == q)
return 0;
pa[q] = p;
return 1;
}
} disj;
int n;
vector<int> gph[MAXN];
vector<vector<int>> seqs;
int u, v, reach, par[MAXN], vis[MAXN];
pi dfs_far(int x, int p = -1) {
pi ans{0, x};
for (auto &y : gph[x]) {
if (y != p) {
auto [d, w] = dfs_far(y, x);
par[y] = x;
ans = max(ans, pi{d + 1, w});
}
}
return ans;
}
void swab(int u, int v) {
vector<int> a(n);
iota(all(a), 0);
a[v] = u;
seqs.push_back(a);
}
void dfs(int x, int p = -1) {
if (x == v)
reach = 1;
sort(all(gph[x]), [&](int a, int b) { return vis[a] < vis[b]; });
for (auto &y : gph[x]) {
if (y == p)
continue;
swab(x, y);
dfs(y, x);
if (!reach)
swab(x, y);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int m;
cin >> n >> m;
disj.init(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
if (disj.uni(u, v)) {
gph[u].push_back(v);
gph[v].push_back(u);
}
}
for (int i = 0; i < n - 1; i++) {
int r = 0;
while (sz(gph[r]) == 0)
r++;
u = dfs_far(r)[1];
v = dfs_far(u)[1];
memset(vis, 0, sizeof(vis));
for (int j = v; j != u; j = par[j])
vis[j] = 1;
reach = 0;
dfs(u);
int k = gph[u][0];
swab(u, k);
gph[k].erase(find(all(gph[k]), u));
gph[u].clear();
}
cout << sz(seqs) << "\n";
for (auto &v : seqs) {
for (auto &w : v)
cout << w << " ";
cout << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
If people start at 1 and 2, then they can avoid each other |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
If people start at 0 and 1, then they can avoid each other |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
If people start at 0 and 1, then they can avoid each other |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
If people start at 1 and 2, then they can avoid each other |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
If people start at 1 and 2, then they can avoid each other |
4 |
Halted |
0 ms |
0 KB |
- |