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 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.

一定要细致看题目啊!

!!

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)

时间限制尽管在1s 可是内存放的非常宽啊!

。 所以什么数组存不下等等的顾虑都抛开吧,大胆的开数组!

【代码】

#include <cstdio>
const int maxn= 10000000+10;
int father[maxn];
int num[maxn];
int Find(int x){ //路径压缩迭代版本号
    int root = x;
    while(root!=father[root])
        root = father[root];//肇东根节点
    while(x!=root){ //将这棵树上的节点都指向根节点
        int tmp = father[x];
        father[x] = root;
        x= tmp;
    }
    return root;
}
void Union(int a,int b){
    int p=Find(a);
    int q=Find(b);
    if(p!=q){
        father[p]=q;
        num[q]+=num[p]; //将合并后的数量加到 父节点上
    }
}
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        if(n==0){
            puts("1");continue;
        }
        for(int i=1;i<=maxn;i++){
            num[i]=1;
            father[i]=i;
        }
        int a,b;
        int Max=0;
        for(int i=0;i<n;i++){
            scanf("%d%d",&a,&b);
            if(a>Max)
                Max=a;
            if(b>Max)
                Max=b;
            Union(a,b);
        }
        //printf("sd");
        int max=0;
         for(int i=1;i<=Max;i++){
             if(num[i]>max)
                 max=num[i];
            // printf("bug\n");
         }
        printf("%d\n",max);
    }
    return 0;
}
时间: 2024-10-12 03:08:06

hdu 1856 More is better(并查集)的相关文章

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 More is better (并查集合并)

[题目链接]click here~~ [题目大意]这个题说的是有m个小朋友(boy),老师要选择尽可能多的小朋友,有一个规则,他们要有关系(比如a,b,c,如果a认识b,b认识c,那么a和c也算有关系)输入他们的关系图,询问最多可以选多少小朋友 [解题思路]把有关系的小朋友加入一个集合中,最后只要统计哪个集合的元素多就行了 代码: #include <bits/stdc++.h> using namespace std; const int N=100005; int father[N],nu

HDU 3635 延缓更新的并查集

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2839    Accepted Submission(s): 1097 Problem Description Five hundred years later, the number of dragon balls will increase unexpecte

HDU 3461 Code Lock(并查集的应用+快速幂)

* 65536kb,只能开到1.76*10^7大小的数组.而题目的N取到了10^7,我开始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 其实路径压缩也可以不用.............  题目的大意: 一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限

HDU 1558 Segment set (并查集+线段非规范相交)

题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. 1 //1558 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <cmath> 6 #define eps 1e-8 7 #define zero(x) (((x) > 0 ? (x) : (-x)) < e

HDU 3407.Zjnu Stadium 加权并查集

Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3726    Accepted Submission(s): 1415 Problem Description In 12th Zhejiang College Students Games 2007, there was a new stadium built

HDU 1213 How Many Tables (并查集)

How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1213 Appoint description:  System Crawler  (2015-05-25) Description Today is Ignatius' birthday. He invites a lot of friends. Now

hdu 1232 畅通工程(并查集算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 31088    Accepted Submission(s): 16354 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条

HDU 3371 Connect the Cities(并查集+Kruskal)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先联通了几个点,所以为了判断连通性 很容易想到用并查集+kruskal. 不过要注意 这题有一个坑点,就是边数很多 上限是25000,排序的话可能就超时了.而点数则比较少 上限是500,所以很多人选择用Prim做.但我个人觉得这样连通性不好判断.其实边数多没关系,我们主要去重就好啦,用邻接矩阵存下两点