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. win端git连接私服仓库+上传本地项目+从服务器下载文件到win

    win端git连接私服仓库: 1.win端 检查c:/Users/用户/.ssh/目录下是否有config文件(!!!没有任何后缀名).如果没有则新建config文件,然后修改添加如下内容: Host ...

  2. JSP 基础之 JSTL <c:choose>用法 if else

    <c:choose> <c:when test="${condition1}"> condition1为true </c:when> <c ...

  3. 分析函数调用堆栈的原理和Delphi实现

    来自:http://blog.163.com/liuguang_123/blog/static/816701920105262543890/ ----------------------------- ...

  4. setCharacterEncoding 和 setContentType

    request.setCharacterEncoding("gbk"); response.setContentType("text/html;charset=gbk&q ...

  5. python接口自动化11-post传data参数案例【转载】

    前言: 前面登录博客园的是传json参数,有些登录不是传json的,如jenkins的登录,本篇以jenkins登录为案例,传data参数. 一.登录jenkins抓包 1.登录jenkins,输入账 ...

  6. Selenium2+python自动化47-判断弹出框存在(alert_is_present)【转载】

    前言 系统弹窗这个是很常见的场景,有时候它不弹出来去操作的话,会抛异常.那么又不知道它啥时候会出来,那么久需要去判断弹窗是否弹出了. 本篇接着Selenium2+python自动化42-判断元素(ex ...

  7. python之多并发socket(zz)

    本文转载自:http://www.cnblogs.com/bainianminguo/p/7337210.html 先看socket多并发的服务端的代码,这里是用多线程实现的多并发socketserv ...

  8. web api 返回数据

    一.Webapi的接口返回值类型 主要有四种类型 :void,HttpResponseMessage,IHttpActionResult,其他 1. void [HttpGet] public voi ...

  9. 操作JSON————精品

    使用背景: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原 ...

  10. 微软应用商店错误 0x00000194

    也可以下载安装包手动更新尝试解决. ------------------------------------------------------------------- 今天OTA升级了1809,应 ...