判断是否为树
森林不是树
空树也是树
成环不是树
数据:
1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0
1 2 2 3 4 5 0 0
2 5 0 0
ans:
no
no
yes
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; typedef long long LL; const int maxn =10000+5; const int maxe = 15000+5; const int INF = 460002326; const int mod = 1000000009;//q[i]表示点i 对应的点 rank[i]表示入度 int fa[maxn],q[maxn],rank[maxn],taj,flag;//taj表示当前点的编号个数 void init() { flag=0; taj=1; for(int i=0; i
=0) { init();//初始化 if(a==0&&b==0)// 0 0 也算树 { printf("Case %d is a tree.\n",++cas); continue; } a=q[a],b=q[b]; merge(a,b); while(scanf("%d%d",&a,&b)) { if(a+b==0) break; dian(a,b); a=q[a],b=q[b]; merge(a,b); } a=0; for(int i=1; i
1) flag=1; } printf("Case %d is ",++cas); if(a==1&&flag==0&&taj>2) puts("a tree."); else puts("not a tree."); } return 0; }