码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1856 More is better

时间:2019-07-06 19:05:17      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:integer   模板   有一个   imm   dir   ted   not   leave   for   

题目:

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<algorithm>
 3 #define MAXN 10000000
 4 using namespace std;
 5 
 6 int pre[MAXN];
 7 int num[MAXN];  //存放一个朋友圈的人数
 8 int maxx = 0;
 9 
10 void init()
11 {
12     for(int i = 1; i <= MAXN; i++)
13     {
14         pre[i] = i;  //初始化成独立节点
15         num[i] = 1;  //独立节点自然只有一个人
16     }
17 }
18 
19 int find(int x)
20 {
21     while(x != pre[x])
22         x = pre[x];
23     
24     return x;
25 }
26 
27 void merge(int x, int y)
28 {
29     int fx = find(x);
30     int fy = find(y);
31     if(fx!=fy)
32     {
33         if(num[fx] > num[fy])  //人数少的归并到人数多的一组
34         {
35             int t = fy;
36             fy = fx;
37             fx = t;
38         }
39         pre[fx] = fy;
40         num[fy] += num[fx];  //归并后人数
41         if(num[fy] > maxx)
42             maxx = num[fy];  //能留下的最大人数
43     }
44 }
45 
46 int main()
47 {
48     int n, a, b;
49     while(scanf("%d", &n) != EOF)
50     {
51         if(n == 0)    //n == 0,说明不存在任何朋友关系,所有人中只能留下一个人
52         {
53             printf("1\n"); 
54             continue;    
55         }
56         init();
57         for(int i = 0; i < n; i++)
58         {
59             scanf("%d %d", &a, &b);
60             merge(a, b);         
61         }
62         printf("%d\n", maxx);
63     }    
64 
65     return 0;
66 }

不知为何会TLE的代码:

 1 #include <stdio.h>
 2 #define MAXN 10000000
 3 
 4 int pre[MAXN];
 5 int num[MAXN];
 6 int maxx;
 7 
 8 void init()
 9 {
10     for(int i = 1; i <= MAXN; i++)
11     {
12         pre[i] = i;
13         num[i] = 1;
14     }
15 }
16 
17 int find(int x)
18 {
19     while(x != pre[x])
20         x = pre[x];
21     
22     return x;
23 }
24 
25 void merge(int x, int y)
26 {
27     int fx = find(x);
28     int fy = find(y);
29     if(fx != fy)
30     {
31         pre[fx] = fy;
32         num[fy] += num[fx];
33         if(num[fy] > maxx)
34             maxx = num[fy];
35     }
36 }
37 
38 int main()
39 {
40     int n, a, b;
41     while(scanf("%d", &n) != EOF)
42     {
43         if(n == 0)    //n == 0,只能留下一个人 
44         {
45             printf("1\n"); 
46             continue;    
47         }
48         init();
49         maxx = 0;
50         for(int i = 0; i < n; i++)
51         {
52             scanf("%d %d", &a, &b);
53             merge(a, b);         
54         }
55         printf("%d\n", maxx);
56     }    
57 
58     return 0;
59 }

 

总结:

还是不知道为什么会超时,也不知道为什么加上下面这段代码就可以AC了:

1         if(num[fx] > num[fy])
2         {
3             int t = fy;
4             fy = fx;
5             fx = t;
6         }

 上面这段代码不过是加了个判断哪个队长带队人数多,将少的归到多的里面而已,但是感觉超时的代码本身也没有错,只要将两队合并,更新合并后的人数就行。
如果非要说有错,只能说是还不够严谨。

  • 所以,还是要严谨!严谨!!严谨!!!

HDU 1856 More is better

标签:integer   模板   有一个   imm   dir   ted   not   leave   for   

原文地址:https://www.cnblogs.com/Anber82/p/11143691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!