题目链接:http://poj.org/problem?id=1308
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.
题目翻译:
树是一个众所周知的数据结构,它要么为空(空,空,无),要么是一组由满足以下属性的节点之间的定向边缘连接的一个或多个节点。
正好有一个节点,称为根,没有定向边指向该
节点。
除根外,每个节点都有一个指向它的边。
从根到每个节点都有一个唯一的定向边序列。
例如,请考虑下面的插图,其中节点由圆表示,边缘用箭头表示。前两个是树,但最后一个不是。
在此问题中,您将得到由定向边连接的节点集合的多个描述。对于其中每一个,您都要确定集合是否满足树的定义。
输入
输入将由一系列描述(测试用例)后跟一对负整数组成。每个测试用例将包含一系列边缘描述,后跟一对零,每个边描述将包含一对整数;第一个整数标识边开始开始的节点,第二个整数标识边缘指向的节点。节点编号将始终大于零。
输出
对于每个测试用例,将显示"Case k 是树"或行"Case k 不是树",其中 k 对应于测试用例编号(它们按顺序编号以 1 开头)。
这个题算是并查集应用的一个经典例子。题意很好理解,判断有向图是不是一棵树。
容易知道:连通后有且仅有1个入度为0,其余入度为1,就是树了。
注意空树的情况!!!!
#include
#include
using namespace std;
const int maxn=1e6;//注意再大就会空间炸了
int p[maxn],in[maxn],ans[maxn];
//ans存储所有点,in统计各个点的入度,p为父节点数组
bool visited[maxn];//判断点是否被访问过
void init(){//初始化
for(int i=0;i<=maxn;++i){
p[i]=i;
visited[i]=0;
in[i]=0;
}
}
int find(int x){
return p[x]==x?x:p[x]=find(p[x]);
}
void Union(int x,int y){
int xx=find(x);
int yy=find(y);
if(xx!=yy) p[xx]=yy;
}
int main(int argc, char** argv) {
int a,b;
int kcase=0;
int tot=0;
int flag=1;
init();
while(~scanf("%d%d",&a,&b)){
if(a==-1&&b==-1) break;
int cnt=0;
if(a==0&&b==0){
for(int i=0;i=2){//找到入度点数大于1的点
flag=0;
break;
}
}
if(cnt!=1) flag=0;//入度为0的点数为1
if(tot==0) flag=1;//空树
if(flag)
printf("Case %d is a tree.\n",++kcase);
else
printf("Case %d is not a tree.\n",++kcase);
tot=0; //为下一组样例初始化
init();
flag=1;
}
else{
if(!visited[a]){
visited[a]=1;
ans[tot++]=a;
}
if(!visited[b]){
visited[b]=1;
ans[tot++]=b;
}
if(find(a)==find(b)) flag=0;//有环
else{
Union(a,b);
in[b]++;
}
}
}
return 0;
}