HDOJ1213 How Many Tables 并查集裸题

并查集(Union-Find) 应用举例 --- 基础篇 本文是作为上一篇文章 《并查集算法原理和改进》 的后续,焦点主要集中在一些并查集的应用上。材料主要是取自POJ,HDOJ上的一些算法练习题。 首先还是回顾和总结一下关于并查集的几个关键点: 以树作为节点的组织结构,结构的形态很是否采取优化策略有很大关系,未进行优化的树结构可能会是“畸形”树(严重不平衡,头重脚轻,退化成链表等),按尺寸(正规说法叫做秩,后文全部用秩来表示)进行平 阅读详情

How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33675    Accepted Submission(s): 16840


Problem Description
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
 

Author
Ignatius.L
 

Source
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:   1856  1325  1198  1879  1875 

裸题,程序就是标准的模板
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;

const int maxn = 1005;
int n,m,i,j,k,tot;
int root[maxn];

void init(){
   for (i=1; i<=n; i++) root[i] = i;
   tot = 0;
}

int Find(int x){
   int r = x;
   while (r!=root[r]) r = root[r];
   int i = x, k;
   while (root[i] != r) {
      k = root[i];
      root[i] = r;
      i = k;
   }
   return r;
}

void Merge(int x, int y){
   int fx = Find(x), fy = Find(y);
   if (fx != fy) root[fx] = fy;
}

int main(){
    std::ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> m;
        init();
        for (k=1; k<=m; k++) {
            cin >> i >> j;
            Merge(i,j);
        }
        for (i=1; i<=n; i++) if (root[i]==i) tot++;
        cout << tot << endl;
    }
    return 0;
}


[统计模型] 基于R的潜在剖面分析(LPA) 本文主要介绍基于R进行潜在剖面分析(Latent Profile Aanalysis, LPA)。 关于潜在类别分析LCA/潜在剖面分析LPA的概念和区别,可以参考一下这篇文章: 潜在类别分析LCA \潜在剖面分析LPA介绍https://www.jianshu.com/p/4c830094131e?utm_campaign=maleskine 但是此文对潜类别分析的阐释还比较生涩,而且没有具体的代码编写、模型优选等内容。本文旨在丰富LPA的内容,并希望和大家交流、共同进步。 1. 什么是潜类别分. 阅读详情

相关推荐

拼团系统设计-人群标签的设计与思考

最近在学习一个拼团项目中涉及到的人群模块的设计,写下一些实现与思考。

wywcool的博客 971

并查集(大裸题)

给你一个 n 个点,m 条边的无向图,求至少要在这个的基础上加多少条无向边使得任意两个点可达~ 输入 4 2 1 2 3 4 输出 1 居然一开始看到没有立马想到并查集,ans=联通区域数-1; ACCODE: #include<bits/stdc++.h> using namespace std; int fa[100010]; int n,m; int find(int x){ if(x==fa[x])return x; else return

m0_52075409的博客 510

【Diffusion分割】医学图像分割中去噪扩散技术

扩散模型(DPM)在图像生成方面表现出了卓越的性能,其性能往往优于其他生成模型。自其问世以来,强大的噪声-图像去噪管道已被扩展到各种判别任务中,包括图像分割。在医学成像中,图像往往是大型三维扫描图像,由于内存消耗大、迭代采样过程耗时,使用 DPM 对一幅图像进行分割的效率极低。在这项工作中,我们提出了一种(LDSeg),可在潜空间中执行扩散,用于医学影像分割。我们。潜空间条件扩散不仅能确保对多标签对象进行精确的 n-D 图像分割,还能缓解基于 DPM 的传统分割法的主要潜在问题:(。

qq_43583311的博客 1171

B - The Suspects (并查集裸题)

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to s...

cherish_lin的博客 316

Unreal Engine鸿蒙移植实战:环境配置、构建流程与性能优化指南

游戏引擎移植是跨平台开发中的关键技术环节,其核心原理在于通过适配目标操作系统的底层接口和工具链,实现同一套代码在不同硬件和软件环境下的高效运行。这项技术的价值在于最大化代码复用率,降低多平台维护成本,并能让应用快速触达新兴生态系统的用户。在移动游戏和XR应用领域,随着鸿蒙HarmonyOS生态的快速发展,将成熟的商业引擎如Unreal Engine迁移至此平台,成为开发者探索新市场机会的重要路径。本文聚焦于Unreal Engine项目向鸿蒙系统的移植实践,详细解析了从环境搭建、源码适配到构建打包的全链路流

weixin_29011395的博客 404

使用再生龙还原镜像文件到硬盘

1. 把制作好的再生龙光盘插入需要的电脑,开机按F4使用再生龙启动2. 选择默认的:Clonezailla live(Default settings,VGA 800*600),然后等待几分钟 3. 选择语言:简体中文 4. 选择不修改键盘映射 5. 选择使用再生龙          6. 选择硬盘/分区(存到/来自)镜像文件,此选项既可以制作镜像,也可以恢复镜像。 7. 使用本机的分区     ...

weixin_42168893的博客 1万+

爆刷PAT(甲级)——之【1118】 Birds in Forest(25 分)——并查集赤裸模板题

补发一遍。   题意:好几张图画,每张图画上面的鸟,都在一棵树上。给你好几幅图的鸟,根据询问回答两只鸟在不在一颗树上。 难点:并查集,还是裸的并查集,直接跑就行了。 Code: #include&lt;bits/stdc++.h&gt; using namespace std; #define inf 10009 #define INF 0x3f3f3f3f int father[i...

Hide_in_Code 480

YOLOv11改进 | 细节涨点篇 | DySample一种最新的轻量化动态上采样算子(效果完爆CARAFE)

本文给大家带来的最新改进机制是一种号称超轻量级且有效的动态上采样器——DySample。与传统的基于内核的动态上采样器相比,DySample采用了一种基于点采样的方法,相比于以前的基于内核的动态上采样器,DySample具有更少的参数、浮点运算次数、GPU内存和延迟。此外,DySample在包括语义分割、目标检测、实例分割、全景分割和单目深度估计在内的五个预测任务中,性能均优于其他上采样器(截至目前最有效的上采样算子),

Snu77的博客 4209

数据结构 —— 并查集(笔记及裸题)

并查集 代码短但是特别精巧。 将两个集合合并 询问两个元素是否在一个集合当中(并查集的时间复杂度近乎O(1) ) 基本原理:每一个集合用一颗树来表示。树根的编号就是整个集合的编号。每个节点存储它的父节点,p[x]表示x的父节点 问题1:如何判断树根? 如果是树根的话 → if(p[x] == x) ,除了根节点之外p[x] ≠ x 问题2:如何求x的集合编号? while(p[x] != x) x = p[x];只要x不是树根,就一直往上走。 其实就是解决第二点,询问两个元素是否在一个集合当中,分别

ACM-ICPC 现役(铜牌) Codeforces Rating: 2172 361

【图像去噪】论文复现:降维打击!图像对输入变成像素对输入!Pytorch实现Noise2Void(N2V),基于U-Net模型训练,简洁明了理解N2V核心思想!附训练好的灰度图和RGB图的模型文件!

论文题目:Noise2Void-Learning Denoising from Single Noisy Images —— Noise2Void -从单个噪声图像中学习去噪【图像去噪】论文精读:Noise2Void-Learning Denoising from Single Noisy Images(N2V)由于源码是tensorflow版本,本文将基于Pytorch复现。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 1355

315、弱电基础知识介绍

常见的弱电系统包括:闭路电视监控系统、防盗报警系统、门禁系统、电子巡更系统、停车场管理系统、可视对讲系统、家庭智能化系统及安防系统、背景音乐系 统、LED显示系统、等离子拼接屏系统、DLP大屏系统、三表抄送系统、楼宇自控系统、防雷与接地系统、寻呼对讲及专业对讲系统、弱电管道系统、UPS不 间断电源系统、机房系统、综合布线系统、计算机局域网系统、物业管理系统、多功能会议室系统、有线电视系统、卫星电视系统、卫星通讯系统、消防系统、电话 通讯系统、酒店管理系统、视频点播系统、人力资源管理系统等等。

qq_43416206的博客 1365

Hdoj 1213 How Many Tables 【并查集】

How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16590 Accepted Submission(s): 8117Problem DescriptionToday is Ignatius’ bir

shengweisong 994

【Unity程序技巧】Unity中单例模式管理器

单例模式(Singleton Pattern)是 最简单的设计模式之一

秩沅 1657

hdoj 1213 How Many Tables 【并查集】

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18124    Accepted Submission(s): 8927 Problem Description Today is Igna

longge33445的博客 405

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

并查集——HDOJ 1213How Many Tables解题报告

Problem Description 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 s

plsxysam的专栏 411

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

hdoj 1213 How Many Tables

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15314    Accepted Submission(s): 7501 Problem Description Today is Ig

世界很大 488

HDOJ1213 How Many Tables(并查集)

Problem Description 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 fri

GKHack's Blog 975

Hdoj 1213.How Many Tables 题解

Problem Description 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 f...

优快云2082的博客 251

HDOJ 1213 How Many Tables(并查集)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26139    Accepted Submission(s): 13001 Problem Description Today is Ign

想飞的小菜鸡 221

hdoj1213How Many Tables(并查集)

Problem DescriptionToday 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

菜鸟的自我修养 355

HDOJ 1213 How Many Tables【并查集】

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24264    Accepted Submission(s): 12126 Problem Description Toda

※夏日星空※ 273

HDOJ1213. How many tables(并查集连通块计数)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33891    Accepted Submission(s): 16955 Problem Description Today

cr496352127的博客 396

hdoj1213How Many Tables

Problem Description 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 frien

WOLF的博客 360

HDU 1213 How Many Tables (并查集,连通分支数,两种方式)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23012    Accepted Submission(s): 11485 Problem Description Today is

AC_Dreameng 4240

HDOJ 1213.How Many Tables

How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16221 Accepted Submission(s): 7955 Problem Description Today is Ignati

I surrender 444
上一篇: HDOJ1272 并查集简单应用
下一篇: HDOJ1856 并查集水题
罗侯
博客等级 码龄10年 135粉丝 258原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值