首页 > 代码库 > HDU 1325 Is It A Tree?
HDU 1325 Is It A Tree?
E - Is It A Tree?
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uDescription
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.
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 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 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 <iostream>#define maxn 100010using namespace std;struct TREE{ int mask; int root,rudu;//根、入度}f[maxn];void init ()//初始化{ int i; for(i=0;i<maxn;i++) { f[i].mask=0; f[i].rudu=0; f[i].root=i; }}int find(int x)//查找{ if(f[x].root!=x) f[x].root=find(f[x].root); return f[x].root;}void joint(int a,int b)//合并{ int fa,fb; fa=find(a); fb=find(b); if(fa!=fb) f[fb].root=fa;}int main(){ int N=1; int n,m; init(); bool flag =true; while(cin>>n>>m) { if(n<0&&m<0) break; if(n==0&&m==0) { cout<<"Case "<<N<<" "; int j; int num=0;//树根个数 for(j=1;j<maxn;j++) { if(f[j].mask!=0&&find(j)==j)//森林 num++; if(f[j].rudu>1)//入度大于1 { flag=false; break; } } if(num>1)//根数大于1,是森林 flag=false; if(flag==true) cout<<"is a tree."<<endl; else cout<<"is not a tree."<<endl; N++; init();//初始化 flag =true; continue; } if((m!=n&&find(n)==find(m))||m==n) flag = false; else { f[n].mask=1; f[m].mask=1; f[m].rudu++;//记录入度 joint(n,m); } } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。