Is It A Tree?
Time Limit: 1000MS | Memory Limit: 10000K |
---|
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.
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.
一棵树有且只有一个入度为0的结点,并且所有结点的入度都不大于1.
注:
1、“0 0”空树也是树 ( ╯□╰ )
2、“1 1 0 0”不是 不能指向自己 ( ╯□╰ )
3、“1 2 1 2 0 0”不是 不能重复 ( ╯□╰ )
4、“1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0”不是 属于root==0的情况
5、“1 2 2 1 0 0”不是 ( ╯□╰ )
POJ 往HDU上提交TLE/(ㄒoㄒ)/~~
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stack>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
int visit_time[1002];//记录被指向次数(入度)
int main()
{
int a, b, Case=1;
while(scanf("%d%d", &a, &b), a!=-1&&b!=-1)
{
set<int>s;//储存出现过的节点
memset(visit_time, 0, sizeof(visit_time));
while(a&&b)
{
s.insert(a), s.insert(b);
visit_time[b]++;//b节点被指向一次,+1
scanf("%d%d", &a, &b);
}
int flag=1, root=0;//入度为0的结点的个数
for(set<int>::iterator i=s.begin(); i!=s.end(); i++)//遍历s
{
if(visit_time[*i]==0)//如果是根节点,root++
{
root++;
if(root>1)//根节点只能出现一次
{
flag=0;
break;
}
}
if(visit_time[*i]>1)//每个节点最多被指向一次
{
flag=0;
break;
}
}
if(root==0&&!s.empty())//如果没有入度为0的节点且不是空树
flag=0;
if(flag)
printf("Case %d is a tree.\n", Case++);
else
printf("Case %d is not a tree.\n", Case++);
}
return 0;
}
HOJ
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
struct node
{
int st, en;
} node[1005];
int indegree[1005];
bool is(queue<int> q,int a)
{
while(!q.empty())
{
int temp = q.front();
q.pop();
if(temp == a)
return true;
}
return false;
}
int main()
{
int a, b, casee=1;
while(scanf("%d%d", &a, &b))
{
if(a<0&&b<0)
break;
queue<int>q;
memset(indegree, 0, sizeof(indegree));
int counter=0;
while(a!=0&&b!=0)
{
if(is(q, a)==0)
q.push(a);
if(is(q, b)==0)
q.push(b);
node[counter].st=a;
node[counter].en=b;
indegree[node[counter].en]++;
counter++;
scanf("%d%d", &a, &b);
}
int flag=1, root=0;
while(!q.empty())
{
int temp=q.front();
q.pop();
if(indegree[temp]==0)
root++;
if(indegree[temp]>1)
{
flag=0;
break;
}
}
if(root>1)
flag=0;
if(flag)
printf("Case %d is a tree.\n", casee++);
else
printf("Case %d is not a tree.\n", casee++);
}
}