#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int cmp1[100001], cmp2[100001];
vector<int> graph[100001];
bool visited[100001];
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;
}
void onion(int A, int B, int *cmp) {
cmp[find(A, cmp)] = find(B, cmp);
}
void dfs(int node, int parent = 0) {
visited[node] = true;
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 (find(u, cmp1) != find(v, cmp1)) {
onion(u, v, cmp1);
graph[u].push_back(v);
graph[v].push_back(u);
} else if (find(u, cmp2) != find(v, cmp2)) {
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:23:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int ptr = 0; ptr < graph[node].size(); ptr++) {
| ~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3180 KB |
Output is correct |
2 |
Correct |
6 ms |
2924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
3052 KB |
Output is correct |
2 |
Correct |
104 ms |
3052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
3948 KB |
Output is correct |
2 |
Correct |
203 ms |
3308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
293 ms |
5356 KB |
Output is correct |
2 |
Runtime error |
270 ms |
19052 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
423 ms |
10860 KB |
Output is correct |
2 |
Runtime error |
397 ms |
25452 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
624 ms |
11972 KB |
Output is correct |
2 |
Runtime error |
643 ms |
42356 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
840 ms |
14316 KB |
Output is correct |
2 |
Runtime error |
804 ms |
50668 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1061 ms |
20460 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1240 ms |
32876 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |