nyoj 129树的判定

来源:互联网 发布:搜狐出品的网络剧 编辑:程序博客网 时间:2024/05/21 03:25

树的判定

描述

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.

输入
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.

The number of test cases will not more than 20,and the number of the node will not exceed 10000.
The inputs will be ended by a pair of -1.
输出
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).
样例输入
6 8  5 3  5 2  6 4 5 6  0 08 1  7 3  6 2  8 9  7 5 7 4  7 8  7 6  0 03 8  6 8  6 4 5 3  5 6  5 2  0 0-1 -1
样例输出
Case 1 is a tree.Case 2 is a tree.Case 3 is not a tree.
</pre>
<span style="font-size:18px;">/*<span style="color:#ff0000;">树的条件:只有一个根节点;没有环;节点的入度最大为1;空树也是树;</span>*///这道题错了好多次,想死的心都有了#include<stdio.h>#include<string.h>int pre[10010];int vis[10010];int indegree[10010];int find(int root){int son = root,tmp;while(root!=pre[root])root = pre[root];while(son!=root){tmp = pre[son];pre[son] = root;son = tmp;}return root;}int main(){//freopen("stdin.txt","r",stdin);int a,b,x,y,i,len=0;for(i=0;i<10010;i++)pre[i] = i;memset(indegree,0,sizeof(indegree));memset(vis,0,sizeof(vis));int q=0;bool flag = true;while(scanf("%d%d",&a,&b)&&a!=-1||b!=-1){if(a==0&&b==0){int cont = 0,tmp = 0;for(i=1;i<=len;i++){if(vis[i]&&pre[i]==i)tmp++;}//改了一下这里,tmp的取值 /*tmp可以取值0,1;当数据只有0,0的时候;tmp是为零的; */if(tmp>1)flag = false;if(flag)printf("Case %d is a tree.\n",++q);elseprintf("Case %d is not a tree.\n",++q);for(i=0;i<10010;i++)pre[i] = i;memset(vis,0,sizeof(vis));memset(indegree,0,sizeof(indegree));flag = true;}else{len = a > b ? (a>len? a:len):(b>len? b:len);vis[a]=vis[b]=1;indegree[b]++;if(indegree[b]>1)//如果是一棵树,那么节点的入度只能为0(根)或者1(其他点)flag = false;//所以,如果有一个节点的入度大于1,就可以判定这不是一棵树if(!flag)continue;/*对于输入的点可分以下情况,"个人感觉" 1.a,b都在已有的树中,那么树中会出现环2.a,b其中一个在,一个不在,这里又分,a==b; 那么,树的根节点会出现自环;a!=b; 这种情况可以; 3.a,b都不在已有的树中;这种情况也可以; 下面的x!=y处理合法的情况;else处理不合法的情况; */x = find(a);y = find(b);if(x!=y)pre[y] = x;elseflag = false;}}return 0;}</span>


0 0
原创粉丝点击