HDU 1856 More is better(并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=1856
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 18523 Accepted Submission(s): 6814
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
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<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define N 10000010 int f[N], num[N]; int Find(int x)
{
if(x != f[x])
f[x] = Find(f[x]);
return f[x];
} int main()
{
int n, i, x, y, rx, ry, Max;
while(scanf("%d", &n) != EOF)
{
for(i = ; i <= N ; i++)
{
f[i] = i;
num[i] = ;
}
while(n--)
{
scanf("%d%d", &x, &y);
rx = Find(x);
ry = Find(y);
if(rx != ry)
{
f[ry] = rx;
num[rx] += num[ry];
}
}
Max = ;
for(i = ; i < N ; i++)
{
if(f[i] == i && Max < num[i])
Max = num[i];
}
printf("%d\n", Max);
}
return ;
}
HDU 1856 More is better(并查集)的更多相关文章
- HDU 1856 More is better (并查集)
题意: 给你两个数代表这两个人是朋友,朋友的朋友还是朋友~~,问这些人组成的集合里面人最多的是多少... 思路: 属于并查集了,我用的是带路径压缩的,一个集合里面所有元素(除了根节点)的父节点都是根节 ...
- 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 ...
随机推荐
- vi编辑器基本用法介绍
vi是Linux系统中编写文件的工具 如果vi出现乱码情况,需要升级vi,命令如下: sudo apt-get install vim //升级vi vi的启动方式有两种,直接使用vi命令和在vi命 ...
- [转载]Python模块学习 ---- subprocess 创建子进程
[转自]http://blog.sciencenet.cn/blog-600900-499638.html 最近,我们老大要我写一个守护者程序,对服务器进程进行守护.如果服务器不幸挂掉了,守护者能即时 ...
- 第六届华为创新杯编程大赛-进阶1第1轮 洞穴逃生 (bfs + 优先队列)
这个题容易出错想了挺长时间,然后代码不长,1Y.. 做完题,看了一下别人的博客,也可以优先用 闪烁法术, 在闪烁法术不不如跑步的阶段(即魔法恢复的时候)用跑步. 洞穴逃生 描述: 精灵王子爱好冒险,在 ...
- HighCharts 饼图
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section ...
- 【笨嘴拙舌WINDOWS】API
如今,相对于大行其道的对象,服务,API概念的提出要早很多,却依然经久不衰:所谓万变不离其宗,如今很多服务(Web Services,云服务)的提供方式和API如出一辙. Windows API(Ap ...
- iOS开发:插件记录
进入沙盒的插件 https://github.com/TongeJie/ZLGotoSandboxPlugin 图片提示的插件 https://github.com/ksuther/KSImageNa ...
- Windows下免费、开源邮件服务器hMailServer
Windows下免费.开源邮件服务器hMailServer 一.Windows下搭建免费.开源的邮件服务器hMailServer 二.邮件服务器hMailServer管理工具hMailServer A ...
- busybox filesystem httpd php-5.5.31 sqlite3 webserver
/******************************************************************** * busybox filesystem httpd php ...
- pip
查看安装的包 pip list
- 关于ecshop中jquery与js冲突解决的方案
ECShop把AJAX事件和JSON解析的模块放在common/transport.js之中,可以说它也有自己封装的一套工具,这其实是很正常的. 但恰恰的,在封装JSON各种方法的同时对objec ...