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

注意以下4种情况:

0 0              可以
1 1 0 0       不可以
1 2 1 2 0 0 不可以
1 2 2 1 0 0 不可以

 1 #include <cstdio>
 2 #include <cstring>
 3
 4 const int MAXN=100000+5;
 5
 6 int a,b,mycase=1;
 7 bool ok=true;
 8 int father[MAXN];//记录父节点
 9 int temp[MAXN];//判断是否为森林(用并查集)
10 bool vis[MAXN];
11
12 void init(){
13     ok=true;
14     for(int i=0;i<MAXN;i++){
15         father[i]=i;
16         temp[i]=i;
17         vis[i]=0;
18     }
19 }
20 int main()
21 {
22     init();
23     while( scanf("%d %d",&a,&b)!=EOF && a>-1 && b>-1 ){
24
25         if(ok==false && a && b)continue;
26
27         if(a==0 && b==0){
28             int root=0;//根节点的个数
29             for(int i=0;i<MAXN;i++){
30                 if(vis[i] && temp[i]==i)
31                     root++;
32             }
33             if(root>1)ok=false;
34
35             if(ok)printf("Case %d is a tree.\n",mycase++ );
36             else printf("Case %d is not a tree.\n",mycase++ );
37
38             init();
39             continue;
40         }
41
42         if(a!=b && father[b]==b && father[a]!=b){
43             father[b]=a;
44             temp[b]=a;
45             vis[a]=vis[b]=true;
46         }
47         else ok=false;
48
49     }
50
51     return 0;
52 }
时间: 2024-08-26 02:09:40

【并查集】hdu 1325 Is It A Tree?的相关文章

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

HDU 1325 Is It A Tree? (POJ 1308)

并查集问题... 这题以前做过-- 以前做过-- 做过-- 过-- 不过重做时候被吭得异常之爽-- 在判断 vis[i]的时候.我记得标准C++是非0 即为真. 而我用C++ 提交的时候 if(vis[i]) 去直接给我WA了. 用G++ 就AC了...然后改成if(vis[i]==1) 交C++ 就AC了. 特瞄的我每次初始化都把 vis[i] 都赋值为 0 了..都能出这种错? 求路过大神明示我的错误. 题意是判断是否是一棵树. 不能存在森林,用并查集合并,每个点的入度不能超过1. 比如 1

并查集 -- HDU 1232 UVALA 3644

并查集: 1 int pa[maxn],Rank[maxn]; 2 ///初始化 x 集合 3 void make_set(int x) 4 { 5 pa[x]=x; 6 Rank[x]=0; 7 } 8 ///递归查找 x 所在的集合 9 int find_set(int x) 10 { 11 if(pa[x]!=x) pa[x]=find_set(pa[x]); 12 return pa[x]; 13 } 14 ///更新根节点,如果不更新可能会暴栈 15 void mix(int x,in

集合问题 离线+并查集 HDU 3938

题目大意:给你n个点,m条边,q个询问,每条边有一个val,每次询问也询问一个val,定义:这样条件的两个点(u,v),使得u->v的的价值就是所有的通路中的的最长的边最短.问满足这样的点对有几个. 思路:我们先将询问和边全部都按照val排序,然后我们知道,并查集是可以用来划分集合的,所以我们就用并查集来维护每一个集合就行了. //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespac

并查集 hdu 1856

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 16863    Accepted Submission(s): 6205 Problem Description Mr Wang wants some boys to help him with a project. Because the projec

(并查集) hdu 2473

Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6389    Accepted Submission(s): 2025 Problem Description Recognizing junk mails is a tough task. The method used here consists o

分组并查集 hdu 1829

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

hdu 1325 Is It A Tree?(并查集)

题意:给出点.边,判断是不是一棵树 思路:问题是如何判断是不是树? 我总结了一下,但不官方,正确性待验证. 1.入度<=1(根节点为0,其他为1) 2.不能有环 3.只有一个根节点 #include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define MAXN 50000 int fa[MAXN]; int a[MAXN]; int in[MAXN]; int se

(枚举+并查集) hdu 1598

find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4096    Accepted Submission(s): 1768 Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级