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. Hibernate框架增删改查测试类归为一个类

    package cn.happy.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org ...

  2. Android开发中用到的框架、库介绍

    Android开发中用到的框架介绍,主要记录一些比较生僻的不常用的框架,不断更新中...... 网路资源:http://www.kuqin.com/shuoit/20140907/341967.htm ...

  3. Android项目svn代码管理问题

    用svn控制版本,svn本身是不会识别哪些该传,哪些不该传,这就导致有些关于路径的东西(比如拓展jar的路径)也被上传了,而当别人下载后,那个路径对于这个人可能完全不存在,项目编译就会出问题.用ecl ...

  4. [转]CSS vertical-align属性详解 作者:黄映焜

      CSS vertical-align属性详解 posted @ 2014-08-26 17:44 黄映焜   前言:关于vertical-align属性. 实践出真知. 垂直居中. 第二种用法. ...

  5. NO.14 两个div并排,左边为绝对宽度,右边为相对宽度

    两个div并排,左边为绝对宽度,右边为相对宽度,这个问题,我也经常遇到,我一般的处理方法是将最大的容器padding-left固定宽度,左边的固定宽度的一块position:absolute,然后ri ...

  6. Linux中Firefox——Firebug插件安装及使用

    Firebug的安装方法即打开方式同httpfox Firebug使用指南: Firebug可以随时编辑页面:在HTML标签中,点击窗口上方的"inspect"命令,然后再选择页面 ...

  7. 我的第一个QML Button的实现

    编写第一个QML,在成功跑完HelloWorld后,决定自己实现Button按钮类. Button是在Quick2版本以上的QtQuick Controls出现的. 在Qt5.5.1版本中,选择插入Q ...

  8. (转)Source vs Binary Disadvantages & Advantages of each!

    原链接:http://www.linuxforums.org/forum/newbie/26472-source-vs-binary-disadvantages-advantages-each.htm ...

  9. Delphi Length函数

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  10. CMarkUp读写XML(转)

    Fast start to XML in C++ Enough bull. You want to create XML or read and find things in XML. All you ...