标签:
6 8 5 3 5 2 6 4 5 6 0 0
8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0
3 8 6 8 6 4 5 3 5 6 5 2 0 0
-1 -1
Case 1 is a tree. Case 2 is a tree. Case 3 is not a tree.
解法一:(只是在小希的迷宫上添加了入度条件即可)
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 #define Max 100100 5 int ID[Max]; 6 int InD[Max]; 7 int Out_Num; 8 int Pio[Max]; 9 int SIGN; 10 int P_Num; 11 void Cread(int N) 12 { 13 for(int i=0;i<=N;i++){ID[i]=i;Pio[i]=1;InD[i]=1;} 14 } 15 int Find(int x) 16 { 17 int TMD=x,TMP; 18 while(TMD!=ID[TMD])TMD=ID[TMD]; 19 while(x!=TMD) 20 { 21 TMP=ID[x]; 22 ID[x]=TMD; 23 x=TMP; 24 } 25 return x; 26 } 27 void Update(int a,int b) 28 { 29 if(Pio[a]){P_Num++;Pio[a]=0;} 30 if(Pio[b]){P_Num++;Pio[b]=0;} 31 if(InD[b]){Out_Num++;InD[b]=0;} 32 int A=Find(a); 33 int B=Find(b); 34 if(A!=B) 35 { 36 ID[A]=B; 37 SIGN++; 38 } 39 else SIGN=Max; 40 } 41 int main() 42 { 43 int T,N,M,i,j,a,b,t=1; 44 Cread(Max);SIGN=0;P_Num=0;Out_Num=0; 45 while(scanf("%d%d",&a,&b)!=EOF) 46 { 47 if(a<0&&b<0)break; 48 if(a==0&&b==0) 49 { 50 // printf("%d %d %d\n",SIGN,Out_Num,P_Num); 51 if((SIGN==P_Num-1||P_Num==0)&&Out_Num==SIGN) 52 printf("Case %d is a tree.\n",t++); 53 else 54 printf("Case %d is not a tree.\n",t++); 55 Cread(Max);SIGN=0;P_Num=0;Out_Num=0; 56 continue; 57 } 58 Update(a,b); 59 } 60 return 0; 61 }
正解:(只需要根据树的定义,判断入度即可。。。)
1 #include<stdio.h> 2 #include<algorithm> 3 #include<string.h> 4 using namespace std; 5 int d[100005]; 6 bool bo[100005]; 7 int bianshu=0; 8 void init(){ 9 memset(d,0,sizeof(d)); 10 memset(bo,0,sizeof(bo)); 11 bianshu=0; 12 } 13 int main(){ 14 int x,y,cas=1; 15 while(~scanf("%d%d",&x,&y)) 16 { 17 if(x==-1&&y==-1){ 18 break; 19 } 20 if(x==0&&y==0){ 21 int bobo=0; 22 int i; 23 for(i=1;i<=100000;i++){ 24 if(bo[i]&&d[i]==0){ 25 if(bobo==0) bobo=1; 26 else break; 27 }else 28 if(bo[i]&&d[i]!=1){ 29 break; 30 } 31 } 32 if(i<=100000) printf("Case %d is not a tree.\n",cas++); 33 else printf("Case %d is a tree.\n",cas++); 34 init(); 35 continue; 36 } 37 d[y]++; 38 bo[x]=true; 39 bo[y]=true; 40 bianshu++; 41 } 42 return 0; 43 }
标签:
原文地址:http://www.cnblogs.com/LWF5201314614/p/4693318.html