#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> muchii1,muchii2;
vector<int> nodes;
int comp1[100005],comp2[100005],par[100005],niv[100005],nivmin[100005],lg1[100005],lg2[100005];
bool use[100005];
int n,m;
void dfs(int nod)
{
use[nod]=1;
nivmin[nod]=niv[nod];
for(int i:muchii1[nod])
{
int node=i;
if(node==par[nod])
continue;
if(!use[node])
{
par[node]=nod;
niv[node]=niv[nod]+1;
dfs(node);
nivmin[nod]=min(nivmin[nod],nivmin[node]);
}
else
nivmin[nod]=min(nivmin[nod],niv[node]);
}
for(int i:muchii2[nod])
{
int node=i;
if(node==par[nod])
continue;
if(!use[node])
{
par[node]=nod;
niv[node]=niv[nod]+1;
dfs(node);
nivmin[nod]=min(nivmin[nod],nivmin[node]);
}
else
nivmin[nod]=min(nivmin[nod],niv[node]);
}
int cnt=0;
if(nivmin[nod]==niv[nod]&&par[nod]!=0)
{
for(int i:muchii1[nod])
if(i==par[nod])
cnt++;
for(int i:muchii2[nod])
if(i==par[nod])
cnt++;
if(cnt==1)
cout<<par[nod]<<' '<<nod<<'\n';
}
}
void dfs1(int nod)
{
use[nod]=1;
nodes.push_back(nod);
for(int i:muchii1[nod])
if(!use[i])
dfs1(i);
}
void dfs2(int nod)
{
use[nod]=1;
nodes.push_back(nod);
for(int i:muchii2[nod])
if(!use[i])
dfs2(i);
}
void dsu_merge1(int a,int b)
{
int c1=comp1[a],c2=comp1[b];
if(lg1[c1]<lg1[c2])
{
swap(c1,c2);
swap(a,b);
}
nodes.clear();
dfs1(b);
for(int i:nodes)
{
comp1[i]=c1;
use[i]=0;
}
lg1[c1]+=lg1[c2];
lg1[c2]=0;
}
void dsu_merge2(int a,int b)
{
int c1=comp2[a],c2=comp2[b];
if(lg2[c1]<lg2[c2])
{
swap(c1,c2);
swap(a,b);
}
nodes.clear();
dfs2(b);
for(int i:nodes)
{
comp2[i]=c1;
use[i]=0;
}
lg2[c1]+=lg2[c2];
lg2[c2]=0;
}
int main()
{
cin>>n>>m;
muchii1.resize(n+1);
muchii2.resize(n+1);
for(int i=1;i<=n;i++)
{
comp1[i]=i;
comp2[i]=i;
lg1[i]=1;
lg2[i]=1;
}
int cnt=0;
for(int i=1;i<=m;i++)
{
int a,b;
cin>>a>>b;
if(comp1[a]!=comp1[b])
{
dsu_merge1(a,b);
muchii1[a].push_back(b);
muchii1[b].push_back(a);
cnt++;
}
else
{
if(comp2[a]!=comp2[b])
{
dsu_merge2(a,b);
muchii2[a].push_back(b);
muchii2[b].push_back(a);
cnt++;
}
}
}
assert(cnt<=2*n);
for(int i=1;i<=n;i++)
if(!use[i])
dfs(i);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1108 KB |
Output is correct |
2 |
Correct |
11 ms |
960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
267 ms |
3216 KB |
Output is correct |
2 |
Correct |
266 ms |
3136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
486 ms |
4444 KB |
Output is correct |
2 |
Correct |
530 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
723 ms |
7216 KB |
Output is correct |
2 |
Correct |
604 ms |
4836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
967 ms |
14144 KB |
Output is correct |
2 |
Correct |
834 ms |
10736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1483 ms |
16200 KB |
Output is correct |
2 |
Runtime error |
1479 ms |
45816 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2065 ms |
22156 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2400 ms |
29388 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3104 ms |
33996 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |