#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> p1(100001,-1);
vector<int> p2(100001,-1);
vector<bool> vis;
vector<pair<int,int> > vk;
vector<vector<int> > v;
vector<int> rednibroj;
int edges=0;
int broj=1;
int dsu1(int x)
{
int px=x;
while(p1[x]!=-1)
x=p1[x];
int y=x;
while(px!=y)
{
int xx=p1[px];
p1[px]=y,px=xx;
}
return x;
}
int dsu2(int x)
{
int px=x;
while(p2[x]!=-1)
x=p2[x];
int y=x;
while(px!=y)
{
int xx=p2[px];
p2[px]=y,px=xx;
}
return x;
}
int bridges(int x,int prev,int broj)
{
rednibroj[x]=broj;
int minbr=broj;
int maxi=0;
int granki=0;
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==prev)
continue;
if(rednibroj[v[x][i]])
minbr=min(minbr,rednibroj[v[x][i]]);
else
{
int t=bridges(v[x][i],x,broj+1);
maxi=max(maxi,t);
minbr=min(minbr,t);
granki++;
}
}
if(maxi>=rednibroj[x])
{
if(x>=edges)
vis[x-edges]=true;
}
return minbr;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
a--;
b--;
int x=dsu1(a);
int y=dsu1(b);
if(x!=y)
{
p1[x]=y;
vk.push_back(make_pair(a,b));
vis.push_back(0);
edges++;
}
else
{
int x1=dsu2(a);
int y1=dsu2(b);
if(x1!=y1)
{
p2[x1]=y1;
vk.push_back(make_pair(a,b));
vis.push_back(0);
edges++;
}
}
}
p1.clear();
p2.clear();
vector<int> vec;
v.insert(v.begin(),2*(n+edges+2),vec);
for(int i=0;i<2*(n+edges+3);i++)
{
rednibroj.push_back(0);
}
int br=edges;
for(int i=0;i<edges;i++)
{
pair<int,int> p=vk[i];
v[p.first].push_back(br);
v[br].push_back(p.first);
v[br].push_back(p.second);
v[p.second].push_back(br);
br++;
}
for(int i=0;i<n;i++)
{
if(!rednibroj[i])
{
bridges(i,-1,1);
}
}
for(int i=0;i<edges;i++)
{
if(vis[i])
{
cout<<vk[i].first+1<<" "<<vk[i].second+1<<endl;
}
}
return 0;
}
Compilation message
pipes.cpp: In function 'int bridges(int, int, int)':
pipes.cpp:45:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1152 KB |
Output is correct |
2 |
Correct |
3 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2816 KB |
Output is correct |
2 |
Correct |
9 ms |
2432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
2588 KB |
Output is correct |
2 |
Correct |
104 ms |
2144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
4912 KB |
Output is correct |
2 |
Correct |
213 ms |
3888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
333 ms |
10396 KB |
Output is correct |
2 |
Correct |
284 ms |
9692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
546 ms |
28724 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
850 ms |
32844 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1030 ms |
40288 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1226 ms |
40204 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1470 ms |
38288 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |