More is better

Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 327680/102400K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
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. [/hint]
王老师要找一些男生帮助他完成一项工程。要求最后挑选出的男生之间都是朋友关系,可以说直接的,也可以是间接地。问最多可以挑选出几个男生(最少挑一个)。
 思路:加一个数组记录数的元素个数;剩下的并差集解决;
代码:
 #include<stdio.h>
int relate[],num[];//num[]记录个数;
int min;
int find(int x){
int r=x;
while(r!=relate[r])r=relate[r];
int i=x,j;
while(i!=r)j=relate[i],relate[i]=r,i=j;
return r;
}
void initial(){
for(int i=;i<=;++i)relate[i]=i,num[i]=;
}
void merge(int x,int y){
int f1,f2;
f1=find(x);f2=find(y);
if(f1!=f2)relate[f1]=f2,num[f2]+=num[f1];
min=min>num[f2]?min:num[f2];
}
int main(){
int n,temp1,temp2;
while(~scanf("%d",&n)){
min=;initial();
while(n--){
scanf("%d%d",&temp1,&temp2);
merge(temp1,temp2);
}
printf("%d\n",min);
}
return ;
}

More is better(并差集)的更多相关文章

  1. C# 数组的交集、差集、并集

    C# 数组的交集.差集.并集 工作中经常会用这方面的知识来检查那些字段是必须输入的,那些是禁止输入. using System; using System.Collections.Generic; u ...

  2. java求字符串数组交集、并集和差集

    import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Ma ...

  3. SQL中对于两个不同的表中的属性取差集except运算

    SQL中对两个集合取差集运算,使用except关键字,语法格式如下: SELECT column_name(s) FROM table_name1 EXCEPT SELECT column_name( ...

  4. C# Linq 交集、并集、差集、去重

    using System.Linq;         List<string> ListA = new List<string>(); List<string> L ...

  5. js取两个数组的交集|差集|并集|补集|去重示例代码

    http://www.jb51.net/article/40385.htm 代码如下: /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一 ...

  6. Linux 对比两个文本的交集和差集(comm)

    200 ? "200px" : this.width)!important;} --> 介绍 comm命令可以对两个已排序好的文本的内容进行交集和差集的对比,记住必须是已排序 ...

  7. [转]SQL 操作结果集 -并集、差集、交集、结果集排序

    本文转自:http://www.cnblogs.com/kissdodog/archive/2013/06/24/3152743.html 操作结果集 为了配合测试,特地建了两个表,并且添加了一些测试 ...

  8. scala中集合的交集、并集、差集

    scala中有一些api设计的很人性化,集合的这几个操作是个代表: 交集: scala> Set(1,2,3) & Set(2,4) // &方法等同于interset方法 sc ...

  9. Codeforces Round#250 D. The Child and Zoo(并差集)

    题目链接:http://codeforces.com/problemset/problem/437/D 思路:并差集应用,先对所有的边从大到小排序,然后枚举边的时候,如果某条边的两个顶点不在同一个集合 ...

  10. java list 交集 并集 差集 去重复并集

    package com; import java.util.ArrayList;import java.util.Iterator;import java.util.List; public clas ...

随机推荐

  1. [Redux] Extracting Action Creators

    We will create an anction creator to manage the dispatch actions, to keep code maintainable and self ...

  2. MySQL学习笔记:MySQL: ERROR 1064(42000)

    ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL s ...

  3. [Python学习笔记][第六章Python面向对象程序设计]

    1月29日学习内容 Python面向对象程序设计 类的定义与使用 类定义语法 使用class关键词 class Car: def infor(self): print("This is ca ...

  4. XMLHttpResponse 在项目里面的运用

    前些天在项目里面遇到了一个问题,项目的列表页面每条记录后面都有按钮做审核操作,但是这个操作并不需要引起弹窗,只需要到后台修改一下这条记录的一些状态值,但是操作执行之后却没有刷新页面,只有重新载入或者刷 ...

  5. AE分级渲染

    分级渲染classbreakrenderer位于carto类库中,进行分级渲染时,首先要将相应图层按照某一Field分级.可使用esrisystem类库中的iclassifyGEN类的classify ...

  6. Selenium WebDriver 学习笔记

    1. 打开VS2012 2. 新建工程(单元测试工程或控制台程序都可以, 看需求) 3. 工具->NuGet程序包管理器->程序包管理器控制台 4. 输入"Install-Pac ...

  7. 关于EventHandler的使用

    也就是委托和事件的使用.使用过,但了解的不够深入,现在逐渐来填以前留下的坑吧. EventHandler, EventHandler<TEventArg> 是.net Framework内 ...

  8. 如何在程序退出的时候清除activity栈

    在公司里接手了一个后期的项目,由于项目前期对activity栈管理的不够谨慎,所以导致了在某些情况下程序退出的时候没有将activity栈清除掉.在网上找到的无非就是那几种例子,都不是最好的解决办法. ...

  9. emjio表情转json

    今天碰到了上传文字给服务端的时候emjio表情转成json就不对了 有大神帮忙解决了,记笔记记笔记--- #import "NSString+EncodedString.h" @i ...

  10. (原)下载pubFig的python代码

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5715305.html pubFig数据库网址: http://www.cs.columbia.edu/ ...