[原]1856-More is better-基础并查集
思路:注意n为0的时候输出1,还有内存。这题是数据水了,要不我的Count[ ]数组,开10^5绝对会WA。离散化还没想清楚,想清楚了再更新代码。【水过代码下面是正经的AC代码,其实这道题不用离散化,因为即使离散化还是要开多两个10^7的数组,之前就是因为酱紫MLE了,后来只是改变了路径压缩的方式,把原本的记录高度改成记录树里的节点数,道理是一样的,不会退化。还省了点内存。】
下面是水过的AC代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 10000009
int par[maxn], h[maxn], n, Count[maxn/100];
bool vis[maxn];
void init()
{
for(int i = 0; i < maxn; i++) {
par[i] = i;
vis[i] = false;
h[i] = 0;
if(i < maxn/100)
Count[i] = 0;
}
}
bool cmp(int a, int b)
{
return a > b;
}
int Find(int x)
{
if(par[x] == x) return x;
return par[x] = Find(par[x]);
}
void Union(int a, int b)
{
a = Find(a);
b = Find(b);
if(h[a] > h[b]) par[b] = par[a];
else{
if(h[a] == h[b]) h[b]++;
par[a] = par[b];
}
}
void work()
{
for(int i = 0; i < n; i++){
int a, b;
scanf("%d%d", &a, &b);
Union(a, b);
vis[a] = vis[b] = true;
}
for(int i = 0; i < maxn; i++)
if(vis[i]) {
int p = Find(i);
Count[p]++;
}
sort(Count,Count+maxn/100,cmp);
cout<<Count[0]<<endl;
}
int main()
{
while(scanf("%d", &n) != EOF){
if(n != 0){
init();
work();
}
else cout<<"1"<<endl;
}
return 0;
}
AC代码II:
//AC
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 10000009
int par[maxn],child_num[maxn], Max, n;
void init()
{
for(int i = 0; i < maxn; i++) {
par[i] = i;
child_num[i] = 1;
}
Max = 0;
}
int Find(int x)
{
if(par[x] == x) return x;
return par[x] = Find(par[x]);
}
void Union(int a, int b)
{
a = Find(a);
b = Find(b);
if(a != b)
if(child_num[a] > child_num[b]) {
par[b] = par[a];
child_num[a] += child_num[b];
if(Max < child_num[a]) Max = child_num[a];
}
else{
child_num[b] += child_num[a];
par[a] = par[b];
if(Max < child_num[b]) Max = child_num[b];
}
}
void work()
{
for(int i = 0; i < n; i++){
int a, b;
scanf("%d%d", &a, &b);
Union(a, b);
}
cout<<Max<<endl;
}
int main()
{
while(scanf("%d", &n) != EOF){
if(n != 0){
init();
work();
}
else cout<<"1"<<endl;
}
return 0;
}
[原]1856-More is better-基础并查集的更多相关文章
- hdu 1829 基础并查集,查同性恋
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- HDU 1856 More is better(并查集+离散化)
题目地址:HDU 1856 水题.因为标号范围太大,而数据数仅仅有10w,所以要先进行离散化.然后就是裸的并查集了. 代码例如以下: #include <iostream> #includ ...
- 杭电 1856 More is better (并查集求最大集合)
Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...
- HDU4496 D-City【基础并查集】
Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...
- AOJ 2170 Marked Ancestor (基础并查集)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...
- poj2236 基础并查集
题目链接:http://poj.org/problem?id=2236 题目大意:城市网络由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连 进行若干操作,O ...
- 基础并查集poj2236
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...
- hdu 1856 More is better (并查集)
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- CodeForces - 827A:String Reconstruction (基础并查集)
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
随机推荐
- 看我是一只IT小小鸟有感
当我看了<我是一只IT小小鸟>后,有许多的感想.就像许多作者一样,在接触计算机这个专业时都有许多的抱怨,对这个专业的不了解,对这个专业不知道从何学起有深深的无助感,对这个专业在未来的发展有 ...
- 【Tsinsen】【A1365】森林旅店
KD-Tree 啊哈~检验了一下自己KD-Tree的学习情况,还算可以,模板至少是记下来了. 支持插入(所以要带重建),查询最近的P个点的距离. 然而题目并没有说是按怎样的顺序输出这P个点?...(事 ...
- 系统集成之用户统一登录( LDAP + wso2 Identity Server)
本文场景: LDAP + wso2 Identity Server + asp.net声明感知 场景 ,假定读者已经了解过ws-*协议族,及 ws-trust 和 ws-federation. 随着开 ...
- 社区O2O,才是未来10年移动互联网最赚钱的项目
原文:http://blog.sina.com.cn/s/blog_70e76a920102uyoi.html 8月12日 上海 晴 从深圳回来后,一直和郭老师探讨一个问题:新媒体营销未来最大的市 ...
- Unity3D开发Windows Store应用程序 注意事项
原地址:http://blog.csdn.net/jbjwpzyl3611421/article/details/12704491 针对最近在移植window store项目中遇到的问题,我整理了官方 ...
- URAL1018 Binary Apple Tree(树dp)
组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) ...
- POJ 2041
#include <iostream> #include <string> #include <algorithm> using namespace std; st ...
- DF学Mysql(一)——数据库基本操作
1.创建数据库 create Database <数据库名>; 注意:1)数据库名由字母.下划线.@.#和$组成 2)首字母不能是数字和$符号 3)不允许有空格和特殊字符 2.查看数据库 ...
- Ajax的进阶学习(一)
在Ajax课程中,我们了解了最基本的异步处理方式.本章,我们将了解一下Ajax的一些全局请求事件.跨域处理和其他一些问题. 加载请求 在Ajax异步发送请求时,遇到网速较慢的情况,就会出现请求时间较长 ...
- 几个经常用到的字符串的截取(java)
几个经常用到的字符串的截取 string str="123abc456";int i=3;1 取字符串的前i个字符 str=str.Substring(0,i); // or ...