裸并查集,但有二坑:

1.需要路径压缩,不写的话会TLE

2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <string>
#include <cctype>
#include <cmath> #pragma comment(linker, "/STACK:102400000,102400000")
using namespace std; const int maxn = 1e7 + ;
int father[maxn], ans[maxn], res; int getFather (int x) {
if (x != father[x]) {
father[x] = getFather (father[x]);
}
return father[x];
} void Union (int p, int q) {
int x = getFather (p);
int y = getFather (q);
if (x != y) {
father[y] = x;
ans[x] += ans[y];
res = max (ans[x], res);
}
} int main () {
int n;
while (~scanf("%d", &n)) {
for (int i = ; i < maxn; ++ i) {
father[i] = i;
ans[i] = ;
} int x, y, MAXX = ;
res = ;
for (int i = ; i < n; ++ i) {
scanf("%d%d", &x, &y);
Union (x, y);
MAXX = max(x, MAXX);
MAXX = max(y, MAXX);
} cout << res << endl;
}
return ;
}

【HDU1856】More is better(并查集基础题)的更多相关文章

  1. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  2. 【HDU1232】畅通工程(并查集基础题)

    裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...

  3. 【HDU1272】小希的迷宫(并查集基础题)

    仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...

  4. 【HDU2120】Ice_cream's world I(并查集基础题)

    查环操作,裸题.一次AC. #include <iostream> #include <cstring> #include <cstdlib> #include & ...

  5. 【HDU1325】Is It A Tree?(并查集基础题)

    有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路 ...

  6. poj1182 食物链(并查集 好题)

    https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...

  7. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  8. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  9. PAT甲级 并查集 相关题_C++题解

    并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...

随机推荐

  1. Android自定义ListView的Item无法响应OnItemClick的解决办法

     转: 如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也 ...

  2. hdu 4512 吉哥系列故事——完美队形I_LCIS

    题目链接 题意: 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要    求,则就是新 ...

  3. js基础例子购物车升级版(未优化版)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. (转)iOS Wow体验 - 第一章 - iOS人机界面设计规范纵览

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第一章译文精选,其余章节将陆续放出. 关于本套译文 ...

  5. [Err] 1449 - The user specified as a definer ('admin_isbox'@'localhost') does not exist

    晚上加班调用一个远程拷贝的本地Mysql的储存过程,报错:[Err] 1449 - The user specified as a definer ('admin_isbox'@'localhost' ...

  6. 数学之路(3)-机器学习(3)-机器学习算法-PCA

    PCA 主成分分析(Principal components analysis,PCA),维基百科给出一个较容易理解的定义:“PCA是一个正交化线性变换,把数据变换到一个新的坐标系统中,使得这一数据的 ...

  7. Filter过滤器实现登录检查

    主要利用filter过滤掉未经登录而直接跳转到非登录访问页面.代码而言的话并不难,只是有几点问题需要注意一下. 1.使用filter需要配置web.xml,如果是/*那么在拦截后的页面会连带jsp页面 ...

  8. OC-字典&数组运用实例:通讯录的实现

    需求实现: 一.定义联系⼈类ContactPerson 实例变量:姓名.性别.电话号码.住址.分组名称. 方法:初始化⽅方法(姓名.电话号码).显⽰示联系⼈信息 二.定义AddressBook类, 封 ...

  9. 普通用户登录PLSQL后提示空白OK对话框错误

    问题描述: 1.普通域账号登录域成员服务器后,打开PLSQL正常,输入用户名密码登录后提示一个空白的OK对话框,点确定后又返回到输入用户密码界面. 2.在CMD窗口下调用SQLPLUS登录数据库时报如 ...

  10. 22个CSS黑魔法

    原链接:http://www.ido321.com/1665.html Hey there! Today we are going to talk about some useful tricks i ...