题目链接:http://poj.org/problem?id=1308
题意:给你一个有向图,问你这是不是一棵树
解析:输入有点坑,他可能没有一条边,其他的你只需要判断一下边数是不是等于点数-1,每个结点的前驱结点是不是只有一个
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e3+100;
int fa[maxn],vis[maxn];
int main(void)
{
int x,y,case_t = 1;
while(~scanf("%d %d",&x,&y))
{
if(x==-1 || y==-1)
break;
int flag = 1;
memset(vis,0,sizeof(vis));
memset(fa,0,sizeof(fa));
int c1 = 0,c2 = 0;
if(x!=0)
{
do
{
if(!fa[y])
fa[y] = x;
else
flag = 0;
if(!vis[x])
{
vis[x] = 1;
c1++;
}
if(!vis[y])
{
vis[y] = 1;
c1++;
}
c2++;
}while(~scanf("%d %d",&x,&y)&&x&&y);
if(c1-1 != c2)
flag = 0;
}
printf("Case %d ",case_t++);
if(flag)
puts("is a tree.");
else
puts("is not a tree.");
}
return 0;
}