HDU-1325&&POJ-1308 Is It A Tree?(基础并查集)

Is It A Tree?

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

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.

题意:和1271很像,但需要注意几个细节

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<string.h>
 4 #include<stdio.h>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<climits>
 8 #include<math.h>
 9 #include<vector>
10 #include<cstdio>
11 using namespace std;
12 #define maxn 100005
13 int  visit[maxn];
14 int father[maxn];
15 int cic;
16 int ding;
17 int edge;
18 void init()
19 {
20     for(int i=1;i<=maxn;i++)
21         visit[i]=0,father[i]=i;
22     ding=0;edge=0;
23 }
24 int find(int x)
25 {
26     if(father[x]==x)return x;
27     else find(father[x]);
28 }
29
30 void man(int a,int b)
31 {
32     int x,y;
33     x=find(a);
34     y=find(b);
35     if(x!=y)
36         father[x]=y;
37 }
38 int main()
39 {
40     int a,b,t=1,p;
41     while(1)
42     {
43         init();
44         p=0;
45         cin>>a>>b;
46         if(a==0&&b==0)
47             {cout<<"Case "<<t<<" is a tree."<<endl;continue;}
48         if(a<0&&b<0)break;
49         man(a,b);
50         visit[a]=1;
51         visit[b]=2;
52         edge++;
53         while(1)
54         {
55             cin>>a>>b;
56             if(a==0&&b==0)break;
57             visit[a]=1;
58             if(visit[b]==2)p=1;
59             else visit[b]=2;
60             edge++;
61             man(a,b);
62         }
63         for(int i=1;i<=maxn;i++)
64             if(visit[i]!=0)ding++;
65         if(ding-1==edge&&!p)cout<<"Case "<<t<<" is a tree."<<endl;
66         else cout<<"Case "<<t<<" is not a tree."<<endl;
67         t++;
68     }
69     return 0;
70 }
时间: 2024-10-08 20:09:52

HDU-1325&&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? (并查集)

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

HDU 1829 &amp;&amp; POJ 2492 A Bug&#39;s Life(种类并查集)

题目地址:HDU 1829     POJ 2492 这个题可以用两种方法做,第一眼看完题是觉得用dfs染色判断二分图.然后又写的刚学的种类并查集.原来并查集可以这样用,真是神奇.. dfs染色代码: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #incl

【转】POJ 2492 A Bug&#39;s Life:基础并查集进阶

思路参考这里(较详细) 一开始总是WA调了一晚上原来···Init初始化写在scanf上面了···哎╮(╯▽╰)╭anyway!对并查集的理解更深了一步! #include<cstdio> #include<cstring> using namespace std; #define Size 2000 struct node { int Pre; int Relation;// 与父节点的关系 0同性 1异性 }Bug[Size+1]; int N, M; bool found;

hdoj 1325 Is It A Tree? 【并查集】

做了一上午,终于ac了 wa了一次主要是忘了还有环!!! 主要是运用并查集知识,又复习了一次!! 思路:输入之后找能不能成环,成环就不是,其次还要判断是不是有两个父节点,如果有两个父节点也不是,之后就找相关的祖先就好了: 还要注意:如果只有一个节点,也是树,如果有两个或多个根节点也不是树:如果没有根节点也不是 链接http://acm.hdu.edu.cn/showproblem.php?pid=1325 代码 #include<stdio.h> int fat[1000]; int fath

hdu 1829 A Bug&#39;s Life (基础并查集)

题目: 链接:点击打开链接 题意: 给定虫子的交配关系,确定实验是否支持教授的假设即没有同性恋或者不符合假设. 思路: 是一道基础的并查集题目.存在两个集合异性和同性,给出多组关系,看这两个集合有木有联系,即是否有同性恋. 定义一个数组sex[],sex[i]表示与编号i的性别相反的虫子编号.然后将和i虫子有联系的合并为同一个集合(认为是同性的).如果findset(u) == findset(v),出现了反常行为. 代码: #include <iostream> #include <c

Poj 1182种类(带权)并查集

题目链接 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44316 Accepted: 12934 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是

【HDU1325】Is It A Tree?(并查集基础题)

有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路也可用树的性质:结点数 = 边树 + 1 来取代) 1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 #include <cctype> 5 #include <cmath&

POJ 1611 The Suspects(特别无语的并查集)

很简单的一道题目,开始用的是并查集的分离集合森林做,不知道怎么的特别不稳定,太奇怪了,WA无数次,无奈之下改成一维数组了..sad AC #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <math.h> #define PI acos(-1,0) using namespa