Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author : unlucky_13
- Problem_link :
- Category :
- Algorithm_Used : Got run time error then used map stl and got AC just check the dis-joints sets
- */
- #include<cstdio>
- #include<sstream>
- #include<cstdlib>
- #include<cctype>
- #include<cmath>
- #include<algorithm>
- #include<set>
- #include<queue>
- #include<stack>
- #include<list>
- #include<iostream>
- #include<fstream>
- #include<numeric>
- #include<string>
- #include<vector>
- #include<cstring>
- #include<map>
- #include<iterator>
- #define i64 long long int
- using namespace std ;
- //const long long int inf = 2147483647 ;
- //const int minx=;
- const int maxn = 1000+10 ;
- int par[maxn] ;
- int find(int u){
- if(par[u]==u) return u ;
- else return par[u] = find(par[u]) ;
- }
- int main() {
- freopen("C:\\Users\\Mazhar\\Desktop\\in.txt", "r", stdin);
- int x,y,tree,ct=1,roots,siz,X,Y;
- map<int,int>G ;
- map<int,int>::iterator it;
- while(1){
- scanf("%d %d",&x,&y) ;
- if(x==-1 && y==-1) break ;
- G.clear() ;
- roots = siz = 0 ;
- for(int i=0;i<maxn;i++) par[i]=i ;
- tree=true ;
- while(1){
- if(x==0 && y==0) break ;
- it = G.find(x) ;
- if(it==G.end()){
- G[x]=siz ;
- X = siz ;
- siz++ ;
- }
- else X = it->second ;
- it = G.find(y) ;
- if(it==G.end()){
- G[y]=siz ;
- Y = siz ;
- siz++ ;
- }
- else Y = it->second ;
- int U = find(X) ;
- int V = find(Y) ;
- if(U!=V){
- par[V]=U ;
- }
- else tree = 0 ;
- scanf("%d %d",&x,&y) ;
- }
- //cout<<siz<<endl ;
- for(int i=0;i<siz;i++){
- if(find(i)==i) roots++ ;
- }
- //cout<<roots<<endl ;
- if(roots>1) tree = 0 ;
- if(tree) printf("Case %d is a tree.\n",ct++) ;
- else printf("Case %d is not a tree.\n",ct++) ;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment