HDU 1213 How Many Tables(模板——并查集)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1213
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 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.
#include<stdio.h>
void merge(int u,int v);
int getf(int v);
int f[];
int main()
{
int T,n,m,a,b,i,sum;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
f[i]=i;
while(m--)
{
scanf("%d%d",&a,&b);
merge(a,b);
} for(sum=,i=;i<=n;i++)
{
if(f[i]==i)
sum++;
}
printf("%d\n",sum);
}
return ;
}
void merge(int u,int v)
{
int t1,t2;
t1=getf(u);
t2=getf(v);
if(t1 != t2)
f[t2]=t1;
}
int getf(int v)
{
if(f[v]==v)
return v; return f[v]=getf(f[v]);
}
HDU 1213 How Many Tables(模板——并查集)的更多相关文章
- HDU 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
- HDU - 1213 How Many Tables 【并查集】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- hdu 1213 How Many Tables(并查集练习)
题目链接:hdu1213 赤裸裸的并查集.....水题一个.... #include<stdio.h> #include<string.h> #include<algor ...
- HDU 1213 How Many Tables(并查集)
传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...
- HDU 1213 How Many Tables(并查集裸题)
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- HDU 1213 How Many Tables【并查集】
解题思路:和畅通工程类似,问最后还剩下几个不连通的区域. How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- hdu 1213 How Many Tables(并查集求无向图有几个连通分量)
代码: #include<cstdio> #include<cstring> using namespace std; int n,m; int father[1005]; i ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
随机推荐
- Nodejs进阶:crypto模块中你需要掌握的安全基础
本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址. 一. 文章概述 互联网时代,网络上的数据量每天都在以惊人的速度增长.同时,各类网络安全问题层出不穷.在信 ...
- vi 和vim 的区别
它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面.vim的这些优势主要体现在以下几个方面:1.多级撤消我们知道在vi里,按 u只能撤消上次命 ...
- gitlab 本地 定时备份
=============================================== 20171015_第1次修改 ccb_warlock === ...
- 正确使用volatile场景--状态标志
同步机制:volatile 特点:可见性:不具备原子性 每个线程有自己单独的内存:如果线程1和线程2公用一个变量name:如果两个线程并发进行,并且需要访问变量name:如果这个变量具有了可见性,线程 ...
- ADB 安卓开发配置环境
下载完后将名称中含有adb的文件,和fastboot.exe复制到 c:/windows/system32目录 或c:/windows/system64目录(看自己电脑系统配置 如电脑64操作系统就写 ...
- C# System.Windows.Forms.NumericUpDown 控件全选其中文字
num_length.Focus(); UpDownBase updbText = (UpDownBase)num_length; ...
- CentOS7源码安装lamp
环境介绍 虚拟机 : VMware Workstation 14 Pro 镜像 : CentOS Linux release 7.4.1708 (Core) 物理机 : windows 7 64位 防 ...
- linux系统常见压缩命令
在linux环境中,压缩文件的扩展名基本是:*.tar,*.tar.gz,*.tgz,*.gz,*.Z,*.bz2 *.Z compress程序压缩的文件 *.gz gzip程序压缩的文件 *.bz2 ...
- Jenkins 学习笔记(一):我对 Jenkins 的宏观认识
Jenkins 是一个持续构建的系统,通过一周的了解熟悉,其逻辑似乎很简单. Jenkins 拓扑 Jenkins 逻辑 1. 从代码库拉取代码. 2. 处理代码. 对于需要编译的程序,需要进行处理, ...
- git上传文件到github
一.git之上传代码到github. 安装git,这个就不说了,很多帖子都有详细说明. 二.新建仓库,GitHub上的,首先申请账号. 三.本地选择地方新建本地仓库. 建完本地仓库文件夹,在本地 ...