【并查集】hdu 1856 More is better

【并查集】hdu 1856 More is better

n个元素的集合应用问题——并查集。

题目大意

n个元素,告诉它们的两两连接关系,求单个连通分支所含元素个数的最大值


说一下思路

经典的并查集求的是所含连通分支的个数,这道题求的是并查集森林最大点集数(元素数),思路是维护一个cnt[ ]数组,初始化元素所在集合为自身cnt[ ] = 1;当要合并时,父节点累加孩子节点,cnt[ y ] += cnt [ x ],最后输出最大值。

★这题没有离散化就过了,so big the memory is !~


参考代码

#include<bits/stdc++.h>
using namespace std;

const int _max = 1e7 + 10;
int n,a,b,pre[_max],cnt[_max],m;

void init(){
 for(int i = 1;i <= _max;++ i){
    pre[i] = i;//每个元素所在的森林初始化自身
    cnt[i] = 1;//自身点数为1
 }
}

int find(int x){//查,元素x的根节点
  return pre[x] == x?x:pre[x] = find(pre[x]);
}

void join(int a,int b){
  int x = find(a),y = find(b);
  if(x != y) {//根节点不属于同一片森林
    pre[x] = y;
    cnt[y] += cnt[x];//y作父亲节点!!!
  }
}

int main(){
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  #endif // ONLINE_JUDGE
  while(scanf("%d",&n) == 1){
    if(n == 0) {puts("1");continue;}//特判n == 0的情况
    init();
    m = 0;
    for(int i = 0;i < n; ++ i){
        scanf("%d%d",&a,&b);
        join(a,b);
        if(a > b) m = max(m,a);//找到最大的那个节点
        else m = max(m,b);
    }
    printf("%d\n",*max_element(cnt+1,cnt+1+m));
  }
  return 0;
}
  • 加粗 Ctrl + B
  • 斜体 Ctrl + I
  • 引用 Ctrl + Q
  • 插入链接 Ctrl + L
  • 插入代码 Ctrl + K
  • 插入图片 Ctrl + G
  • 提升标题 Ctrl + H
  • 有序列表 Ctrl + O
  • 无序列表 Ctrl + U
  • 横线 Ctrl + R
  • 撤销 Ctrl + Z
  • 重做 Ctrl + Y

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-20 10:45:33

【并查集】hdu 1856 More is better的相关文章

并查集 hdu 1856

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 16863    Accepted Submission(s): 6205 Problem Description Mr Wang wants some boys to help him with a project. Because the projec

并查集 -- HDU 1232 UVALA 3644

并查集: 1 int pa[maxn],Rank[maxn]; 2 ///初始化 x 集合 3 void make_set(int x) 4 { 5 pa[x]=x; 6 Rank[x]=0; 7 } 8 ///递归查找 x 所在的集合 9 int find_set(int x) 10 { 11 if(pa[x]!=x) pa[x]=find_set(pa[x]); 12 return pa[x]; 13 } 14 ///更新根节点,如果不更新可能会暴栈 15 void mix(int x,in

集合问题 离线+并查集 HDU 3938

题目大意:给你n个点,m条边,q个询问,每条边有一个val,每次询问也询问一个val,定义:这样条件的两个点(u,v),使得u->v的的价值就是所有的通路中的的最长的边最短.问满足这样的点对有几个. 思路:我们先将询问和边全部都按照val排序,然后我们知道,并查集是可以用来划分集合的,所以我们就用并查集来维护每一个集合就行了. //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespac

(并查集) hdu 2473

Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6389    Accepted Submission(s): 2025 Problem Description Recognizing junk mails is a tough task. The method used here consists o

分组并查集 hdu 1829

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10063    Accepted Submission(s): 3288 Problem Description Background Professor Hopper is researching the sexual behavior of a rare

(枚举+并查集) hdu 1598

find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4096    Accepted Submission(s): 1768 Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级

(并查集) hdu 4750

Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 952    Accepted Submission(s): 422 Problem Description   With the 60th anniversary celebration of Nanjing University of Science

(并查集) hdu 3038

How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2856    Accepted Submission(s): 1100 Problem Description TT and FF are ... friends. Uh... very very good friends -_____

(并查集) hdu 3635

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3443    Accepted Submission(s): 1342 Problem Description Five hundred years later, the number of dragon balls will increase unexpecte

(并查集) hdu 3461

Code Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1308    Accepted Submission(s): 471 Problem Description A lock you use has a code system to be opened instead of a key. The lock contai