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. 最简单的ajax调用webservice

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestHelloWorld ...

  2. HDU 5621 KK's Point

    N个点中任意选取四个点,就能产生一个圆内的交点,所以圆内总共有C(N,4)个交点,圆上有N个,相加就可以了. 注意:组合数运算的时候会爆longlong,中间先除一下就可以了. #include &l ...

  3. 流水线技术原理和Verilog HDL实现(转)

    源:流水线技术原理和Verilog HDL实现 所谓流水线处理,如同生产装配线一样,将操作执行工作量分成若干个时间上均衡的操作段,从流水线的起点连续地输入,流水线的各操作段以重叠方式执行.这使得操作执 ...

  4. 再次写了第一个servlet

    费时2小时,熟悉tomcat和编写了第一个servlet

  5. 使用spol导出exce

    sqlplus 能生产xls的excel文件 connect / as sysdba; SET NEWPAGE 0 SET SPACE 0 SET LINESIZE 80 SET PAGESIZE 0 ...

  6. 初学杂文 String类

    String: 两个字符床  String stra 和String strb stra = "hello " ; strb = "hello " 在对象池中开 ...

  7. python3中bytes与string的互相转换

    首先来设置一个原始的字符串, Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32 Ty ...

  8. django的HTTPREQUEST对象

    Django使用request和response对象 当请求一张页面时,Django把请求的metadata数据包装成一个HttpRequest对象,然后Django加载合适的view方法,把这个Ht ...

  9. jmeter接口测试实践

    一.什么是接口测试? 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻 ...

  10. QT第一天学习

    sudo apt-get install libqt4-dev回顾: 面向对象方法: 封装.继承.多态 封装:类 数据和操作 实现了信息隐藏 public: 类的内部 类的外部 private: pr ...