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.

 
  题意:
    要找一些人完成一项工程。要求最后挑选出的人之间都是朋友关系,可以说直接的,也可以是间接地。问最多可以挑选出几个人(最少挑一个)。
    在基础的并查集上加个数组记录集合的数量。
 #include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
#define N 100 int Tree[N];
int findRoot(int x){//查找x的根节点
if(Tree[x]==-)
return x;
else{
int temp=findRoot(Tree[x]);
Tree[x]=temp;
return temp;
}
} int main()
{
int sum[N];//结点i为根的树的节点数
int n;
scanf("%d",&n);
//初始化
for(int i=;i<N;i++){
Tree[i]=-;
sum[i]=;
} while(n--){
int a,b;
scanf("%d %d",&a,&b);
a=findRoot(a);//找到a所在树的根节点
b=findRoot(b);//找到b所在树的根节点
if(a!=b){//ab不在一棵树上
Tree[a]=b;//a树连接到b树上
sum[b]+=sum[a];//以b为根节点的树上节点数添加a上节点的数目
}
}
int ans=;
for(int i=;i<N;i++){
if(Tree[i]==- && sum[i]>ans)//找到最大值
ans=sum[i];
}
printf("%d",ans);
return ;
}

这里特别说明一下,王道上面的答案有问题,问题出在第42行,因为N=100,而 i 作为数组的下标,应该是0-99,100越界了,如果按照王道上面的做法,最后答案会成100.

More is better——并查集求最大集合(王道)的更多相关文章

  1. 杭电 1856 More is better (并查集求最大集合)

    Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...

  2. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  3. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

  5. BC68(HD5606) 并查集+求集合元素

    tree  Accepts: 143  Submissions: 807  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65 ...

  6. 利用并查集求最大生成树和最小生成树(nlogn)

    hdu1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. PAT L2-013 红色警报(并查集求连通子图)

    战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不 ...

  8. 蓝桥杯历届试题 危险系数(dfs或者并查集求无向图关于两点的割点个数)

    Description 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系. 我们来定义一个 ...

  9. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

随机推荐

  1. 字符设备驱动ioctl实现用户层内核层通信

    测试代码实现 memdev.h #ifndef _MEMDEV_H_ #define _MEMDEV_H_ #include<linux/ioctl.h> #ifndef MEMDEV_M ...

  2. [NOIP 2010] 引水入城

    搜索+贪心. 参考博客:http://blog.sina.com.cn/s/blog_8442ec3b0100xib1.html 主要是要看出来,如果有解的话,每个沿湖城市能够流到的范围是连续的区间. ...

  3. facets学习(1):什么是facets

    ML 数据集可以包含数亿个数据点,每个数据点由数百(甚至数千)的特征组成,几乎不可能以直观的方式了解整个数据集.为帮助理解.分析和调试 ML 数据集,谷歌开源了 Facets,一款可视化工具. Fac ...

  4. 《锋利的jQuery》读书要点笔记7——制作商城网页:网站脚本

    第8章 用jQuery打造个性网站 上一节将网页的样式设计完了,现在开始用jQuery来编写网站的脚本.首先要确定的是应该完成哪些功能. 首页应该完成的功能是: 详情页: 这个页面要完成的效果是: 接 ...

  5. docker官方文档中的dns,link,expose,publish

    link是过时的了,尽量不要用. dns内部集成,也可以用外部. expose只是用于记录,并不真的. publish是否起作用,也要看情况,是否被占用端口. -------------------- ...

  6. HDU 6208 The Dominator of Strings【AC自动机/kmp/Sunday算法】

    Problem Description Here you have a set of strings. A dominator is a string of the set dominating al ...

  7. 训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分)

    layout: post title: 训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分) author: "luowentaoaa" catalog: tr ...

  8. 【bzoj2763】[JLOI2011]飞行路线 (分层图最短路)(优先队列dij)

    [bzoj2763][JLOI2011]飞行路线 2014年3月25日1,7260 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城 ...

  9. [ARC100]E:Or Plus Max(FZT)

    https://arc100.contest.atcoder.jp/tasks/arc100_c 一个很自然的想法是,对于每个K求出i or j=k的所有a[i]+a[j]的最大值ans[k],答案就 ...

  10. Codeforces 915 G Coprime Arrays

    Discipntion Let's call an array a of size n coprime iff gcd(a1, a2, ..., an) = 1, where gcd is the g ...