#include <bits/stdc++.h>
#pragma GCC opimize("O3")
using namespace std;
vector<vector<int>> muchii;
vector<int> nodes;
int comp1[100005],comp2[100005],lg1[100005],lg2[100005];
bool use[100005];
int n,m;
void dfs(int nod)
{
use[nod]=1;
comp2[nod]=lg1[nod];
for(int z=0;z<muchii[nod].size();z++)
{
int node=muchii[nod][z];
if(node==comp1[nod])
continue;
if(!use[node])
{
comp1[node]=nod;
lg1[node]=lg1[nod]+1;
dfs(node);
comp2[nod]=min(comp2[nod],comp2[node]);
}
else
comp2[nod]=min(comp2[nod],lg1[node]);
}
int cnt=0;
if(comp2[nod]==lg1[nod]&&comp1[nod]!=0)
{
for(int z=0;z<muchii[nod].size();z++)
{
int i=muchii[nod][z];
if(i==comp1[nod])
cnt++;
}
if(cnt==1)
cout<<comp1[nod]<<' '<<nod<<'\n';
}
}
void dfs1(int nod)
{
use[nod]=1;
nodes.push_back(nod);
for(int z=0;z<muchii[nod].size();z++)
{
int i=muchii[nod][z];
if(comp1[nod]==comp1[i]&&!use[i])
dfs1(i);
}
}
void dfs2(int nod)
{
use[nod]=1;
nodes.push_back(nod);
for(int z=0;z<muchii[nod].size();z++)
{
int i=muchii[nod][z];
if(comp2[nod]==comp2[i]&&!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();
nodes.shrink_to_fit();
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()
{
ios_base::sync_with_stdio(false);
cin>>n>>m;
muchii.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);
muchii[a].push_back(b);
muchii[b].push_back(a);
cnt++;
}
else
{
if(comp2[a]!=comp2[b])
{
dsu_merge2(a,b);
muchii[a].push_back(b);
muchii[b].push_back(a);
cnt++;
}
}
}
for(int i=1;i<=n;i++)
{
comp1[i]=0;
comp2[i]=0;
lg1[i]=0;
lg2[i]=0;
}
for(int i=1;i<=n;i++)
if(!use[i])
dfs(i);
return 0;
}
Compilation message
pipes.cpp:2: warning: ignoring '#pragma GCC opimize' [-Wunknown-pragmas]
2 | #pragma GCC opimize("O3")
|
pipes.cpp: In function 'void dfs(int)':
pipes.cpp:13:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for(int z=0;z<muchii[nod].size();z++)
| ~^~~~~~~~~~~~~~~~~~~
pipes.cpp:31:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int z=0;z<muchii[nod].size();z++)
| ~^~~~~~~~~~~~~~~~~~~
pipes.cpp: In function 'void dfs1(int)':
pipes.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int z=0;z<muchii[nod].size();z++)
| ~^~~~~~~~~~~~~~~~~~~
pipes.cpp: In function 'void dfs2(int)':
pipes.cpp:56:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int z=0;z<muchii[nod].size();z++)
| ~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
964 KB |
Output is correct |
2 |
Correct |
4 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
756 KB |
Output is correct |
2 |
Correct |
74 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
1624 KB |
Output is correct |
2 |
Correct |
166 ms |
1172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
227 ms |
3532 KB |
Output is correct |
2 |
Runtime error |
206 ms |
17244 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
311 ms |
10052 KB |
Output is correct |
2 |
Runtime error |
305 ms |
24876 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
486 ms |
11408 KB |
Output is correct |
2 |
Runtime error |
468 ms |
41976 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
637 ms |
14028 KB |
Output is correct |
2 |
Runtime error |
667 ms |
50924 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
787 ms |
17704 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
932 ms |
30100 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |