#include <bits/stdc++.h>
#define maxn 200005
#define fin std::cin
#define fout std::cout
// std::ifstream fin("pipes.in");
// std::ofstream fout("pipes.out");
std::vector <int> edge[maxn];
int disc[maxn], low[maxn], timer;
bool visited[maxn];
std::vector <std::pair <int, int>> bridges;
void dfs(int node, int parent) {
visited[node] = true;
disc[node] = low[node] = ++timer;
for(auto next: edge[node]) {
if(visited[next] == false) {
dfs(next, node);
low[node] = std::min(low[node], low[next]);
if(low[next] > disc[node]) {
bridges.push_back({node, next});
}
}
else if(parent != next) {
low[node] = std::min(low[node], disc[next]);
}
}
}
class DSU {
int size;
std::vector <int> rank, parent;
public:
DSU(int size): size(size) {
rank.resize(size+1);
parent.resize(size+1);
for(int i = 1; i <= size; i ++)
parent[i] = i;
}
int findRoot(int node) {
if(parent[node] == node)
return node;
return parent[node] = findRoot(parent[node]);
}
void combine(int node1, int node2) {
node1 == findRoot(node1);
node2 == findRoot(node2);
if(node1 == node2)
return;
if(rank[node1] < rank[node2])
std::swap(node1, node2);
parent[node2] = node1;
if(rank[node1] == rank[node2])
rank[node1] ++;
}
bool sameComponent(int node1, int node2) {
node1 = findRoot(node1);
node2 = findRoot(node2);
return node1 == node2;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int V, E;
fin >> V >> E;
DSU first(V), second(V);
for(int i = 0, src, dest; i < E; i ++) {
fin >> src >> dest;
if(first.sameComponent(src, dest) == false) {
edge[src].push_back(dest);
edge[dest].push_back(src);
first.combine(src, dest);
}
else if(second.sameComponent(src, dest) == false) {
edge[src].push_back(dest);
edge[dest].push_back(src);
second.combine(src, dest);
}
}
for(int i = 1; i <= V; i ++)
if(visited[i] == false)
dfs(i, -1);
for(auto i: bridges)
fout << i.first << ' ' << i.second << '\n';
return 0;
}
Compilation message
pipes.cpp: In member function 'void DSU::combine(int, int)':
pipes.cpp:54:15: warning: value computed is not used [-Wunused-value]
54 | node1 == findRoot(node1);
| ~~~~~~^~~~~~~~~~~~~~~~~~
pipes.cpp:55:15: warning: value computed is not used [-Wunused-value]
55 | node2 == findRoot(node2);
| ~~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5024 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5760 KB |
Output is correct |
2 |
Incorrect |
7 ms |
5452 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
12868 KB |
Output is correct |
2 |
Correct |
98 ms |
11828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
195 ms |
21272 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
430 ms |
38616 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
704 ms |
55744 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1240 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1601 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2123 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2356 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |