Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10221 Accepted Submission(s): 2348
Problem 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.
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 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<iostream> #include<vector> #include<queue> #include<string> using namespace std; struct NODE //邻接表 { int R;//节点前驱 int data;//节点信息 vector<int>edg;//邻接点 }node[100005]; bool visited[100005]; //顶点标志 //确定顶点为位置 int find(NODE node[],int data,int &n) { int i; for(i=0;i<n;i++) if(node[i].data==data) return i; n++; return n-1; } //确定根节点 int findroot(NODE node[],int n) { int i,k,count=0; for(i=0;i<n;i++) if(node[i].R==0) { k=i; count++; } if(count!=1) return -1; else return k; } //广度优先搜索 bool BFS(NODE node[],int v,int n) { for(int i=0;i<n;i++) visited[i]=false; queue<int>que; que.push(v); while(!que.empty()) { v=que.front(); visited[v]=true; que.pop(); for(unsigned i=0;i<node[v].edg.size();i++) if(visited[node[v].edg[i]]==true) return false; else { que.push(node[v].edg[i]); visited[node[v].edg[i]]=true; } } for(int i=0;i<n;i++) if(visited[i]==false) return false; return true; } int main() { int a,b,t=1; while(cin>>a>>b&&a>=0||b>=0) { int i,n=0; int p,q; bool flag,count=true; if(a||b) //a,b均为0为空树 { node[0].data=a; node[0].edg.push_back(1); node[1].data=b; n=2; if(a==b) //a,b相等不是树 count=false; while(cin>>a>>b&&a||b) //邻接表的建立 { p=find(node,a,n); q=find(node,b,n); node[p].data=a; node[q].data=b; node[p].edg.push_back(q); if(a==b) count=false; } for(i=0;i<n;i++) node[i].R=0; for(i=0;i<n;i++) //统计结点前驱 for(unsigned j=0;j<node[i].edg.size();j++) node[node[i].edg[j]].R++; int root=findroot(node,n); if(root!=-1) { flag=BFS(node,root,n); if(flag&&count) cout<<"Case "<<t<<" is a tree."<<endl; else cout<<"Case "<<t<<" is not a tree."<<endl; } else cout<<"Case "<<t<<" is not a tree."<<endl; } else cout<<"Case "<<t<<" is a tree."<<endl; t++; for(i=0;i<n;i++) //清空邻接表信息 { node[i].edg.clear(); node[i].data=0; } } return 0; }