#include <vector>
#include <iostream>
#include <cassert>
#include <map>
using namespace std;
int n, m, x, y, sz[100002], p[100002], dp[20][100002], depth[100002];
vector<int>G[100002];
int find (int x) {
if (p[x] == x)
return x;
return p[x] = find(p[x]);
}
void dfs2 (int x, int par) {
dp[0][x] = par;
depth[x] = depth[par] + 1;
for (int i = 1; (1 << i) < n; i++)
dp[i][x] = dp[i - 1][dp[i - 1][x]];
for (auto it : G[x]) {
if (it == par)
continue;
dfs2(it, x);
}
}
void merge (int x, int y) {
int px = find(x);
int py = find(y);
if (px == py)
return;
if (sz[px] > sz[py]) {
swap(x, y);
swap(px, py);
}
G[x].push_back(y);
G[y].push_back(x);
p[px] = py;
sz[py] += sz[px];
dfs2(x, y);
}
int lca (int x, int y) {
if (depth[x] > depth[y])
swap(x, y);
int d = depth[y] - depth[x];
for (int i = 0; (1 << i) <= d; i++)
if (d & (1 << i))
y = dp[i][y];
if (x == y)
return x;
for (int i = 19; i >= 0; i--)
if ((1 << i) != n && dp[i][x] != dp[i][y]) {
x = dp[i][x];
y = dp[i][y];
}
return dp[0][x];
}
int add[100002], viz[100002];
map<pair<int, int>, int>mp;
map<pair<int, int>, int>cnt;
void dfs (int x, int par) {
viz[x] = 1;
for (auto it : G[x]) {
if (it == par)
continue;
dfs(it, x);
add[x] += add[it];
}
if (add[x] == 0 && par != 0) {
if(mp[make_pair(min(x, par), max(x, par))]!=1) {
assert(cnt[make_pair(min(x, par), max(x, par))] == 1);
}
cout << x << " " << par << "\n";
}
}
void dfs3 (int x, pair<int, int> edge) {
viz[x] = 1;
int i = 0;
for (auto it : G[x]) {
if (edge == make_pair(x, i)) {
i++;
continue;
}
if (!viz[it])
dfs3(it, edge);
i++;
}
}
pair<int, int>e[202];
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
p[i] = i;
sz[i] = 1;
}
if (m <= 200) {
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
cnt[make_pair(min(x, y), max(x, y))]++;
e[i] = {x, y};
G[x].push_back(y);
G[y].push_back(x);
}
for (int x = 1; x <=n; x++) {
for (int j = 0; j < G[x].size(); j++) {
int val = G[x][j];
for (int k = 1; k <= n; k++)
viz[k] = 0;
dfs3(x, {x, j});
if (!viz[val] && x < val) {
// cout << x << " " << val << "\n";
mp[{x, val}]=1;
}
}
}
for (int i = 1; i <= n; i++)
G[i].clear();
}
for (int i = 1; i <= m; i++) {
auto [x, y] = e[i];
if (x > y)
swap(x, y);
if (find(x) == find(y)) {
add[x]++;
add[y]++;
int L = lca(x, y);
add[L] -= 2;
}
else {
merge(x, y);
//assert(abs(depth[x] - depth[y]) == 1);
}
}
for (int k = 1; k <= n; k++)
viz[k] = 0;
for (int i = 1; i <= n; i++)
if (!viz[find(i)])
dfs(find(i), 0);
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:119:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for (int j = 0; j < G[x].size(); j++) {
| ~~^~~~~~~~~~~~~
pipes.cpp:134:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
134 | auto [x, y] = e[i];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Runtime error |
4 ms |
5332 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
5332 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
5332 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
5332 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
5588 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
6320 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
6484 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
6740 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
6740 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
7 ms |
6708 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |