More is better(MST)(求无向图中最大集合元素个数)
Time Limit:1000MS Memory Limit:102400KB 64bit IO Format:%I64d & %I64u
Description
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
Output
Sample Input
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
Sample Output
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<stdio.h>
#include<algorithm>
using namespace std;
int p[] , rk[] , maxn ;
int m ; void init ()
{
for (int i = ; i <= ; i++) {
p[i] = i ;
rk[i] = ;
}
maxn = ;
} int find (int x)
{
return x == p[x] ? x : p[x] = find (p[x]) ;
} void Union (int x , int y)
{
p[x] = y ;
rk[y] += rk[x] ;
maxn = max (maxn , rk[y]) ;
rk[x] = ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
int x , y ;
while (~ scanf ("%d" , &m) ) {
init () ;
for (int i = ; i < m ; i++ ) {
scanf ("%d%d" , &x , &y ) ;
x = find (x) ;
y = find (y) ;
if (x != y)
Union ( x , y ) ;
}
if (maxn )
printf ("%d\n" , maxn ) ;
else
printf ("1\n") ;
}
return ;
}
More is better(MST)(求无向图中最大集合元素个数)的更多相关文章
- 蓝桥杯历届试题 危险系数(dfs或者并查集求无向图关于两点的割点个数)
Description 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系. 我们来定义一个 ...
- 萌新笔记——Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)
最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对"基数"以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了"HyperLo ...
- 求n!中因子k的个数
思路: 求n的阶乘某个因子k的个数,如果n比较小,可以直接算出来,但是如果n很大,此时n!超出了数据的表示范围,这种直接求的方法肯定行不通.其实n!可以表示成统一的方式. n!=(km)*(m!)*a ...
- Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)
最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对“基数”以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了“HyperLogLog”,从而引出了Card ...
- 求数组中最小的k个数
题目:输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. package test; import java.util.Arra ...
- 求n!中含有某个因子个数的方法
链接 [https://www.cnblogs.com/dolphin0520/archive/2011/04/11/2012891.html]
- poj 2406 求字符串中重复子串的个数
Sample Input abcdaaaaababab.Sample Output 1 //1个abcd4 //4个a3 //3个ab #include<stdio.h> #include ...
- php 统计一维数组中重复的元素个数
<?php echo "<pre>"; $array = array(1, 1, 1, 54, 3,4, 3,4, 3, 14, 3,4, 3,7,8,9,12, ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
随机推荐
- [C#详解] (1) 自动属性、初始化器、扩展方法
文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/CSharp-focus-1.html 代码下载:点我下载 目录 前言 属性与自动属性 属性 自动属 ...
- 关于JavaScript打印去掉页眉页脚
因为这个问题,Google和百度都查了个遍,网上主要解决方案都是这一个代码: <script language="JavaScript"> var hkey_root, ...
- 高校手机签到系统——zxing.net生成二维码(补充)
高校手机签到系统——第一部分Authority权限系统(上) 高校手机签到系统——第一部分Authority权限系统(下) 高校手机签到系统——手机客户端 关于zxing.net的使用网上已有很多说明 ...
- [codevs 1503]愚蠢的宠物(特殊的LCA)
题目:http://codevs.cn/problem/1503/ 分析:一道裸的LCA,但是由于询问只有一次,所以可以简单打打……可以从a,b向父节点推直到1节点,然后比较两个序列,后面一段肯定相同 ...
- android之外部文件存储和读取
这次借用上次读写内部存储的代码,只是对将更换文件的读写路径即可.这里需要对获取SDcard的读写权限. 一.AndroidManifest.xml 这里增加了对外部存储设备的读写权限 <?xml ...
- 《TCP/IP详解卷1:协议》第4章 ARP:地址解析协议-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- angular-scope.assign
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- iOS边练边学--static(作用域),copy(深浅拷贝)
一.static作用 二.copy
- IOS开发 证书总结
开发者证书 ------>> 首先你必须获得apple开发者证书,上图这个文件就是apple开发者证书,只要有apple的开发者账号就可以下载到,此证书可以直接到 developer ...
- android经典实战项目视频教程下载
注:这是一篇转载的文章,原文具体链接地址找不到了,将原文分享如下,希望能对看到的朋友有所帮助! 最近在学习android应用方面的技术,自己在网上搜集了一些实战项目的资料,感觉挺好的,发布出来跟大伙分 ...