注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

告别迷茫

梦想与现实的差距,就是我们生活的意义。因为我们有差距,我们才会一直积累,在努力。

 
 
 

日志

 
 

HDU 1325 Is It A Tree?  

2014-04-09 14:54:19|  分类: 并查集 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
看一幅图是不是一颗树。。。

1. 肯定满足只有一个树根

2. 一个点不会被指向两次或两次以上。

3. 不能出现环

A tree is a well-known data structure that is either
empty (null, void, nothing) or is a set of one or more nodes connected by
directed edges between nodes satisfying the following properties.
There is
exactly one node, called the root, to which no directed edges point.


Every node except the root has exactly one edge pointing to it.


There is a unique sequence of directed edges from the root to each node.


For example, consider the illustrations below, in which nodes are
represented by circles and edges are represented by lines with arrowheads. The
first two of these are trees, but the last is not.


HDU  1325   Is It A Tree? - 983433479 - jet skyHDU  1325   Is It A Tree? - 983433479 - jet skyHDU  1325   Is It A Tree? - 983433479 - jet sky


In this problem you will be given
several descriptions of collections of nodes connected by directed edges. For
each of these you are to determine if the collection satisfies the definition of
a tree or not.


 


Input

The input will consist of a sequence of descriptions
(test cases) followed by a pair of negative integers. Each test case will
consist of a sequence of edge descriptions followed by a pair of zeroes Each
edge description will consist of a pair of integers; the first integer
identifies the node from which the edge begins, and the second integer
identifies the node to which the edge is directed. Node numbers will always be
greater than zero.

 


Output

For each test case display the line ``Case k is a
tree." or the line ``Case k is not a tree.", where k corresponds to the test
case number (they are sequentially numbered starting with 1).

 


Sample Input

6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1

 


Sample Output

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

 


Source

North Central North America 1997

 


#include<stdio.h>
#include<algorithm>
using namespace std;
int father[1110];
int yes[1100];
int root[1100];
int flag;
int find(int x)
{
int r=x;
while(r!=father[r])
r=father[r];
return r;
}
void build()
{
flag=0;
int i;
for(i=1;i<=1001;i++)
{
yes[i]=0;//遍历是否!
father[i]=i;
root[i]=0;//查找根基!
}
}
bool judge()
{
int i,t;
for(i=1;i<=1001;i++)
{
if(yes[i])
root[find(i)]++;
}
t=0;
for(i=1;i<=1001;i++)
{
if(root[i]>1) t++;/*如果是一棵树的话,就只会有一个根,就只会出现一个比1大的值*/
}
if(t>1) return false;
return true;
}
int main()
{
int a,b,count=0,jishu=0;
build();
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a<0) break;
if(a==0&&b==0&&jishu==0)/*坑人的数据*/
{
count++;
printf("Case %d is a tree.\n",count);
jishu==0;
continue;
}
if(a==0&&b==0&&jishu!=0)
{
count++;
if(flag) printf("Case %d is not a tree.\n",count);
else
{
if(judge()) printf("Case %d is a tree.\n",count);
else printf("Case %d is not a tree.\n",count);
}
build();
jishu=0;
continue;
}
jishu++;
yes[a]=yes[b]=1;
if(find(a)==find(b)) flag=1; /*判断是否有环*/
else
{
father[a]=b;
}
}
return 0;
}



  评论这张
 
阅读(0)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017