SHARE
TWEET

Untitled

a guest Apr 26th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. using namespace std;
  3. main(){
  4.     int n,m,i,j,k=0,c0,c1;
  5.     cin>>n>>m;
  6.     int a[m][2],c[n+1];bool b[m];
  7.     for(i=0;i<m;++i){
  8.         b[i]=1;
  9.         cin>>a[i][0]>>a[i][1];
  10.     }
  11.     for(j=1;j<=n;++j)
  12.         c[j]=j;
  13.     while(k<n-1){
  14.         for(i=0;i<m;++i)
  15.             if(b[i]&&c[a[i][0]]!=c[a[i][1]]){
  16.                     b[i]=0;
  17.                     c0=c[a[i][0]];
  18.                     c1=c[a[i][1]];
  19.                 for(j=1;j<=n;++j)if(c[j]==c1)
  20.                     c[j]=c0;
  21.             k++;break;}
  22.         }
  23.     for(i=0;i<m;++i)if(!b[i])
  24.         cout<<a[i][0]<<' '<<a[i][1]<<endl;
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top