#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
int h[N], mnh[N], edges;
bool vis[N];
vector<pair<int, int>> g[N];
int root1(int v){
return (h[v] == -1 ? v : h[v] = root1(h[v]));
}
int root2(int v){
return (mnh[v] == -1 ? v : mnh[v] = root2(mnh[v]));
}
void merge1(int u, int v){
int r1 = root1(v);
int r2 = root1(u);
h[r1] = r2;
edges++;
// g[v].push_back({u, edges});
// g[u].push_back({v, edges});
}
void merge2(int u, int v){
int r1 = root2(u);
int r2 = root2(v);
if (r1 == r2)
return;
mnh[r1] = r2;
edges++;
// g[v].push_back({u, edges});
// g[u].push_back({v, edges});
}
void dfs(int v, pair<int, int> prev = {-1, -1}){
vis[v] = 1;
if (prev.first == -1)
h[v] = 0;
mnh[v] = h[v];
for (auto& [u, id] : g[v]){
if (prev.first == u and prev.second == id)
continue;
mnh[v] = min(mnh[v], h[u]);
if (vis[u])
continue;
h[u] = h[v] + 1;
dfs(u, {v, id});
mnh[v] = min(mnh[v], mnh[u]);
if (mnh[u] >= h[u])
printf("%d %d\n", u, v);
}
}
int main(){
int n, m;
scanf("%d%d", &n, &m);
for (int i=1; i<=n; i++)
h[i] = mnh[i] = -1;
for (int i=0; i<m; i++){
int u, v;
scanf("%d%d", &u, &v);
if (root1(u) != root1(v))
merge1(u, v);
else if (root2(u) != root2(v))
merge2(u, v);
}
if (edges > 2 * N){
exit(-1);
return 0;
}
for (int i=1; i<=n; i++)
h[i] = mnh[i] = n + 100;
for (int v = 1; v <= n; v++)
if (!vis[v])
dfs(v);
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2648 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
2652 KB |
Output is correct |
2 |
Incorrect |
83 ms |
2796 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
170 ms |
2848 KB |
Output is correct |
2 |
Incorrect |
161 ms |
2848 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
225 ms |
2908 KB |
Output is correct |
2 |
Incorrect |
205 ms |
3152 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
276 ms |
3164 KB |
Output is correct |
2 |
Incorrect |
274 ms |
3316 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
446 ms |
3392 KB |
Output is correct |
2 |
Incorrect |
452 ms |
3392 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
574 ms |
3540 KB |
Output is correct |
2 |
Incorrect |
594 ms |
3672 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
720 ms |
3544 KB |
Output is correct |
2 |
Incorrect |
683 ms |
3664 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
882 ms |
3512 KB |
Output is correct |
2 |
Incorrect |
879 ms |
3548 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |