Is It A Tree?(POJ_1308)
来源:互联网 发布:等高线地图软件 编辑:程序博客网 时间:2024/06/04 19:48
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.
Source
North Central North America 1997
代码
咱是用并查集写的,听说数据很水,是不是水过去的咱也不造,有哪位大神要是发现了错误请及时告诉咱^ ^
#include <iostream>#include <cstring>using namespace std;int bin[100000];int flag1[50000],flag2[50000];int find(int x){ return x==bin[x]?x:find(bin[x]);}void merge(int x,int y){ int fx=find(x),fy=find(y); if(fx!=fy) bin[fy]=fx;}int main(){ int a,b,mark,cnt; int c=0; while(cin>>a>>b,~a||~b) { mark=0; c++; if(!a&&!b) cout<<"Case "<<c<<" is a tree."<<endl; //注意空树的情况 else { if(a==b) //指向自己的情况 mark=1; int maxx=-1; memset(flag1,0,sizeof(flag1)); memset(flag2,0,sizeof(flag2)); flag1[a]=flag2[b]=1; maxx=max(max(a,maxx),b); for(int i=1; i<=10; i++) bin[i]=i; merge(a,b); while(cin>>a>>b,a||b) { if(bin[a]==b||bin[b]==a) //有环的情况 mark=1; merge(a,b); flag1[a]++; flag2[b]++; maxx=max(max(a,maxx),b); } if(mark==0) { cnt=0; mark=0; for(int i=1; i<=maxx; i++) { if(flag1[i]||flag2[i]) { if(bin[i]==i) { cnt++; } } if(flag2[i]>1) //树的入度不能大于2 { mark=1; break; } } } if(cnt==1&&mark==0) cout<<"Case "<<c<<" is a tree."<<endl; else cout<<"Case "<<c<<" is not a tree."<<endl; } } return 0;}
- Is It A Tree?(POJ_1308)
- Is It A Tree?
- Is it a tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- IS IT A tree
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- is it a tree
- Is It A Tree?
- Is It A Tree?
- android 收集已发布版本的错误信息(UncaughtExceptionHandler)
- KMP算法专题-1001
- Snail—UI学习之表视图TableView单行添加、删除和移动
- ios实现简单的计算器
- hdoj 2277 Change the ball【规律题】
- Is It A Tree?(POJ_1308)
- 数字图像学习0
- Android Native C Log
- 什么是存储过程?
- 黑马程序员—JAVA基础—io流
- 四川华迪信息技术有限公司 实训 月总结(2015.7)
- [BZOJ2120]数颜色
- Xcode提高编码效率小技巧(一)
- HDOJ 1551 Cable master(二分查找)