hdu--1856 More is better

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements. 
Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang‘s selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

Input

The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

Output

The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

Sample Input

4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8

Sample Output

4
2

Hint

A and B are friends(direct or indirect), B and C are friends(direct or indirect),
then A and C are also friends(indirect).

 In the first sample {1,2,5,6} is the result.
In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
 题意:王老师要找一些男生帮助他完成一项工程。要求最后挑选出的男生之间都是朋友关系,可以说直接的,也可以是间接地。问最多可以挑选出几个  男生(最少挑一个)。 代码:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int num[1000000],par[1000000];
 6 void init()
 7 {
 8     for(int i=1;i<1000000;i++)
 9      { par[i]=i;
10        num[i]=1;
11      }
12 }
13 int find(int x)
14 {
15     if(par[x]!=x)
16       return par[x]=find(par[x]);
17     return x;
18
19 }
20 void unite(int a,int b)
21 {
22     int fa=find(a);
23     int fb=find(b);
24     if(fa!=fb)
25     {par[fa]=fb;
26     num[fb]+=num[fa];  //这个地方注意,第一次num【fb】写成了b,
27
28     }
29 }
30 int main()
31 {
32     int a,b,n;
33     while(scanf("%d",&n)!=EOF)
34     {if(n==0)
35       {printf("1\n");
36        continue;
37
38       }
39      init();
40      int max=0;
41      while(n--)
42       {
43        scanf("%d %d",&a,&b);
44        if(a>max) max=a;
45        if(b>max) max=b;
46        unite(a,b);
47       }
48       int m=0;
49      for(int i=1;i<=max;i++)
50        {if(num[i]>m)
51           m=num[i];
52
53        }
54      printf("%d\n",m);
55
56
57
58     }
59     return 0;
60 }
时间: 2024-10-07 10:13:21

hdu--1856 More is better的相关文章

hdu 1856 并查集(路径压缩)

hdu 1856 More is better 简单的并查集,用到了路径压缩 题意:找出节点数最多的连通分支,并输出该节点数. 思路:统计每个连通分支的节点数(利用并查集构建图时进行路径压缩,用一个与根节点下标对应的sum数组记录节点数),比较后得出最大值. 1 #include<cstdio> 2 #include<cstring> 3 4 int set[10000000 + 50]; 5 int sum[10000000 + 50]; 6 bool visit[1000000

【并查集】hdu 1856 More is better

[并查集]hdu 1856 More is better n个元素的集合应用问题--并查集. 题目大意 n个元素,告诉它们的两两连接关系,求单个连通分支所含元素个数的最大值 说一下思路 经典的并查集求的是所含连通分支的个数,这道题求的是并查集森林最大点集数(元素数),思路是维护一个cnt[ ]数组,初始化元素所在集合为自身cnt[ ] = 1;当要合并时,父节点累加孩子节点,cnt[ y ] += cnt [ x ],最后输出最大值. ★这题没有离散化就过了,so big the memory

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 1856 More is better(并查集+离散化)

题目地址:HDU 1856 水题.由于标号范围太大,而数据数只有10w,所以要先进行离散化.然后就是裸的并查集了. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queu

hdu 1856 More is better(并查集)

题目链接: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): 18985    Accepted Submission(s): 6990 Problem Description Mr Wang wants some

HDU 1856 More is better(并查集)

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): 18523    Accepted Submission(s): 6814 Problem Description Mr Wang wants some boys

HDU(1856),裸的带权并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856 题意:朋友圈问题,A和B是朋友,B和C是朋友则A和C也是朋友,依次类推,题目的意思就是求最大的朋友圈,即求最大集合中元素的个数.裸的并查集加个秩数组就行了. #include <stdio.h> int father[100050]; int rank[100050]; int Find_Set (int x) { if(x!=father[x]) father[x] = Find_Set(

HDU 1856 More is better (并查集)

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

HDU 1856

并查集基本应用. 题意:有编号为1到10^7的男生在一个房间,现在给出n组数据,每组数据包括两个编号,且这两个编号的人是朋友,如果A和B是朋友,B和C是朋友,那么A,B,C三人都是朋友,哪一个朋友圈中人数最多,输出这个朋友圈的人数 思路:因为编号是1到10^7所以,每次都要求出最大的编号数,如果每次都循环到10^7的话是会超时的,最后利用路径压缩,记录和的话是用的寻找根节点然后++ (提醒一下:如果数据很多的话用scanf输入比较好,cin的话会很慢) 1 #include<cstdio> 2

More is better HDU - 1856 ?

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements. Mr Wang selected a room big enough to hold the boys. The boy who are not b