More is better(hdu 1856 计算并查集集合中元素个数最多的集合)

More is better 

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 21167    Accepted Submission(s): 7720

Problem Description

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 <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <set>
 6 #include <cmath>
 7 using namespace std;
 8 #define Max  10000000+5
 9 int per[Max];
10 int ans[Max];
11 int num[Max];
12 bool vis[Max];
13 int n;
14 void init()
15 {
16     for(int i=1;i<Max;i++)
17         per[i]=i;
18 }
19 int find(int a)
20 {
21     if(a==per[a])
22         return a;
23     return per[a]=find(per[a]);
24 }
25 int unite(int a,int b)
26 {
27     a=find(a);
28     b=find(b);
29     if(a!=b)
30         per[a]=b;
31     return 0;
32 }
33 int main()
34 {
35     int t,a,b;
36     freopen("in.txt","r",stdin);
37     while(scanf("%d",&n)!=EOF)
38     {
39         init();
40         int Maxn=1,p=0;
41         memset(vis,0,sizeof(vis));
42         fill(ans,ans+Max,0);
43         for(int i=0;i<n;i++)
44         {
45             scanf("%d%d",&a,&b);
46             if(vis[a]==0)
47             {
48                 vis[a]=1;
49                 num[p++]=a;
50             }
51             if(vis[b]==0)
52             {
53                 vis[b]=1;
54                 num[p++]=b;
55             }
56             unite(a,b);
57         }
58         for(int i=0;i<p;i++)
59         {
60             t=find(num[i]);
61             ans[t]++;
62             //cout<<t<<endl;
63             Maxn=max(Maxn,ans[t]);
64         }
65         printf("%d\n",Maxn);
66     }
67 }
时间: 2024-08-01 10:46:33

More is better(hdu 1856 计算并查集集合中元素个数最多的集合)的相关文章

HDU1865--More is better(统计并查集的秩(元素个数))

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

Hdu 2473(并查集删除操作) Junk-Mail Filter

有木有很吊 加强 加强版   啊  ,看了都不敢做了   ,后来先做了食物链这个我还是看过的,但还是A不掉,没明白神魔意思 ,总而言之,大牛的博客是个好东西,我就那么看了一下,还是不懂怎莫办啊,哎,就那样就A掉了....... 今天我们来谈一下这个并查集的删除操作,根据我对大牛的理解啊,这个并查集的删除操作并不是把原来的节点删除掉,而是用一个替身替掉,现在的这个点只是用作桥梁的作用,即是无用的,del  ,,,del  ,,,,删除,那些被删掉的就从n开始给他们一个地址,然后即如下代码所示 #i

HDU 4496 D-City(并查集,逆思维)

题目 熟能生巧...常做这类题,就不会忘记他的思路了... //可以反过来用并查集,还是逐个加边,但是反过来输出...我是白痴.....又没想到 //G++能过,C++却wa,这个也好奇怪呀... #include<stdio.h> #include<string.h> int fx,fy,r,bin[10010]; int x[100010],y[100010],n,m,i,count,ans[100010],j; int find(int x) { //改成这样就不会超时了么好

HDU 1272 简单并查集

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24915    Accepted Submission(s): 7641 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双

hdu 3038 (并查集)

题目大意: 给出m个询问,问[l,r]之间的和   ,求出有多少次询问不和之前的矛盾的. 思路分析: 用并查集记录当前节点到根节点的和. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 222222 using namespace std; int set[maxn]; int sum[maxn]; int find(int

hdu--1231--并查集&lt;连分量的个数&gt;

我觉得 这题 是纯粹的 并查集 可以算成 入门题吧 问你有几章桌子 就是问你有几个 连通块嘛 一个道理 touch  me 这题 我采用了下 father[x]开始 初始化为-1 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 const int size = 1010; 6 int father[size]; 7 8 int find( int x ) 9 { 10 return fath

[ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21586   Accepted: 10456 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

hdu 4496 D-City 并查集

D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2317    Accepted Submission(s): 814 Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to D-

Farm Irrigation HDU - 1198 (并查集)

Farm Irrigation HDU - 1198 题意:给11种管道,问草地最少需要打多少个井才可以全部灌溉. 把每种管道的状态用二进制表示一下,然后对每一块草地,判断能否和上面或者左面的草地的管道连接. 然后并查集搞一下. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=55; 4 5 int g[12]={10,9,6,5,12,3,11,14,7,13,15}; 6 int f[maxn*maxn]