HDU 1856 More is better【并查集】
解题思路:将给出的男孩的关系合并后,另用一个数组a记录以find(i)为根节点的元素的个数,最后找出数组a的最大值
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 15861 Accepted Submission(s): 5843
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.
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
2
#include<stdio.h>
#include<string.h>
int pre[10000010],a[10000010];
int find( int root)
{
if(root!=pre[root])
pre[root]=find(pre[root]);
return pre[root];
}
void unionroot( int root1,int root2)
{
int x,y;
x=find(root1);
y=find(root2);
if(x!=y)
pre[x]=y;
} int main()
{
int n;
int root1,root2,x,y,k,i,max;
while(scanf("%d",&n)!=EOF)
{
max=-100000;
for(i=1;i<=10000010;i++)
a[i]=0; for(i=1;i<=10000010;i++)
pre[i]=i;
while(n--)
{
scanf("%d %d",&root1,&root2);
x=find(root1);
y=find(root2);
unionroot(x,y);
} for(i=1;i<=10000010;i++)
{
k=find(i);
a[k]++;//记录以find(i)为根节点的包含有多少 个元素
}
for(i=1;i<=10000010;i++)
{
if(a[i]>max)
max=a[i];
}
printf("%d\n",max);
}
}
HDU 1856 More is better【并查集】的更多相关文章
- 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) ...
- HDU HDU1558 Segment set(并查集+判断线段相交)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...
- hdu 1257 小希的迷宫 并查集
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...
- HDU 4496 D-City(逆向并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...
- HDU 3407.Zjnu Stadium 加权并查集
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- HDU 4641 K-string 后缀自动机 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=4641 https://blog.csdn.net/asdfgh0308/article/details/4096 ...
随机推荐
- week7_notebooke
回顾:类:具有相同属相和技能的一类事物对象:类的具体表现class A: country = 'China' #静态变量,静态字段 def __init__(self): #动态变量,方法 self. ...
- surfaceView实现拍照功能
1.布局中只有一个SurfaceView和Button,初始SurfaceView通过surface.getHolder获得SurfaceHolder类 SurfaceView sfv= (Surfa ...
- Android截图<包括Alertdiaog>
1.使用的系统内部的截屏方法…… 2. public class MainActivity extends AppCompatActivity { private static final Strin ...
- SecurityManager USE
import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io. ...
- luoguP4238 【模板】多项式求逆 NTT
Code: #include <bits/stdc++.h> #define N 1000010 #define mod 998244353 #define setIO(s) freope ...
- Java 面向对象详解
0 引言 接触项目开发也有很长一段时间了,最近开始萌发出想回过头来写写以前学过的基础知识的想法. 1 面向对象 面向对象(Object Oriented)是一种新兴的程序设计方法,或者是一种新的程序设 ...
- laravel save() 返回 null
原因:引用其他方法时,没有 return save()的操作结果. 在使用save()方法时,发现返回值是:null:
- php数组 匹配键值
1.array_search() //判断键值是否在数组中,//存在,返回值对应的键;//不存在,返回false;//例子:$type = array( "选考" => 'o ...
- cf掉分记——Avito Code Challenge 2018
再次作死的打了一次cf的修仙比赛感觉有点迷.. 还好掉的分不多(原本就太低没法掉了QAQ) 把会做的前三道水题记录在这.. A: Antipalindrome emmmm...直接暴力枚举 code: ...
- u-boot启动代码start.S详解360
(1)定义入口.由于一个可执行的Image必须有一个入口点,并且只能有一个全局入口,通常这个入口放在ROM(Flash)的0x0地址,因此,必须通知编译器以使其知道这个入口,该工作可通过修改连接器脚本 ...