#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int M = 1e5+1;
vector<int> node[M];
int a[M], tt = 1;
bitset<100005> vis;
void dfs(int s, int p = 1) {
a[s] = tt++;
vis[s] = true;
for (int i:node[s]) {
if (!vis[i]) {
dfs(i, s);
if (a[i] > a[s]) cout << s << ' ' << i << endl;
}
}
for (int i:node[s]) if (i != p && vis[i]) a[s] = min(a[s], a[i]);
}
int pr[M], pb[M], rnk[M], bnk[M];
void uniR(int a, int b) {
if (rnk[a] < rnk[b]) swap(a, b);
if (rnk[a] == rnk[b]) rnk[a]++;
pr[b] = a;
}
void uniB(int a, int b) {
if (bnk[a] < bnk[b]) swap(a, b);
if (bnk[a] == bnk[b]) bnk[a]++;
pb[b] = a;
}
int findR(int s) {
return (s==pr[s]?s:pr[s]=findR(pr[s]));
}
int findB(int s) {
return (s==pb[s]?s:pb[s]=findB(pb[s]));
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) pb[i] = pr[i] = i;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
if (findB(a) == findB(b) && findR(a) == findR(b)) continue;
node[a].push_back(b);
node[b].push_back(a);
if (findR(a) != findR(b)) uniR(a, b);
else uniB(a, b);
}
for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i); vis = 0;
return 0;
}
/*
10 11
1 7
1 8
1 6
2 8
6 7
5 8
2 5
2 3
2 4
3 4
10 9
*/
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:61:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
61 | for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i); vis = 0;
| ^~~
pipes.cpp:61:55: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
61 | for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i); vis = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3248 KB |
Output is correct |
2 |
Incorrect |
4 ms |
3028 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
4912 KB |
Output is correct |
2 |
Correct |
96 ms |
4116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
9564 KB |
Output is correct |
2 |
Correct |
195 ms |
7328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
392 ms |
19488 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
651 ms |
33148 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1084 ms |
55392 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1500 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1826 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2029 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |