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.
题意:给出n条有向边,判断是不是一棵树。
思路:这道题的话,题目中所给的是有向树,并给出了性质:
1.只有一个节点,称为根节点,没有定向边指向它。
2.除了根节点外,每个节点都只有有一条指向它的边。
3.从树根到任一结点有一条有向通路。
抽象过来就是三个条件:
1.只有一个入度为0的点,作为根节点。
2.除根节点外,其他点的入度只能为1。
3.所有点都能连通,也就是所有点需要在一个集合中,使用并查集来划分集合。
4.空树也是树(0,0)。
pojAC代码:
#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <vector>
typedef long long ll;
const int maxx=100010;
const int inf=0x3f3f3f3f;
const int mod=10007;
using namespace std;
int pre[maxx],in[maxx],v[maxx];
int getf(int a)
{
if(a==pre[a])
return a;
int tmp=getf(pre[a]);
return tmp;
}
void mer(int a,int b)
{
int fa=getf(a);
int fb=getf(b);
if(fa!=fb)
pre[fb]=fa;
}
int main()
{
int a,b,k=0;
while(~scanf("%d%d",&a,&b) && (a!=-1 && b!=-1))
{
k++;
if(a==0 && b==0)
{
printf("Case %d is a tree.\n",k);
continue;
}
for(int i=1; i<maxx; i++)
{
pre[i]=i;
v[i]=0;
in[i]=0;
}
v[a]=1;
v[b]=1;
in[b]++;
mer(a,b);
while(~scanf("%d%d",&a,&b),a,b)
{
v[a]=1;
v[b]=1;
in[b]++;
mer(a,b);
}
int flag=1;
int ans=0,cnt=0;
for(int i=1; i<maxx; i++)
{
if(v[i] && !in[i])
{
ans++;
}
if(in[i]>=2)
{
flag=0;
}
if(v[i] && pre[i]==i)
{
cnt++;
}
}
if(ans!=1 || cnt>1)
flag=0;
if(flag)
printf("Case %d is a tree.\n",k);
else
printf("Case %d is not a tree.\n",k);
}
return 0;
}
但是这个代码在hdu上过不了,然后我看了看大佬的博客,发现上面这个代码还有所缺陷,还需要再加一个条件:保证子节点是根节点。
hdu和pojAC代码:
#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <vector>
typedef long long ll;
const int maxx=10010;
const int inf=0x3f3f3f3f;
const int mod=10007;
using namespace std;
int pre[maxx],v[maxx];
int flag,maxn;
void init()
{
for(int i=0; i<maxx; i++)
{
pre[i]=i;
v[i]=0;
}
maxn=0;
flag=1;
}
int getf(int a)
{
if(a==pre[a])
return a;
int tmp=getf(pre[a]);
return tmp;
}
void mer(int a,int b)
{
int fa=getf(a);
int fb=getf(b);
if(fa!=fb)
pre[fb]=fa;
if(b!=fb || fa==fb)//体现在这里,多加的条件
flag=0;
}
int main()
{
int a,b,k=0;
init();
while(~scanf("%d%d",&a,&b) && (a!=-1 && b!=-1))
{
if(a==0 && b==0)
{
int ans=0;
for(int i=1; i<=maxn; i++)
{
if(v[i] && pre[i]==i)
ans++;
}
if(ans>1)
flag=0;
if(flag)
printf("Case %d is a tree.\n",++k);
else
printf("Case %d is not a tree.\n",++k);
init();
continue;
}
maxn=max(max(a,b),maxn);
v[a]=1;
v[b]=1;
mer(a,b);
}
return 0;
}