裸并查集,但有二坑:

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. c指针点滴2之比大小

    #include <stdio.h> #include <stdlib.h> void main2() { ]={,,,4.5}; ]; ]; if(p1<p2) { p ...

  2. HTML与CSS入门——第七章 使用表格显示信息

    知识点: 1.创建简单表格的方法 2.控制表格大小的方法 3.对齐内容及在表格中跨越行和列的方法 7.1 创建简单的表格: table标签,border控制边框 tr标签,创建表格的行,包含td td ...

  3. 用sp_change_users_login消除Sql Server的孤立用户

    异常详细信息: System.Data.SqlClient.SqlException: 拒绝了对对象 'zwj_EnterpriseActivities' (数据库 'Ntours',架构 'dbo' ...

  4. WebApi2官网学习记录---Media Formatters

    Web API内建支持XML.JSON.BSON.和form-urlencoded的MiME type. 创建的自定义MIME类型要继承一下类中的一个: MediaTypeFormatter 这个类使 ...

  5. SQL SERVER数据库服务操作

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. [C#][Database]C#通过ODBC以自定义端口连接数据库

    数据库端的配置暂且不说,比较简单,新建用户并开启相应连接权限即可. 通过ODBC连接数据库,重点在于Connection String的书写,在此可以查到几乎所有类型的Data Server的Conn ...

  7. (一)CodeMirror - 基本应用

    基本引用: <link rel="stylesheet" href="../lib/codemirror.css"> <script src= ...

  8. poj1064 二分,注意精度!

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35269   Accepted: 7513 Des ...

  9. Angularjs 日期格式转换

    我自己的随笔,记录我编码的点滴. <!DOCTYPE HTML><html><head>    <meta charset="utf-8" ...

  10. Python提取图片的ROI

    图像处理经常需要提取图片的ROI,本文使用Python提取图片的ROI. 使用的Module是PIL (Pillow),一个图像处理库,用到的函数为类 Image 中的 crop 方法. 函数原型为: ...