POJ - 1308 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.

做法:并查集判断有没有环然后判断有没有森林有没有入度大于1的点即可小心自己指向自己的点小心 只有一个 0 0代码:
 1 #include<iostream>
 2 using namespace std;
 3 #include<cstring>
 4 #include<cstdio>
 5 const int maxn = 110000;
 6 struct DSU{
 7     int f[maxn];
 8     int v[maxn];
 9     void init(){
10         for(int i=0;i<maxn;i++)
11             f[i] = i,v[i] = 0;
12     }
13     int getfa(int x){
14         return f[x]==x?x:f[x] = getfa(f[x]);
15     }
16     int connect(int x,int y){
17         v[x] = v[y] = 1;
18         int fx,fy;
19         fx = getfa(x);
20         fy = getfa(y);
21         if(fx==fy)
22             return 0;
23         f[fx] = fy;
24         getfa(x);
25         return 1;
26     }
27     int getnum(){
28         int ans = 0;
29         for(int i=0;i<maxn;i++){
30             if(v[i]&&f[i]==i)
31                 ans+=1;
32         }
33         return ans==1;
34     }
35 };
36 int rd[maxn];
37 DSU pt;
38 int main(){
39     int x,y,num = 0;
40     while(scanf("%d%d",&x,&y)!=EOF&&x>=0&&y>=0){
41         pt.init();
42         memset(rd,0,sizeof(rd));
43         pt.connect(x,y);
44         rd[y]+=1;
45         int ok = 1;
46         if(x==y&&(x!=0||y!=0))
47             ok = 0;
48         if(x!=0&&y!=0)
49         while(scanf("%d%d",&x,&y)!=EOF&&x!=0&&y!=0){
50             if(!ok)
51                 continue;
52             if(x==y)
53                 ok = 0;
54             rd[y]+=1;
55             if(ok==1)
56             ok = rd[y]==1;
57             if(ok==1)
58                 ok = pt.connect(x,y);
59         }
60         if(ok==1)
61             ok = pt.getnum();
62         if(ok)
63             printf("Case %d is a tree.\n",++num);
64         else
65             printf("Case %d is not a tree.\n",++num);
66     }
67     return 0;
68 }

原文地址:https://www.cnblogs.com/xfww/p/8819514.html

时间: 2024-10-06 22:29:06

POJ - 1308 Is It A Tree?【并查集】的相关文章

HDU 1325 POJ 1308 Is It A Tree? (并查集)

这道题就是裸并查集,关键在于对不是树几种的判断 1. 空树是树 2. 森林不是树 3. 无环 或者从入度来看:1,无环:2,除了根,所有的入度为1,根入度为0:3,这个结构只有一个根,不然是森林了. 这道题本来暑假做的POJ 1308 但是HDU没有过.在于空树没有考虑. 用并查集判断有多少个森林注意编号是随机的,不是次序.... /* input: 0 0 1 1 0 0 1 2 1 2 0 0 1 2 2 3 4 5 0 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

[POJ 1308]Is It A Tree?(并查集判断图是否为一棵有根树)

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, t

POJ 1308 Is It A Tree? (并查集)

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24237   Accepted: 8311 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 edge

POJ 1984 Navigation Nightmare (数据结构-并查集)

Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4072   Accepted: 1615 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series o

POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y均能C通信,则x和y可以通信.现在给出若干个操作, O p 代表修复编号为p的电脑 S p q代表询问p和q是不是能通信. 思路: 并查集即可.. 如果修复了一台电脑,则把与它相连距离不超过d的且修复了的放在一个集合里面. #include<cstdio> #include<cstring&

POJ 2492 A Bug&#39;s Life (并查集)

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 30130   Accepted: 9869 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

poj 3415 后缀数组分组+排序+并查集

Source Code Problem: 3415   User: wangyucheng Memory: 16492K   Time: 704MS Language: C++   Result: Accepted Source Code #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define N 510000

HDU 1325 Is It A Tree? 并查集

判断是否为树 森林不是树 空树也是树 成环不是树 数据: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0 1 2 2 3 4 5 0 0 2 5 0 0 ans: no no yes #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype

poj 2912 Rochambeau(带权并查集 + 暴力)

题目:poj 2912 Rochambeau(带权并查集 + 暴力) 题目大意:题目给出三个团队和一个裁判,这三个团队和裁判一起玩剪刀石头布,然后规定每个团队必须出一样的,只有裁判可以任意出.然后给出关系,x > y 代表 x 赢y , x < y代表 y 赢 x , 相等则出的一样.问这样的关系可以推出裁判是哪个吗?可以需要说明从第一条到第几条推出来的,不可以也要说明是不可能出现这样的关系,还是裁判不唯一. 解题思路:这题重点是裁判在里面会扰乱关系,并且n * m 才 100000,完全可以

POJ 2524 Ubiquitous Religions Union Find 并查集

本题是标准的并查集了,最后利用这些集求有多少独立集. 所以这里也写个标准程序过了. 最后查找独立集合: 看有多少个节点的父母节点是自己的,那么就是独立集合了.自己做自己的父母当然最独立的了,没有任何依赖,呵呵. #include <stdio.h> const int MAX_N = 50001; //const int MAX_M = MAX_N/2 * (MAX_N-1) + 1; int N, M; struct SubSet { int p, r; }; SubSet sub[MAX_