当前位置: 首页 > article >正文

《算法笔记》9.6小节 数据结构专题(2)并查集 问题 D: More is better

题目描述

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

输入

The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

输出

The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

样例输入
3
1 3
1 5
2 5
4
3 2
3 4
1 6
2 6

样例输出
4
5

题目大意: 房间里有编号从1~10000000共10000000人,每次给出n对关系,每对关系表示这两个人被选中了且成为朋友。问最后被选中的最大朋友人数是多少,如果没有人被选中,则留下1个人;如果有多组朋友,输出最大的组有多少人。

分析:并查集的应用。不过这个集合很大,因此要在合并的时候,记录合并后组的人数。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;

int father[10000005],cnt[10000005];

int findFather(int x,int father[])
{
    int a=x;
    while(father[x]!=x)x=father[x];
    while(a!=father[a])
    {
        int temp=a;
        a=father[a],father[temp]=x;
    }
    return x;
}

void Union(int a,int b,int father[],int cnt[],int &ans)
{
    int faA=findFather(a,father),faB=findFather(b,father);
//    db4(a,b,faA,faB);
    if(faA!=faB)
    {
        father[faA]=faB;
        cnt[faB]+=cnt[faA];
        ans=max(ans,cnt[faB]);
    }
    return;
}

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("in.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int T,n,m;
    while(~scanf("%d",&T))
    {
        int ans=-1;
        if(T==0)
        {
            printf("1\n");continue;
        }
        for(int i=1;i<=10000005;++i)
            father[i]=i,cnt[i]=1;
        for(int i=0;i<T;++i)
        {
            scanf("%d%d",&n,&m);
            Union(n,m,father,cnt,ans);
        }
        printf("%d\n",ans);
    }

    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}


http://www.kler.cn/a/600826.html

相关文章:

  • 特殊行车记录仪DAT视频丢失的恢复方法
  • 日志截断/日志中途清空/不停止程序
  • PLC物联网网关是什么?PLC网关应用场景
  • 程序化广告行业(32/89):常见广告位类型深度剖析
  • 106.在 Vue3 中使用 OpenLayers 动态添加 Layer 到 LayerGroup,并动态删除
  • 第 5 章 | Solidity 合约中的整数溢出与精度陷阱全解析
  • 笔记整理三
  • 开源模型应用落地-语音转文本-whisper模型-AIGC应用探索(五)
  • 最大连续子序列和(动态规划 -- 经典Kadane算法)
  • 可视化工程项目管理软件:让复杂工程数据一目了然
  • rabbitmq承接MES客户端服务器
  • sourcetree中的“master“,“origin/master“,“origin/HEAD“这三个图标都是什么意思?GIT 超详细➕通俗易懂版本
  • influxdb在centOS stream 9安装教程
  • 3、孪生网络/连体网络(Siamese Network)
  • <KeepAlive>和<keep-alive>有什么区别
  • 基于51单片机的多点位水位监测proteus仿真
  • Java学习总结-Stream流
  • 微信小程序中使用WebSocket通信
  • 使用Python爬虫获取1688商品(按图搜索)接口
  • 状态空间模型解析 (State-Space Model, SS)