HDU 1325 Is It A Tree?
来源:互联网 发布:乐视无线显示网络异常 编辑:程序博客网 时间:2024/05/22 15:42
HDU 1325 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.OutputFor 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.
题意:给定一些点,判断最后是不是构成一棵树。
题解:看上去这道题和HDU1272一样,只不过1272可以水过。。这道题的坑点有点多,一是要判断入度不能>=2,二是判断不能有多个根点,最后要处理奇怪的输入输出,PS:本人因为输出没有.而wa掉多次。。
AC代码:
#include<iostream>#include<cstdio>using namespace std;const int maxn = 1000005;int vis[maxn];int n,m;int fa[maxn];int rude[maxn];int fi(int x){ return fa[x]==x?x:fa[x]=fi(fa[x]);}void unionset(int x,int y){ int p1= fi(x); int p2= fi(y); if(p1==p2)return; fa[p1]=p2;}void intc(){ for(int i=1;i<=100000;i++) { fa[i]=i; vis[i]=0; rude[i]=0; }}int kk(){ int kk=0; for(int i=1;i<=100000;i++) { if(vis[i]&&fa[i]==i)kk++; if(vis[i]&&rude[i]>=2)kk=2; } return kk;}int main(){ int ans = 1; int t=1; while(cin>>n>>m) { if(ans==1) { ans=2; intc(); } if(n<=-1&&m<=-1)break; if(n==0&&m==0) { if(kk()>=2)ans=0; if(ans)printf("Case %d is a tree.\n",t++); if(ans==0) { printf("Case %d is not a tree.\n",t++); ans=2; } intc(); continue; } vis[n]=1; vis[m]=1; if(fi(n)==fi(m))ans=0; else unionset(n,m); rude[m]++; } return 0;}
阅读全文
0 0
- HDU 1325 Is It a Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 is it a tree?
- HDU-1325-Is It A Tree?
- HDU 1325 Is It A Tree? ---树
- HDU 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is it a tree
- hdu 1325 &&poj1308 Is It A Tree?
- HDU 1325 Is It A Tree?
- HDU 1325 Is It A Tree?
- HDU 1325 Is It A Tree?
- hdu 1325 is it a tree?
- 网络层次与主要设备的对应关系
- Ctex中插入Visio图的步骤
- 洛谷P2878 保护花朵
- atoi函数的C实现
- SpringMVC原理及配置详解
- HDU 1325 Is It A Tree?
- Python发送Http请求时,中文乱码问题的解决方法
- UVa 113|Power of Cryptography|牛顿迭代法|pow函数的应用
- 20171005memo
- 1024. 科学计数法 (20)
- 网络编程_高级函数
- JS动画函数的封装
- Bzoj 1647: Fliptile 翻格子游戏 状态压缩
- 捕捉Win32窗口操作