Is It A Tree?
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 31092   Accepted: 10549

Description

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. 

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. 题意很简单,输入一组数据表示存在父子节点关系的点的编号,判断是否为树 方法一:运用并查集,将有连接关系的点放入同一集合内,判断是否出现环和多个根。如果没有则为树。具体见代码:
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
struct edge
{
int s,t;
}E[];
int par[],Rank[];
int k=,n=,ne=,num=;
bool used[];//used用于记录1到出现的最大数字之间的数字是否使用过
void init(){
memset(Rank,,sizeof(Rank));
for(int i=;i<=;i++)
par[i]=i;
} int Find(int i){
if(par[i]==i)return i;
else return Find(par[i]);
}
bool same(int x,int y){
return Find(x)==Find(y);
} void unite(int x,int y){
if(same(x,y))return;
else{
x=Find(x);
y=Find(y);
if(Rank[x]<Rank[y])
par[x]=y;
else{
par[y]=x;
if(Rank[x]==Rank[y]) Rank[x]++;
}
}
}
void tree(){
init();
bool flag=true;
int now=-;
for(int i=;i<=n;i++){
if(!same(E[i].s,E[i].t)){//判断是否首尾相连成环
unite(E[i].s,E[i].t);
}
else{
flag=false;
break;
}
} for(int i=;i<=ne;i++){
if(used[i]==true&&par[i]==i){//判断根的数目,如果多于1则不是树
num++;
if(num==){
flag=false;
break;
}
}
}
if(flag)printf("Case %d is a tree.\n",++k);
else printf("Case %d is not a tree.\n",++k);
} int main(){
int x,y;
while(scanf("%d%d",&x,&y)){
if(x==-&&y==-) break;
else if(x!=&&y!=){
++n;
E[n].s=y;
E[n].t=x;
ne=max(x,max(ne,y));
used[x]=true;//因为题目没有给出数目采用这种方法判断需要的循环次数
used[y]=true;
}
else{
tree();
memset(used,,sizeof(used));
init();
n=;
ne=;
num=;
}
}
return ;
}
方法二:利用树的性质:(1)点数=边数+1,这个性质可以去掉多根的情况。(2)一个点最多有一个父节点,这个性质可以去掉成环的情况,具体代码如下:
 #include<cstdio>
#include<cstring> using namespace std; int x,y,a[]={},m,n,flag;
int main()
{
int k=;
while(scanf("%d%d",&x,&y))
{
if(x==-&&y==-) break;
m++;//输入一组边数+1
if(a[y]==){flag=;}//如果y已经作为过子节点,再一次做子节点,说明出现两个父节点(也可能是重复的边,也不是树),不是树
if(!a[x]&&x){a[x]=;n++;}//1为父节点,2为子节点标记各个点,点数+1
if(!a[y]&&y){a[y]=;n++;}
if(x==&&y==)
{
if(!flag&&((n==m)||(n==&&m==))) printf("Case %d is a tree.\n",k++);//由于0 0也算入边内,因此m为边数+1,m==n;需特判空树
else printf("Case %d is not a tree.\n",k++);
m=,n=,flag=;
memset(a,,sizeof(a));
}
}
return ;
}
方法二由@刘靖尧 金鱼同学提供												

POJ 1308 Is It A Tree?--题解报告的更多相关文章

  1. POJ 1308 Is It A Tree? 解题报告

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32052   Accepted: 10876 D ...

  2. HDU 1325,POJ 1308 Is It A Tree

    HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...

  3. POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫

    POJ题目网址:http://poj.org/problem?id=1308 HDU题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1272 并查集的运用 ...

  4. POJ 1308 Is It A Tree? (并查集)

    Is It A Tree? 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/M Description A tree is a w ...

  5. hdu 1325 && poj 1308 Is It A Tree?(并查集)

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...

  6. POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18778   Accepted: 6395 De ...

  7. HDU ACM 1325 / POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. POJ 1308 Is It A Tree? (并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24237   Accepted: 8311 De ...

  9. 并查集判树 poj 1308

    例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题 ...

随机推荐

  1. FTP服务器中vsftpd主配置文件解析

    /etc/vsftpd/vsftpd.conf#################匿名权限控制############### anonymous_enable=YES #是否启用匿名用户no_anon_ ...

  2. 【转】mysql中文乱码的一点理解

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B. 如果你也遇到了这个问题,咱 ...

  3. fastcgi_param 详解

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径 fastcgi_param QUERY_STRI ...

  4. shell sed

      匹配 sed -n '/pattern/p' file_name |sed -n 7,12p #pattern是你要查的内容 #file_name是你要查的文件 以上实现:打印出匹配结果中的7-1 ...

  5. keystore 介绍

    Keytool 是一个有效的安全钥匙和证书的管理工具. Java 中的 keytool.exe (位于 JDK\Bin 目录下)可以用来创建数字证书,所有的数字证书是以一条一条(采用别名区别)的形式存 ...

  6. poj 3641 ——2016——3——15

    传送门:http://poj.org/problem?id=3461 题目大意:给你两个字符串p和s,求出p在s中出现的次数. 题解:这一眼看过去就知道是KMP,作为模板来写是最好不过了.... 这道 ...

  7. 10.TCP连接的建立与终止

      1.建立连接协议 (1)请求端发送一个SYN段指明客户打算连接的服务器的端口,移机初始序号ISN.这个SYN段为报文段1. (2)服务器发回包含服务器的初始序号的SYN报文段作为应答.同时,将确认 ...

  8. 一点养老APP模式定制系统平台源码

    一点养老APP模式定制系统开发:136.1013.1824电/微:搭建一点养老APP模式定制系统平台.专注于为企业和商家客户提供基于腾讯微信公众平台系统程序和APP等开发服务,中国养老金融50人论坛2 ...

  9. Unity跨平台原理

    An ahead-of-time (AOT) compiler is a compiler that implements ahead-of-time compilation. This refers ...

  10. Failed to register Grid Infrastructure type ora.mdns.type

    安装11g的集群软件的时候,在最后运行root.sh脚本时候,没有执行成功,最后提示如下错误: [root@r2 ~]# /u01/app/11.2.0/grid_1/root.sh Performi ...