标签:
本题本是一道并查集的题,但出的数据比较小,因此用树的特点即一个点入度为0其他的全为1且边的条数等于顶点数减一即可下面是我的AC代码:
#include<iostream> #include <cstring> using namespace std; int main() { int n = 0, x, y, fff, i, j,sss[15], t = 0, k = 0, s ,ss, f, ff = 0, a[10005], b[10005], r[10005]; while (cin >> x >> y) { if (x <0 || y <0) break; ff ++; if (x == 0 && y == 0 && t == 0 && k == 0)//空数也是数 易忽略 { cout << "Case " << ++n << " is a tree." << endl; t = 0; k = 0; ff = 0; continue; } a[t++] = y; //保存入度的点 b[k++] = x; b[k++] = y; // 保存所有的点 if (x == 0 && y == 0 && (t != 0 && k != 0)) { ss = 0; memset(sss,0,sizeof(sss)); for (i = 0 ; i < k - 1; i ++) sss[b[i]] ++; for(i = 0; i <= 15; i ++)//找到顶点数 if(sss[i] > 0) ss ++; f = 1; s = 0; for (i = 0; i < t - 1; i ++) //判断是否入度是否有大于2的和找到入度为一的个数 { for (j = i + 1; j < t; j ++) { if (a[i] == a[j]) { f = 0; break; } } s++; } if (f == 1 && ss - s == 2 && ss - ff == 1&&ff - s == 1)//根据定义判断是否是树 cout << "Case " << ++n << " is a tree." << endl; else cout << "Case " << ++n << " is not a tree." << endl; t = 0; k = 0; ff = 0; } } return 0; }
2016HUAS暑假集训训练2 A - Is It A Tree?
标签:
原文地址:http://www.cnblogs.com/LIUWEI123/p/5697161.html