Advertisement
Rana_093

Uva-459(DEMO)[DisjointSetUnion]

Mar 19th, 2017
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. #define i64 long long int
  7. #define pi acos(-1.00)
  8.  
  9. struct side
  10. {
  11.     char a;
  12.     char b;
  13. }
  14. get;
  15.  
  16. map<char,char>parent;
  17.  
  18.  
  19.  
  20.  
  21. void Make(char ch)
  22. {
  23.     parent[ch]=ch;
  24. }
  25.  
  26.  
  27.  
  28. char Find(char ch)
  29. {
  30.     if(parent[ch]==ch)
  31.     {
  32.         return ch;
  33.     }
  34.  
  35.     return parent[ch]=Find(parent[ch]);
  36. }
  37.  
  38.  
  39.  
  40. void  setunion(char a,char b)
  41. {
  42.     char A = Find(a);
  43.     char B = Find(b);
  44.     if(A==B)
  45.     {
  46.         return ;
  47.     }
  48.     else
  49.     {
  50.         parent[A]=B;
  51.         cout<<"parent A ->  " <<parent[A]<<" Parent B -> "<<parent[B]<<endl;
  52.         return;
  53.     }
  54.  
  55. }
  56.  
  57.  
  58.  
  59. int main()
  60. {
  61.     ios_base::sync_with_stdio(false);
  62.     int i,t,j,k;
  63.  
  64.     // cin>>t;
  65.     char ch;
  66.     char str;
  67.     char s[3];
  68.  
  69.     char space1;
  70.     char maxN;
  71.     vector<side>vec;
  72.     for(int i = 1; i<=t; i++)
  73.     {
  74.  
  75.  
  76.         cin>>str;
  77.         cout<<"hi "<<endl;
  78.         int cnt[30];
  79.         memset(cnt,0,sizeof(cnt));
  80.         maxN = str;
  81.         int p = maxN-0;
  82.         char c;
  83.         for(int j= 65; j<=p; j++)
  84.         {
  85.             c = j;
  86.             Make(c);
  87.             /// cout<<parent[c]<<endl;
  88.         }
  89.         cout<<"hello"<<endl;
  90.         char ch1,ch2;
  91.  
  92.         while( gets(s) )
  93.         {
  94.             cout<<"s -> "<<s<<endl;
  95.             ch1 = s[0];
  96.             if(ch==' ')
  97.             {
  98.                 return 0;
  99.             }
  100.             ch2 = s[1];
  101.             get.a =ch1;
  102.             get.b = ch2;
  103.             vec.push_back(get);
  104.             cout<<"Pushed "<<endl;
  105.         }
  106.         for(int j =0 ; j<vec.size(); j++)
  107.         {
  108.             char from = vec[i].a;
  109.             char to = vec[i].b;
  110.             setunion(from,to);
  111.         }
  112.         map<char,char>::iterator it;
  113.  
  114.  
  115.  
  116.  
  117.         for(it =  parent.begin(); it!= parent.end(); ++it)
  118.         {
  119.             char C = it->second;
  120.             char C1 = it->first;
  121.             cout<<"C -> "<<C<<" C1-> "<<C1<<endl;
  122.  
  123.         }
  124.  
  125.         int mx =  INT_MIN;
  126.         for(int i =0; i<30; i++)
  127.         {
  128.             cout<<"cnt["<<i<<"] -> "<<cnt[i]<<endl;
  129.             mx = max(mx,cnt[i]);
  130.         }
  131.  
  132.         cout<<"ans -> "<<mx<<endl;
  133.         parent.clear();
  134.         vec.clear();
  135.  
  136.     }
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement