首页 技术 正文
技术 2022年11月21日
0 收藏 353 点赞 4,868 浏览 1726 个字

思路:注意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;
}

作者:u011652573 发表于2014-4-30 22:37:43 原文链接 阅读:31 评论:0 查看评论

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898