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 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.
InputThe 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)OutputThe 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

值得注意的是要用一个数组记录每棵树的秩!

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define MAX 10000005
 6 using namespace std;
 7
 8 int T,n,m;
 9 int fa[MAX],record[MAX];
10
11 int find(int u)
12 {
13     if(u!=fa[u]) fa[u]=find(fa[u]);
14     return fa[u];
15 }
16
17 void Union(int a,int b)
18 {   int x=find(a);
19     int y=find(b);
20     if(x==y) return;
21     else
22     {   fa[y]=x;
23         record[x]+=record[y];
24     }
25 }
26
27 int main()
28 {
29     while(cin>>T){
30
31     if(T==0) cout<<"1"<<endl;
32     else{
33           for(int j=1;j<=MAX;j++)
34           {   fa[j]=j;
35               record[j]=1;
36           }
37           for(int i=0;i<T;i++)
38           {   scanf("%d%d",&n,&m);
39               Union(n,m);
40           }
41           int ans=0;
42           for(int i=1;i<MAX;i++)
43           {   if(ans<record[i]) ans=record[i];
44           }
45           cout<<ans<<endl;
46       }
47   }
48 } 
时间: 2024-11-03 01:16:22

More is better HDU - 1856 ?的相关文章

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