Is It A Tree? hdu1325

来源:互联网 发布:linux查找log日志命令 编辑:程序博客网 时间:2024/05/01 11:35

http://acm.hdu.edu.cn/showproblem.php?pid=1325

/*
题目的要求就是判断是否是一棵数,一棵树除了根结点,每个结点的入度都为1,直接用入度来判断即可。
一棵树是一个经典的数据结构,结点为空(不含有结点)或者如何下列性质的一个或由有向边连接的多个结点的集合。
一个没有边指向的结点,被称为根结点。只包含一个根节点。
除了根结点,有并且只有一条边指向每个结点。
从根节点到每个结点的有向边的序列是唯一的。
*/
#include<iostream>//2284661 2010-04-02 21:19:55 Accepted 1325 15MS 320K 871 B C++ 悔惜晟
#include<cstdio>
#include<string>
using namespace std;
const int MAX = 10000;
int visit[MAX];
int dgree[MAX];

int main()
{
 int t, x, y, count, max, i;
 t = 1;
 max = 0;
 while(scanf("%d %d", &x, &y) != EOF)
 {
  if(x < 0 && y < 0) continue;
  else if(x == 0 && y == 0)
  {
   count = 0;
   for( i = 0; i <= max; i++)
     if(visit[i])
    {
     if(dgree[i] == 0)
      count++;
     if(dgree[i] > 1)
     {
      count = 3;
      break;
     }
    }
     if(max == 0 || count == 1)
      printf("Case %d is a tree./n", t++);
     else
      printf("Case %d is not a tree./n", t++);
     memset(visit, 0, sizeof(visit));
     memset(dgree, 0, sizeof(dgree));
     max = 0;
  }
  else
  {
   visit[x] = 1;
   visit[y] = 1;
   if(max < x)
    max = x;
   if(max < y)
    max = y;
   dgree[y]++;
  }
 }
}

原创粉丝点击