并查集。需要考虑入度。
1 #include2 #include 3 4 #define MAXNUM 10005 5 6 int bin[MAXNUM]; 7 int degree[MAXNUM]; 8 int nums[MAXNUM]; 9 10 int find(int x) {11 int r = x;12 13 while (bin[r] != r)14 r = bin[r];15 16 return r;17 }18 19 int main() {20 int x, y, fx, fy, n, case_n = 0;21 int i, flg;22 23 while (1) {24 scanf("%d %d", &x, &y);25 if (x<0 && y<0)26 break;27 memset(degree, 0, sizeof(degree));28 n = 0;29 ++case_n;30 if (x==0 && y==0) {31 printf("Case %d is a tree.\n", case_n);32 continue;33 }34 for (i=0; i 1) {70 flg = 0;71 break;72 }73 }74 if (flg)75 printf("Case %d is a tree.\n", case_n);76 else77 printf("Case %d is not a tree.\n", case_n);78 }79 80 return 0;81 }