[原]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 ...
随机推荐
- c++ swap 函数
转载地址 1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T ...
- ios 聊天demo 和nsoperationdemo
http://blog.csdn.net/zhibudefeng/article/details/7991649 http://blog.csdn.net/kangx6/article/details ...
- HTTP message vs SOAP message
HTTP Message / SOAP Message HTTP Message ===>包括Request, Response.我们主要关注的是 HTTP Message,这样子包含的范围会更 ...
- maven mirror repository
简单点来说,repository就是个仓库.maven里有两种仓库,本地仓库和远程仓库.远程仓库相当于公共的仓库,大家都能看到.本地仓库是你本地的一个山寨版,只有你看的到,主要起缓存作用.当你向仓库请 ...
- photosop快速对白色背景图片进行抠图
因为其中有个作业,做个图书馆的小网页.所以打算取图书馆logo上面那几个字. 图片如下: 因为是白色背景,一开始打算使用魔棒工具,不过效果不好. 后来百度了下,使用色彩范围可以快速抠图 打开photo ...
- Lock wait timeout exceeded; try restarting transaction
What gives this away is the word transaction. It is evident by the statement that the query was atte ...
- Gvim for php 安装配置
VIM for PHP Windows 2011-05-14 11:51:51| 分类: Php|举报|字号 订阅 虽然vim本质上只是一个编辑器.但只要配合一些适当的插件,vim也能变成一 ...
- 传说中的WCF(4):发送和接收SOAP头
如果你实在不明白Header是个啥玩意儿,你就想一想你发送电子邮件时,是不是有个叫“附件”的东东?对啊,那么SOAP头是不是可以理解为一种附加信息?就是附加到消息正文的内容. 消息正文又是啥?WCF除 ...
- JS中的内部类
js内部类 javascript中本身有提供一些可以直接使用的类,这种类就是内部类.主要有:Object.Array.Math.Boolean.String.Number.Date.RegExp. ...
- Shell练习 验证号码
原文: https://leetcode.com/problems/valid-phone-numbers/ Given a text file file.txt that contains list ...