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

《算法笔记》9.6小节 数据结构专题(2)并查集 问题 C: How Many Tables

题目描述

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

输入

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

输出

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

样例输入 复制
2
6 4
1 2
2 3
3 4
1 4

8 10
1 2
2 3
5 6
7 5
4 6
3 6
6 7
2 5
2 4
4 3
样例输出 复制
3
2

题目大意:今天是 Ignatius 的生日,他邀请了很多朋友。Ignatius 想知道他至少需要多少张桌子。不是所有的朋友都互相认识,而并不是所有的朋友都想和陌生人呆在一起。如果 A 认识 B,B 认识 C,则 A、B、C 互相认识,他们可以呆在一张桌子上。如果 A 认识 B,B 认识 C,D 认识 E,则 A、B、C 可以呆在一张桌子上,而 D、E 必须呆在另一张桌子上。所以 Ignatius 至少需要 2 张桌子。

分析: 题目可以抽象为有n个点m条边的无向图,问有多少个连通分量。实际上和这一节的题目B一样。

#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 main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("in.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int n,m,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        int cnt=1;
        int citys[n+5]={0};
        for(int i=0;i<m;++i)
        {
            int a,b;scanf("%d%d",&a,&b);
            if(citys[a]==0&&citys[b]==0)
                citys[a]=citys[b]=cnt,cnt++;
            else if(citys[a]==0&&citys[b]!=0)
                citys[a]=citys[b];
            else if(citys[a]!=0&&citys[b]==0)
                citys[b]=citys[a];
            else
            {
                int temp=citys[b];
                for(int i=1;i<=n;++i)
                    if(citys[i]==temp)
                        citys[i]=citys[a];
            }
        }
        sort(citys+1,citys+n+1);
//        for(int i=1;i<=n;++i)
//            printf("%d ",citys[i]);
//        printf("\n");
        int sum=1,num=citys[1];
        for(int i=2;i<=n;++i)
        {
            if(citys[i]!=num||citys[i]==0)sum++,num=citys[i];
        }

        printf("%d\n",sum);
    }

    #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;
}

相关文章:

  • EasyExcel构建复杂多级表头
  • 网络安全员证书
  • go类(结构体)和对象
  • windows下玩转vllm:在wsl下安装vllm
  • 高速光耦在通信行业的应用(四) | 1Mbps通信光耦的应用
  • sentinel详细使用教学
  • 两分种解决:xshell终端delete键无效
  • c++中的静态多态和动态多态简介
  • 性能测试【Perfdog】
  • 接口测试工具:postman详解
  • CAM350_安装
  • 【Linux高级IO】Linux多路转接:深入探索poll与epoll的奥秘
  • Ollama的底层实现原理分析
  • 《浔川AI翻译v6.1.1版本推迟上线公告》
  • C++类和对象
  • 算法day4 dfs搜索2题
  • 【机房——LCA】
  • 小米火龙CPU和其他几代温度太高的CPU是由谁代工的
  • 卢卡斯定理判断组合数奇偶(Codeforces Round 1006 (Div. 3)——F)
  • 【含文档+PPT+源码】基于SpringBoot的宠物领养系统设计与实现
  • 制作网站的走马灯怎么做/网络推广代理
  • 如何拥有自己的私人网站平台/steam交易链接在哪看
  • 公司网站重新备案/aso优化渠道
  • 做自己的网站好还是博客好/网站平台做推广
  • 建站怎么建/大数据营销的概念
  • 建设机械网站机构/搜索引擎调价工具哪个好