#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int cmp1[100001], cmp2[100001];
vector<int> graph[100001];
bitset<100001> visited;
int tin[100001], low[100001], timer = 0;
int find(int A, int *cmp) {
while (cmp[A] != A) cmp[A] = cmp[cmp[A]], A = cmp[A];
return A;
}
bool onion(int A, int B, int *cmp) {
if (find(A, cmp) == find(B, cmp)) return false;
cmp[find(A, cmp)] = find(B, cmp);
return true;
}
void dfs(int node, int parent = 0) {
visited[node] = 1;
tin[node] = low[node] = timer++;
bool multi = false;
for (int ptr = 0; ptr < graph[node].size(); ptr++) {
int i = graph[node][ptr];
if (i != parent || multi) {
if (visited[i]) low[node] = min(low[node], tin[i]);
else {
dfs(i, node);
low[node] = min(low[node], low[i]);
}
} else multi = true;
}
if (parent && tin[node] == low[node]) cout << node << ' ' << parent << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
iota(cmp1 + 1, cmp1 + n + 1, 1);
iota(cmp2 + 1, cmp2 + n + 1, 1);
while (m--) {
int u, v;
cin >> u >> v;
if (onion(u, v, cmp1) || onion(u, v, cmp2)) {
graph[u].push_back(v);
graph[v].push_back(u);
}
}
for (int i = 1; i <= n; i++) if (!visited[i]) dfs(i);
return 0;
}
Compilation message
pipes.cpp: In function 'void dfs(int, int)':
pipes.cpp:25:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int ptr = 0; ptr < graph[node].size(); ptr++) {
| ~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3180 KB |
Output is correct |
2 |
Correct |
6 ms |
2924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
3052 KB |
Output is correct |
2 |
Correct |
103 ms |
2924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
3820 KB |
Output is correct |
2 |
Correct |
215 ms |
3340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
301 ms |
5484 KB |
Output is correct |
2 |
Correct |
252 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
434 ms |
10732 KB |
Output is correct |
2 |
Correct |
398 ms |
7148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
643 ms |
11884 KB |
Output is correct |
2 |
Correct |
599 ms |
8940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
863 ms |
14284 KB |
Output is correct |
2 |
Correct |
804 ms |
9068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1066 ms |
14060 KB |
Output is correct |
2 |
Correct |
1012 ms |
9196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1305 ms |
13432 KB |
Output is correct |
2 |
Runtime error |
1196 ms |
23180 KB |
Memory limit exceeded |