hdu 2545 并查集

题目描述:给定一个无向图,判断这个图是否满足任意两点之间有且只有一条通路。

思路:并查集,若a和b之间有一条边且处于不同的集合中,则将a和b所在集合合并;若a和b本就在同一集合中,则a到b一定有不止一条通路。另外还需要判断一下

是否所有的点最后都在一个集合中,即根的数量是否等于1.

注意:0 0的情况应该输出Yes

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5
 6 const int N = 100007;
 7 int f[N];
 8 bool mark[N];
 9
10 void init()
11 {
12     for ( int i = 1; i < N; i++ )
13     {
14         mark[i] = false;
15         f[i] = i;
16     }
17 }
18
19 int find_f( int x )
20 {
21     if ( f[x] != x ) f[x] = find_f(f[x]);
22     return f[x];
23 }
24
25 void union_set( int x, int y )
26 {
27     //f[x] = y居然会栈溢出
28     f[y] = x;
29 }
30
31 int main ()
32 {
33     int a, b;
34     while ( 1 )
35     {
36         scanf("%d%d", &a, &b);
37         if ( a == -1 && b == -1 ) break;
38         if ( a == 0 && b == 0 )
39         {
40             printf("Yes\n");
41             continue;
42         }
43         init();
44         mark[a] = mark[b] = true;
45         union_set( a, b );
46         bool flag = true;
47         while ( 1 )
48         {
49             scanf("%d%d", &a, &b);
50             if ( a == 0 && b == 0 ) break;
51             if ( !flag ) continue;
52             mark[a] = mark[b] = true;
53             a = find_f(a), b = find_f(b);
54             if ( a == b ) flag = false;
55             else union_set( a, b );
56         }
57         if ( !flag )
58         {
59             printf("No\n");
60             continue;
61         }
62         int cnt = 0;
63         for ( int i = 1; i < N; i++ )
64         {
65             if ( mark[i] && f[i] == i ) cnt++;
66         }
67         if ( cnt == 1 ) printf("Yes\n");
68         else printf("No\n");
69     }
70     return 0;
71 }
时间: 2024-11-23 11:03:54

hdu 2545 并查集的相关文章

HDU 1051 并查集+贪心

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11694    Accepted Submission(s): 4837 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU 1512 并查集+左偏树

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3105    Accepted Submission(s): 1330 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

hdu 1829 并查集(食物链的弱化版)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

hdu 1856 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 13672    Accepted Submission(s): 5008 Problem Description Mr Wang wants some boys

HDU 1181 并查集 or SPFA

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4733 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规

hdu 1213 并查集入门

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12538    Accepted Submission(s): 6145 Problem Description Today is Ignatius' b

HDU 3938 并查集

求小于L的路径点对数(路上的最大值),按权值排序,从小到大并查集建图,有点kruskal的意思. /** @Date : 2017-09-22 17:30:11 * @FileName: HDU 3938 并查集 离线.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h>

HDU 1829 并查集

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