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 1 0 0
1 2 2 1 0 0
-1 -1

output:

Case 1 is a tree.

Case 2 is not a tree.

Case 3 is not a tree.

Case 4 is not a tree.

Case 5 is not a tree.

Case 6 is not a tree.

*/

#include<math.h>
#include<stdio.h>
#include<string.h>
#define inf 0x3ffffff
#define maxn 10+100000
#define max(a,b) ((a)>(b)?(a):(b))
int N,M,K;
int parent[maxn];
int vis[maxn];
int find(int *parent,int k)
{
    if(parent[k]==-1)
        return k;
    parent[k]=find(parent,parent[k]);
    return parent[k];
}
int main()
{
    int i,j,again;
    int e,r,ans,ee,rr,cnt;
	int nn;
    memset(parent,-1,sizeof(parent));
    memset(vis,0,sizeof(vis));
    nn=ans=again=cnt=0;
    while(1)
    {
        if(again)
        {
            for(j=0,i=1;i<=nn;i++)
            {
                if(vis[i] && parent[i]==-1)
                    j++;
                if(j>1)
                {ans=1;break;}
            }
			if(nn==0)ans=0;
            if(ans)
                printf("Case %d is not a tree.\n",++cnt);

            else
                printf("Case %d is a tree.\n",++cnt);
            ans=again=0;
            memset(parent,-1,sizeof(parent));
            memset(vis,0,sizeof(vis));
        }
        scanf("%d%d",&e,&r);
		nn=max(nn,max(e,r));
        if(e<0 && r<0)
            break;
        if(e==0  && r==0 )
        {again=1;continue;}
		vis[r]=vis[e]=1;
        if(ans==1)
            continue;
        ee=find(parent,e);
        rr=find(parent,r);
        if(r!=rr || rr==ee)
            ans=1;
        parent[rr]=ee;    

    }
  return 0;
}
时间: 2024-12-10 21:07:18

HDU 1325 POJ 1308 Is It A Tree? (并查集)的相关文章

[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

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

HDU1325 Is It A Tree? 并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1325 这题与HDU1272 小希的迷宫 (并查集) 非常像,不过细细看,还是有一点区别的.就是这题的路径是单向的,每次只能由起点指向终点,在连接之前终点必须是根节点. 注意的问题: 1.不能成环,即每次输入的两个数的根节点不能相同: 2.最终根节点数目为一 3.注意当只输入"0 0" 时要输出"Case %d is a tree." 4.路径是单向的,即每次只能由起点指

hdu 1829 A Bug&#39;s Life(分组并查集(偏移量))

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9204    Accepted Submission(s): 2961 Problem Description Background Professor Hopper is researching the sexual behavior of a rare sp

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

HDU 3038 How Many Answers Are Wrong(种类并查集)

题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[x] y - x = s 可以推出b - a = rank[y] - rank[x] + s; 并查集 延迟更新什么的,都忘了啊. 还有这题,如果是x--的话,记得更新0的根. #include <cstring> #include <cstdio> #include <stri

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