欢迎参加——每周六晚的BestCoder(有米!)

Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17569    Accepted Submission(s): 3945

Problem 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.

题解:参考了大神的解法,好多地方写的很好,但是要判断入度,别问我为什么,我也不知道,但大神就是这样写的。。。

代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 const int MAXN=100010;
 4 int tree[MAXN],in[MAXN];//in 入度
 5 int find(int x){
 6     int r=x;
 7     while(r!=tree[r])
 8         r=tree[r];
 9 //    int i=x,j;
10 //    while(i!=r) j=tree[i], tree[i]=r,i=j;
11     tree[x] = r;//需要的才 压缩路径,会长讲的好棒;
12     return r;
13 }
14 int main(){
15     int x,y,f1,f2,flot=0,tot=0,circle;
16     while(1){
17     tot++;
18     memset(tree,0,sizeof(tree));
19     memset(in,0,sizeof(in));
20     while(scanf("%d%d",&x,&y),x||y){
21     if(x<0&&y<0)break;
22     if(!tree[x])
23         tree[x]=x;//
24     if(!tree[y])
25         tree[y]=y;//这里很好,需要了再让tree【y】=y;
26         in[y]++;//大神的入度,不理解;;;
27         f1=find(x);//**刚开始写错了写成tree了,找了半天错误
28         f2=find(y);//**
29         if(f1!=f2)
30             tree[f1]=f2;
31         else flot=1;
32     }
33     if(x<0&&y<0)break;
34     circle=0;
35     for(int i=1;i<MAXN;++i){
36         if(tree[i]==i)circle++;
37         if(circle>1){
38             flot=1;break;
39         }
40         if(in[i]>1){
41             flot=1;break;
42         }
43     }//printf("circle=%d\n",circle);
44     if(flot)printf("Case %d is not a tree.\n",tot);
45     else printf("Case %d is a tree.\n",tot);
46     flot=0;
47     }
48     return 0;
49 }

Is It A Tree?(并查集)相关推荐

  1. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  2. HDU 1325 Is It A Tree? 并查集

    点击打开链接 Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. HDU 1325POJ 1308 Is it A tree ? [并查集+树判定]

    题目链接 poj hdu Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  4. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  5. 【POJ】1308 Is It A Tree?((并查集 + set)or (map))

    http://poj.org/problem?id=1308 这个题数组开到200就可以了,但题目中貌似没有说呢? 读入每一对顶点,看看他们是否在同一个集合中,如果是的话,肯定成环,不是一棵树. 用s ...

  6. Tree Restoration Gym - 101755F (并查集)

    There is a tree of n vertices. For each vertex a list of all its successors is known (not only direc ...

  7. 2017乌鲁木齐ICPC: I. A Possible Tree(带权并查集)

    I. A Possible Tree Alice knows that Bob has a secret tree (in terms of graph theory) with n nodes wi ...

  8. 2017 西安网络赛A Tree(树上静态查询,带权并查集,矩阵乘法压位,好题)

    题目链接 题意: 给出 \(n(n \leq 3000)\) 个结点的一棵树,树上每个结点有一个 \(64 \times 64\) 的 \(0,1\)矩阵,每个结点上的矩阵是根据输入的 \(seed\ ...

  9. hdu-----(4514)湫湫系列故事——设计风景线(树形DP+并查集)

    湫湫系列故事--设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  10. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

最新文章

  1. 德鲁克管理思想:管理的7大理论、43条原则,每一条都是精华
  2. 取代Python多进程!伯克利开源分布式框架Ray
  3. Linux服务器通过rz/sz轻松上传下载文件
  4. Pavel and Triangles(贪心)
  5. T-SQL:流程控制 4,Case 语句
  6. 阿里云ECS服务器多种实例规格如何选择
  7. 数据结构与算法--贪婪算法2
  8. i9 9900k mysql_i9-9900K和9900KS有什么区别?i9-9900KS和i9-9900K区别对比评测
  9. 华为云PB级数据库GaussDB(for Redis)揭秘第十期:GaussDB(for Redis)迁移系列(上)
  10. windows下CCS安装教程
  11. 服务器的ftp数据库信息,服务器ftp及数据库账号
  12. 大数据入门的知识体系,大数据学习路线
  13. Unity3d十二 3d主要引擎名称Ogre Unreal Unity Gamebryo Bigworld
  14. 添加proc文件,控制sctp的debug输出
  15. java蘑菇岛种子_我的世界:以2002年2月12日为种子代码,居然是蘑菇岛神级种子!...
  16. 数据库的隔离级别以及锁的关系的思考
  17. 橘子学ES19之词项搜索全文检索
  18. 2019-3-13-win10-uwp-使用-ScaleTransform-放大某个元素
  19. 华为p40pro android11,90Hz的华为P40Pro用了半年?最流畅的安卓旗舰?
  20. 成都五月花计算机专业学校单招,成都五月花高级技工学校这所单招学校如何

热门文章

  1. Python爬虫Scrapy入门
  2. LaTeX技巧:算法标题 Algorithm如何重命名
  3. Linux设置node的process.env.NODE_ENV
  4. c++之static的一些用法
  5. EditPlus安装及远程连接Linux
  6. 别再写 main 方法测试了,太 Low,这才是专业 Java 测试方法
  7. LeakCanary的原理,你知道么?
  8. java流家族_1.1.2 完整的流家族
  9. 中国高性能计算服务器排名,2007中国高性能计算100强出炉
  10. java空指针找不到,跪空指针异常,所有的传入参数都判断了非空,实在找不到哪里没有赋值了...