#include <bits/stdc++.h>
#define MAXN 100005
#define pb push_back
#define mp make_pair
using namespace std;
int n,m;
int otacs[MAXN],vels[MAXN],otaco[MAXN],velo[MAXN];
int predak(int p,int *otac,int *vel){
while(p!=otac[p]){
otac[p]=otac[otac[p]];
p=otac[p];
}
return p;
}
void spoji(int a,int b,int *otac,int *vel){
a=predak(a,otac,vel);
b=predak(b,otac,vel);
if(a==b)return;
if(vel[a]<vel[b])swap(a,b);
otac[b]=a;
vel[a]+=vel[b];
}
void init(){
for(int i=0;i<MAXN;i++){
otacs[i]=otaco[i]=i;
vels[i]=velo[i]=1;
}
}
vector<int> niz[MAXN];
bool obidjen[MAXN];
int dfs(int t,int o){
obidjen[t]=true;
int vpodst=1;
for(auto x:niz[t])
if(x!=o)
vpodst+=dfs(x,t);
if(o==0)return vpodst;
//printf(" %d %d %d\n",t,velo[predak(t,otaco,velo)],vpodst);
if(velo[predak(t,otaco,velo)]==vpodst)printf("%d %d\n",min(t,o),max(t,o));
spoji(t,o,otaco,velo);
return vpodst;
}
int main()
{
init();
scanf("%d %d",&n, &m);
for(int i=1;i<=m;i++){
int t1,t2;
scanf("%d %d",&t1,&t2);
if(predak(t1,otacs,vels)!=predak(t2,otacs,vels)){
spoji(t1,t2,otacs,vels);
niz[t1].pb(t2);
niz[t2].pb(t1);
}else{
//printf(" %d %d\n",t1,t2);
spoji(t1,t2,otaco,velo);
}
}
for(int i=1;i<=n;i++){
if(!obidjen[i]){
//printf(" %d\n",i);
dfs(i,0);
}
}
/*for(auto x:grane){
int pa=predak(x.first,otaco,velo),pb=predak(x.second,otaco,velo);
//printf(" %d %d\n",pa,pb);
if(pa>pb)swap(pa,pb); ///PAZI
if(s.find(mp(pa,pb))==s.end())s.insert(mp(pa,pb));
else s2.insert(mp(pa,pb));
}
for(auto x:grane){
int pa=predak(x.first,otaco,velo),pb=predak(x.second,otaco,velo);
if(pa>pb)swap(pa,pb); ///PAZI
if(s2.find(mp(pa,pb))==s2.end() && pa!=pb)printf("%d %d\n",x.first,x.second);
}*/
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n, &m);
~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&t1,&t2);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
4472 KB |
Output is correct |
2 |
Correct |
10 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
5368 KB |
Output is correct |
2 |
Correct |
139 ms |
5244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
5172 KB |
Output is correct |
2 |
Correct |
280 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
408 ms |
5696 KB |
Output is correct |
2 |
Correct |
341 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
514 ms |
6936 KB |
Output is correct |
2 |
Correct |
459 ms |
7160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
804 ms |
8152 KB |
Output is correct |
2 |
Correct |
808 ms |
8136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1071 ms |
9336 KB |
Output is correct |
2 |
Correct |
1020 ms |
8952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1349 ms |
8872 KB |
Output is correct |
2 |
Correct |
1307 ms |
9344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1657 ms |
8156 KB |
Output is correct |
2 |
Runtime error |
1620 ms |
18744 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |