• hdu1856 并查集


    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1856/

    题目就是要求并查集中各树的大小的最大值,我们只要在根节点处存树的大小就可以,合并也是合并根节点的数,最后扫一遍即可。

    代码如下:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef unsigned int ui;
     4 typedef long long ll;
     5 typedef unsigned long long ull;
     6 #define pf printf
     7 #define mem(a,b) memset(a,b,sizeof(a))
     8 #define prime1 1e9+7
     9 #define prime2 1e9+9
    10 #define pi 3.14159265
    11 #define lson l,mid,rt<<1
    12 #define rson mid+1,r,rt<<1|1
    13 #define scand(x) scanf("%llf",&x) 
    14 #define f(i,a,b) for(int i=a;i<=b;i++)
    15 #define scan(a) scanf("%d",&a)
    16 #define mp(a,b) make_pair((a),(b))
    17 #define P pair<int,int>
    18 #define dbg(args) cout<<#args<<":"<<args<<endl;
    19 #define inf 0x3f3f3f3f
    20 const int maxn=1e7+10;
    21 int n,m,t;
    22 inline int read(){
    23     int ans=0,w=1;
    24     char ch=getchar();
    25     while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    26     while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    27     return ans*w;
    28 }
    29 int f[maxn],cnt[maxn],ans=0;
    30 void init()
    31 {
    32     f(i,1,maxn-1)f[i]=i;
    33     f(i,1,maxn-1)cnt[i]=1;
    34     ans=0;
    35 }
    36 int find(int x)
    37 {
    38     if(x==f[x])return x;
    39     return f[x]=find(f[x]);
    40 }
    41 void Union(int x,int y)
    42 {
    43     int fx=find(x);
    44     int fy=find(y);
    45     if(fx==fy)return ;
    46     else
    47     {
    48         f[fx]=fy;
    49         cnt[fy]+=cnt[fx];
    50     }
    51 }
    52 int main()
    53 {
    54     //freopen("input.txt","r",stdin);
    55     //freopen("output.txt","w",stdout);
    56     std::ios::sync_with_stdio(false);
    57     while(scan(m)!=EOF)
    58     {
    59         int a,b;
    60         init();
    61         if(m==0)
    62         {
    63             pf("1
    ");
    64             continue;
    65         }
    66         int maxr=0;
    67         f(i,1,m)
    68         {
    69             a=read(),b=read(),Union(a,b);
    70             maxr=max(maxr,max(a,b));
    71         }
    72         f(i,1,maxr)ans=max(ans,cnt[i]);
    73         pf("%d
    ",ans);
    74     }
    75  } 
  • 相关阅读:
    山丽防水墙客户端的卸载
    还原冰点密码清除
    STP学习总结
    NTFS权限设置时卡死
    SQL server 2000安装时“以前的某个程序安装已在安装计算机上创建挂起”
    Projecet客户端登陆无法通过验证
    Linux-nftables
    Linux-kernel-timeline
    blog编辑技巧
    Linux-swap
  • 原文地址:https://www.cnblogs.com/randy-lo/p/12560304.html
Copyright © 2020-2023  润新知