Is It A Tree?
2015-11-11 21:16
274 查看
Is It A Tree?
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 杭电1325和本题一样,和hdu 1272小希的迷宫差不多 但是不知道为什么,一直wa;解决之后再改进。哪位大神知道该怎么做,可以评论告诉一下,嘿嘿^_^。(ˇˍˇ) 谢谢O(∩_∩)O #include<iostream> #include<stdio.h> #define Size 100010 using namespace std; int father[Size],a[Size]; int find(int x){ while(x!=father[x]) x=father[x]; return x; } void merage(int x,int y){ x=find(x); y=find(y); if(x!=y) father[x]=y; } int main() { int i,j,x,y,max,s=0; while(~scanf("%d %d",&x,&y)) { if(x==-1&&y==-1)break; for(i=0;i<Size;i++) { father[i]=i; a[i]=0; } s++; j=0;max=0; while(x&&y){ if(find(x)==find(y)) { j=1; while(x&&y)scanf("%d %d",&x,&y); break; } if(max<x)max=x; if(max<y)max=y; a[x]=1;a[y]=1; merage(x,y); scanf("%d %d",&x,&y); } if(j==1)printf("Case %d is not a tree.\n",s); else { j=0; for(i=0;i<=max;i++) if(a[i]&&father[i]==i) j++; if(j>1)printf("Case %d is not a tree.\n",s); else printf("Case %d is a tree.\n",s); } } return 0; } |
相关文章推荐
- 表空间基于时间点的恢复(TSPITR)
- 使用xcode7进行http请求
- [LeetCode125]Valid Palindrome
- Git+jekyll搭建个人博客
- 从吉他到做事态度
- 团队项目中出现的问题集锦
- leetcode 18 4Sum
- Python函数
- FriendlyARM Tiny210开发板硬件基础 第一篇--点亮LED
- requests的content与text导致lxml的解析问题
- Android: 水平滑动线性布局的实现
- 算法导论 8-3思考题 之变长数据项的排序
- 牛顿迭代法 Newton-Raphson Method
- 学习Opencv的小心得
- codeforces Inna, Dima and Song
- ubuntu安装php常见错误集锦
- 日期计算
- 牛顿迭代法 Newton-Raphson Method
- gazebo+ros搭建单目仿真环境:贴有二维码的天花板+kobuki+camera(1)
- request.getSession()和request.getSession(true/false)的理解