How Many Tables||HDU1213

本文介绍了一个经典的并查集应用案例,通过并查集算法来解决朋友间的分组问题,即最少需要多少张桌子才能让每个认识的人都坐在同一张桌子上。文章提供了一份完整的C++代码实现,并详细解释了如何通过并查集来确定不同朋友之间的关系。

题目来源于2017HPUACM暑期培训:https://cn.vjudge.net/contest/175491#problem/D

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.

Input

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.

Output

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

Sample Input

2
5 3
1 2
2 3
4 5

5 1
2 5

Sample Output

2
4

题解:就是并查集的运用,算出有几棵树

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int par[1005];
int tot;
int find(int x)//查找根节点 
{
    return x == par[x] ? x : /* */find(par[x]);
}
void unite(int x,int y)//合并 
{
    int fx,fy;
    fx=find(x);
    fy=find(y);
    if(fx!=fy)//判断两节点的根节点是否相同 
    {
        par[fy]=fx;//不相等就把x的根节点作为y根节点的父节点 
//      tot--;
    }
}
int main()
{
    int i,n,m,a,b,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
//      tot=n;
        tot=0;
        for(i=1;i<=n;i++)
           par[i]=i;
        while(m--)
        {
            scanf("%d%d",&a,&b);
            unite(a,b);
        }
////        if(tot<0) tot=0;
        for(i=1;i<=n;i++)
        {
            if(i==find(i))//计算有几个根节点,也就是几棵树 
              tot++;
        }
        printf("%d\n",tot);
    }
return 0;
}

PS:HDU1232与其基本相同

分布式微服务企业级系统是一个基于Spring、SpringMVC、MyBatis和Dubbo等技术的分布式敏捷开发系统架构。该系统采用微服务架构和模块化设计,提供整套公共微服务模块,包括集中权限管理(支持单点登录)、内容管理、支付中心、用户管理(支持第三方登录)、微信平台、存储系统、配置中心、日志分析、任务和通知等功能。系统支持服务治理、监控和追踪,确保高可用性和可扩展性,适用于中小型企业的J2EE企业级开发解决方案。 该系统使用Java作为主要编程语言,结合Spring框架实现依赖注入和事务管理,SpringMVC处理Web请求,MyBatis进行数据持久化操作,Dubbo实现分布式服务调用。架构模式包括微服务架构、分布式系统架构和模块化架构,设计模式应用了单例模式、工厂模式和观察者模式,以提高代码复用性和系统稳定性。 应用场景广泛,可用于企业信息化管理、电子商务平台、社交应用开发等领域,帮助开发者快速构建高效、安全的分布式系统。本资源包含完整的源码和详细论文,适合计算机科学或软件工程专业的毕业设计参考,提供实践案例和技术文档,助力学生和开发者深入理解微服务架构和分布式系统实现。 【版权说明】源码来源于网络,遵循原项目开源协议。付费内容为本人原创论文,包含技术分析和实现思路。仅供学习交流使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三更鬼

谢谢老板!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值