POJ 1308 Is It A Tree?--题解报告

时间:2023-03-08 20:34:11
POJ 1308 Is It A Tree?--题解报告
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. 
POJ 1308 Is It A Tree?--题解报告
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 ;
}
方法二由@刘靖尧 金鱼同学提供