HDU 1325 Is It A Tree?(并查集/有向树的判断)
2016-01-19 22:05
369 查看
B - Is It A Tree?
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
HDU 1325
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
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.
分析:
W1:如果是有向树,只需要边数n-1,每个点入度<2。
W2: 并查集(其实感觉很鸡肋啊根本就是强行并查集嘛=-=):对于a b 我相当于把b的祖先改为a的祖先(a<-b)。
判断依据是:
如果b的祖先不是它自己,而它又要指向c的祖先 那么一定不是一棵树。
如果有多个人的祖先是他自己那么有多个连通块,这不是一棵树。
坑点:
输入为负结束,而不是-1。
自环的情况。只要一出现自环那么一定不是一棵树直接判断就好。
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
HDU 1325
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 lineCase 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.
分析:
W1:如果是有向树,只需要边数n-1,每个点入度<2。
W2: 并查集(其实感觉很鸡肋啊根本就是强行并查集嘛=-=):对于a b 我相当于把b的祖先改为a的祖先(a<-b)。
判断依据是:
如果b的祖先不是它自己,而它又要指向c的祖先 那么一定不是一棵树。
如果有多个人的祖先是他自己那么有多个连通块,这不是一棵树。
坑点:
输入为负结束,而不是-1。
自环的情况。只要一出现自环那么一定不是一棵树直接判断就好。
// Created by ZYD in 2015. // Copyright (c) 2015 ZYD. All rights reserved. // #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> #include <climits> #include <string> #include <vector> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> using namespace std; #define maxn 1e6+5 #define ll long long #define mk make_pair #define pb push_back #define mem(array) memset(array,0,sizeof(array)) typedef pair<int,int> P; int fa[1000005]; bool used[1000005]; int find(int x){ int r=x; while(fa[r]!=r) r=fa[r]; int i=x,t; while(fa[i]!=r){ t=fa[i]; fa[i]=r; i=t; } return r; } int main() { int t1,t2,q,z; freopen("in.txt","r",stdin); int t=0; while(~scanf("%d %d",&q,&z)){ t++; if(q<0 && z<0) break; if(q==0 && z==0) {printf("Case %d is not a tree.\n",t);continue;} for(int i=1;i<maxn;i++) used[i]=false; for(int i=1;i<maxn;i++) fa[i]=i; int flag=0; t1=find(q); t2=find(z);used[q]=true;used[z]=true; if(t1!=t2) fa[t2]=t1; while(~scanf("%d %d",&q,&z) && q && z){ if(flag==1) continue; used[q]=true;used[z]=true; t1=find(q); t2=find(z); if(q==z) flag=1; if(t2!=z) { flag=1; continue; } else{ fa[t2]=t1; } } // cout<<fa[6]<<" "<<fa[1]; int tot=0; for(int i=1;i<maxn;i++) if(used[i] && fa[i]==i) tot++; if(tot>1) flag=1; if(flag==0) printf("Case %d is a tree.\n",t); else printf("Case %d is not a tree.\n",t); } return 0; }
相关文章推荐
- @ResponseBody
- 清华EMBA课程系列思考之四 -- 组织行为学
- 万年历
- Java拼接字符串时,去掉最后一个多余的逗号
- Oracle package包头和package body包体例子
- css设置table里面单元格强制换行和强制不换行
- mongoose 增删改查 基础篇
- Java拼接字符串时,去掉最后一个多余的逗号
- Java拼接字符串时,去掉最后一个多余的逗号
- eclipse 快捷键
- PHP+Apache配置备忘
- 同步synchronized用法
- 使用scipy.spatial.Delaunay 三角网的构建
- java数组内存分析
- python核心编程-使用线程锁
- mysql在centos下的安装
- CRLF injection 简单总结
- Python 5.4 定制类
- 机器学习导论
- VirtualBox安装时发生严重错误的解决办法