Is it a Tree? 并查集 坑多
来源:互联网 发布:志鸿优化设计历史答案 编辑:程序博客网 时间:2024/06/10 21:25
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.
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.
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.
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).
6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 3 5 6 5 2 0 0-1 -1
Case 1 is a tree.Case 2 is a tree.Case 3 is not a tree.
这道题目一开始理解为就是是否成环,然而实际上...看了看讨论区这道题比较坑。
如果大家一直WA请测试以下用例吧.. 除了成环还有空树,森林,若简单地用点=边+1 第四个会WA等等..
0 0
1 1 0 0
1 2 1 2 0 0
1 2 2 3 3 1 4 4 1 0
1 2 2 3 3 1 0 0
只有0 0是tree 其他都不是。
AC代码:
#include <cstdio>using namespace std;int fa[100005];bool vis[100005];bool flag;void init(){ for(int i=1;i<=100000;i++) { fa[i]=i; vis[i]=0; }}int findd(int x){ if(fa[x]==x) return x; else return fa[x]=findd(fa[x]);}void unite(int x,int y){ vis[x]=1;vis[y]=1; int xx=findd(x), yy=findd(y); if(xx==yy) flag=1; else fa[xx]=yy;}int main(){ int i=1; int n,m; while(~scanf("%d%d",&n,&m)) { if(n<0&&m<0) break; if(!(n||m)) { printf("Case %d is a tree.\n",i++); } else { flag=0; init(); unite(n,m); while(scanf("%d%d",&n,&m)&&n&&m) unite(n,m); int num=0; for(int j=1;j<=100000;j++) if(vis[j]&&fa[j]==j) num++; if(num>1) flag=1; if(!flag) printf("Case %d is a tree.\n",i++); else printf("Case %d is not a tree.\n",i++); } } return 0;}
其实操作很简单,就是考察的比较细致..据说测试用例很水..
0 0
- 【并查集】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? 并查集 坑多
- 【并查集】hdu 1325 Is It A Tree? 或 poj 1308 Is It A Tree?
- HDU1325 Is It A Tree? + POJ1308 Is It A Tree?【并查集】
- POJ 1308-Is It A Tree 并查集
- HDU 1325 Is It A Tree?(并查集)
- Is It A Tree?(并查集)
- POJ1308--并查集--Is It A Tree?
- POJ 1308 Is It A Tree? (并查集)
- Is It A Tree?--POJ1308--并查集
- hdu 1325 Is It A Tree?(并查集)
- pku 1308 Is It A Tree?(并查集)
- 【图论05】并查集 1003 Is It A Tree?
- 获取其他进程的fs寄存器
- C++中最容易被人忘记的构造函数初始化列表
- 第6章 图的基本算法
- 你都付出了哪些努力
- HDU2026 首字母变大写
- Is it a Tree? 并查集 坑多
- POJ 3051 Satellite Photographs 可能会
- POJ1838 banana 并查集
- bzoj4730
- BZOJ2124 等差子序列
- 虚拟机设置固定IP地址,并且能访问外网
- BZOJ1128: [POI2008]Lam
- (拓扑排序)HDU 1285 确定比赛名次
- POJ 3055 Digital Friends 再想想