SHARE
TWEET

London Bridge

Slayerfeed Apr 23rd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using pii = pair<int,int> ;
  5.  
  6. int parent[100010];
  7.  
  8. int find(int u){
  9.     if(parent[u]==u){
  10.         return u;
  11.     }
  12.     return parent[u] = find(parent[u]);
  13. }
  14. int n ,m;
  15. void merge(int u,int v){
  16.     u=find(u);
  17.     v=find(v);
  18.     if(u==v){
  19.         return;
  20.     }
  21.     --n;
  22.     if(u>v){
  23.         parent[v]=u;
  24.     }
  25.     else{
  26.         parent[u]=v;
  27.     }
  28.  
  29. }
  30. int main(){
  31.  
  32.  
  33.     scanf("%d%d",&n,&m);
  34.     for(int i=1;i<=n;++i){
  35.         parent[i]=i;
  36.     }
  37.     int u ,v;
  38.  
  39.     vector<pii> bridge;
  40.     for(int i=1;i<=m;++i){
  41.         scanf("%d%d",&u,&v);
  42.  
  43.         bridge.push_back(make_pair(u,v));
  44.     }
  45.     int cnt=0;
  46.  
  47.     for(int i=m-1;i>=0;--i){
  48.         if(n==1){
  49.             break;
  50.         }
  51.         int start=bridge[i].first;
  52.         int end1 =bridge[i].second;
  53.         merge(start,end1);
  54.         ++cnt;
  55.  
  56.     }
  57.  
  58.  
  59.      cout << m-cnt;
  60.     return 0;
  61. }
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