#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, sz[100002], p[100002], dp[20][100002], depth[100002];
vector<vector<int>>v[100002];
vector<int>G[100002];
int find (int x) {
if (p[x] == x)
return x;
return p[x] = find(p[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);
}
p[px] = py;
sz[py] += sz[px];
for (int i = y; i; i = dp[0][i]) {
for (int j = 0; (1 << j) < n; j++) {
int lvl = (1 << j) - 1 - (depth[y] - depth[i]);
if (lvl >= 0 && lvl < v[px].size())
for (auto it : v[px][lvl])
dp[j][it] = i;
}
}
for (int i = 0; i < v[px].size(); i++) {
while (depth[y] + i + 1 >= v[py].size())
v[py].push_back({});
for (auto it : v[px][i]) {
assert(depth[it] == i);
v[py][depth[y] + i + 1].push_back(it);
depth[it]+=depth[y] + 1;
}
}
}
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 (dp[i][x] != dp[i][y]) {
x = dp[i][x];
y = dp[i][y];
}
return dp[0][x];
}
int add[100002], viz[100002];
void dfs (int x, int par) {
//cout << x << "\n";
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)
cout << x << " " << par << "\n";
}
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;
v[i].push_back({i});
}
for (int i = 1; i <= m; i++) {
cin >> x >> y;
if (find(x) == find(y)) {
int L = lca(x, y);
add[L] -= 2;
add[x]++;
add[y]++;
}
else {
merge(x, y);
if (depth[x] < depth[y]) {
G[x].push_back(y);
}
else {
G[y].push_back(x);
}
}
}
for (int i = 1; i <= n; i++)
if (!viz[find(i)])
dfs(find(i), 0);
return 0;
}
Compilation message
pipes.cpp: In function 'void merge(int, int)':
pipes.cpp:30:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | if (lvl >= 0 && lvl < v[px].size())
| ~~~~^~~~~~~~~~~~~~
pipes.cpp:35:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i = 0; i < v[px].size(); i++) {
| ~~^~~~~~~~~~~~~~
pipes.cpp:36:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | while (depth[y] + i + 1 >= v[py].size())
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5076 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
5800 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
5648 KB |
Output is correct |
2 |
Incorrect |
127 ms |
5612 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
257 ms |
6628 KB |
Output is correct |
2 |
Incorrect |
309 ms |
6668 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
442 ms |
8920 KB |
Output is correct |
2 |
Incorrect |
453 ms |
10560 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
765 ms |
16904 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1191 ms |
18616 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1836 ms |
21688 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2330 ms |
21692 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2801 ms |
20884 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |