#include <bits/stdc++.h>
using namespace std;
int s1[100005],s2[100005],ufds1[100005],ufds2[100005],low[100005],depths[100005];
vector<int> adj[100005];
int find(int a,int par[]){
if (par[a]==a) return a;
par[a] = find(par[a],par);
return par[a];
}
void dfs(int cur,int par){
depths[cur] = depths[par] + 1;
low[cur] = depths[cur];
bool check= false;
for (auto it: adj[cur]){
if (check == 0 && it==par){
check = true;
}
else{
if (depths[it]==-1){
dfs(it,cur);
low[cur] = min(low[cur],low[it]);
if (low[it]>depths[cur]){
cout << cur << ' ' << it << '\n';
}
}else{
low[cur] = min(low[cur],depths[it]);
}
}
}
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,m;cin>>n>>m;
fill(s1,s1+100005,1);
fill(s2,s2+100005,1);
fill(depths,depths+100005,-1);
for (int i=1;i<=n;++i){
ufds1[i] = i;
ufds2[i] = i;
}
for (int i=0;i<m;++i){
int a,b;cin>>a>>b;
int pa = find(a,ufds1), pb = find(b,ufds2);
if (pa!=pb){
adj[a].push_back(b);
adj[b].push_back(a);
if (s1[pa]>s1[pb]){
ufds1[pb] = pa;
s1[pa] += s1[pb];
}else{
ufds2[pa] = pb;
s1[pb] += s1[pa];
}
}else{ // it is a back edge
int ba = find(a,ufds2), bb = find(b,ufds2);
if (ba!=bb){
adj[a].push_back(b);
adj[b].push_back(a);
if (s2[ba]>s2[bb]){
ufds2[bb] = ba;
s2[ba] += s2[bb];
}else{
ufds2[ba] = bb;
s2[bb] += s2[ba];
}
}
}
}
for (int i=1;i<=n;++i){
if (depths[i]==-1){
dfs(i,i);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3788 KB |
Output is correct |
2 |
Correct |
3 ms |
3788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4556 KB |
Output is correct |
2 |
Correct |
7 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
12228 KB |
Output is correct |
2 |
Correct |
126 ms |
12520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
245 ms |
16576 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
441 ms |
27900 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
771 ms |
35836 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1287 ms |
56272 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1813 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2138 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2317 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |