#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;
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) {
for (auto &y : gph[x]) {
swab(x, y);
dfs(y, x);
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; i++)
dfs(i, -1);
cout << sz(seqs) << "\n";
for (auto &v : seqs) {
for (auto &w : v)
cout << w << " ";
cout << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
775 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
761 ms |
1048580 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
784 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
775 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
775 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |