标签:
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu
Description
Input
Output
Sample Input
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
Sample Output
Case 1 is a tree. Case 2 is a tree. Case 3 is not a tree。
问题分析:题目要求判断是否为树,先理清是树的条件,树是一种无向简单连通图
1.无回路和环 也就是说(1,2)(2,1)或者(1,1)或者(1,5)(1,5)这些情况都是不行的。
2.要点与点之间连通,而且注意空树(即无点无边的树)也是树。
1 #include "cstdio" 2 struct node 3 { 4 int i; 5 int j; 6 }; 7 node a[1000],b[1000]; 8 int number,time=0; 9 int nbegin() 10 { 11 int i=0; 12 time++; 13 while (scanf ("%d%d",&a[i].i,&a[i].j)&& (a[i].i != -1 || a[i].j != -1)) 14 { 15 number = i; 16 if (a[i].i || a[i].j) 17 i++; 18 else 19 return 1; 20 } 21 return 0; 22 } 23 void tree() 24 { 25 int now,next,q; 26 node y; 27 if (a[number].i == 0 && a[number].j == 0 && number == 0) 28 { 29 printf ("Case %d is a tree.\n",time); 30 return; 31 } 32 for (int i=0;i<number;i++) 33 { 34 for (int k=0;k<number;k++) 35 { 36 if (a[i].j == a[k].j && i != k) 37 { 38 printf("Case %d is not a tree.\n",time); 39 return; 40 } 41 if (a[i].i == a[k].j && a[i].j == a[k].i) 42 { 43 printf("Case %d is not a tree.\n",time); 44 return; 45 } 46 } 47 } 48 for (int i=0;i<number;i++) 49 { 50 now=0; 51 next=0; 52 q=0; 53 for (int t=0;t<number;t++) 54 if (a[t].i == a[i].i ) 55 { 56 b[next++] = a[t]; 57 q++; 58 } 59 while (next != now) 60 { 61 y = b[now]; 62 now++; 63 for (int j=0;j<number;j++) 64 if (y.j == a[j].i && y.i != a[j].j) 65 { 66 b[next++] = a[j]; 67 q++; 68 } 69 } 70 if (q == number) 71 { 72 printf ("Case %d is a tree.\n",time); 73 return; 74 } 75 } 76 printf("Case %d is not a tree.\n",time); 77 } 78 int main() 79 { 80 while(nbegin()) 81 tree(); 82 return 0; 83 }
暑假集训(2)第一弹 -----Is It A Tree?(Poj308)
标签:
原文地址:http://www.cnblogs.com/huas-zlw/p/5696945.html