unlucky_13

uva_615_is it tree

Jun 14th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. /*
  2.  Author                             :     unlucky_13
  3.  Problem_link                       :
  4.  Category                           :
  5.  Algorithm_Used                     : Got run time error then used map stl and got AC just check the dis-joints sets
  6.  */
  7.  
  8. #include<cstdio>
  9. #include<sstream>
  10. #include<cstdlib>
  11. #include<cctype>
  12. #include<cmath>
  13. #include<algorithm>
  14. #include<set>
  15. #include<queue>
  16. #include<stack>
  17. #include<list>
  18. #include<iostream>
  19. #include<fstream>
  20. #include<numeric>
  21. #include<string>
  22. #include<vector>
  23. #include<cstring>
  24. #include<map>
  25. #include<iterator>
  26. #define i64 long long int
  27. using namespace std ;
  28. //const long long int inf = 2147483647 ;
  29. //const int minx=;
  30. const int maxn = 1000+10 ;
  31. int par[maxn] ;
  32. int find(int u){
  33.     if(par[u]==u) return u ;
  34.     else return par[u] = find(par[u]) ;
  35. }
  36.  
  37. int main() {
  38.  
  39.     freopen("C:\\Users\\Mazhar\\Desktop\\in.txt", "r", stdin);
  40.     int x,y,tree,ct=1,roots,siz,X,Y;
  41.     map<int,int>G ;
  42.     map<int,int>::iterator it;
  43.     while(1){
  44.  
  45.         scanf("%d %d",&x,&y) ;
  46.         if(x==-1 && y==-1) break ;
  47.         G.clear() ;
  48.         roots = siz = 0 ;
  49.         for(int i=0;i<maxn;i++) par[i]=i ;
  50.         tree=true ;
  51.         while(1){
  52.  
  53.             if(x==0 && y==0) break ;
  54.             it = G.find(x) ;
  55.             if(it==G.end()){
  56.                 G[x]=siz ;
  57.                 X = siz ;
  58.                 siz++ ;
  59.             }
  60.             else X = it->second ;
  61.             it = G.find(y) ;
  62.             if(it==G.end()){
  63.                 G[y]=siz ;
  64.                 Y = siz ;
  65.                 siz++ ;
  66.             }
  67.             else Y = it->second ;
  68.             int U = find(X) ;
  69.             int V = find(Y) ;
  70.             if(U!=V){
  71.                 par[V]=U ;
  72.             }
  73.             else tree = 0 ;
  74.  
  75.             scanf("%d %d",&x,&y) ;
  76.         }
  77.  
  78.         //cout<<siz<<endl ;
  79.         for(int i=0;i<siz;i++){
  80.             if(find(i)==i) roots++ ;
  81.         }
  82.         //cout<<roots<<endl ;
  83.  
  84.         if(roots>1) tree = 0 ;
  85.         if(tree) printf("Case %d is a tree.\n",ct++) ;
  86.         else printf("Case %d is not a tree.\n",ct++) ;
  87.  
  88.     }
  89.  
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment