HDU - 1213 How Many Tables 【并查集】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1213
题意
给出N个人 M对关系 找出共有几对连通块
思路
并查集
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30;
const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 10;
const int MOD = 1e9 + 7;
int pre[maxn];
int find(int x)
{
int r = x;
while (pre[r] != r)
r = pre[r];
int i = x, j;
while (i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
}
void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (x != fy)
pre[fx] = fy;
}
void init()
{
for (int i = 0; i < maxn; i++)
pre[i] = i;
}
int main()
{
int t;
cin >> t;
while (t--)
{
init();
int n, m;
scanf("%d%d", &n, &m);
int x, y;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
join(x, y);
}
map <int, int> M;
for (int i = 1; i <= n; i++)
M[find(i)]++;
cout << M.size() << endl;
}
}
HDU - 1213 How Many Tables 【并查集】的更多相关文章
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- HDU 1213 How Many Tables(并查集,简单)
题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...
- HDU 1213 How Many Tables (并查集,常规)
并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...
- HDU 1213 How Many Tables 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- hdu 1213 求连通分量(并查集模板题)
求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- 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 (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
随机推荐
- ubuntu归档管理器消失了?
Ubuntu - gnome 下的归档管理器软件名称: file-roller如果没有看看是不是这个包让你删除掉了?用这个命令查: dpkg -l | grep file-roller 如果有这 ...
- ubuntu 添加用户
sudo useradd 用户名 创建用户 sudo passwd 用户名 修改用户密码 添加sudo权限 sudo usermod -aG sudo(要添加的用户组,也可以是root) 用户名 su ...
- 关于Web项目的pom文件处理
pom文件的方式需要修改的是 <packaging>war</packaging> <profiles> <profile> <id>com ...
- python 制作wordcloud词云
pip install wordcloud 需要用到numpy pillow matplotlib 安装完成以后 wordcloud_cli --text in.txt --imagefile ou ...
- CentOS下配置iptables防火墙 linux NAT(iptables)配置
CentOS下配置防火墙 配置nat转发服务CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/syscon ...
- 【Web API系列教程】1.2 — Web API 2中的Action Results
前言 本节的主题是ASP.NET Web API怎样将控制器动作的返回值转换成HTTP的响应消息. Web API控制器动作能够返回下列的不论什么值: 1. void 2. HttpResponseM ...
- 转 FreeBSD 安装JDK
cd /usr/ports/java/openjdk6make install clean 默认什么都不用选,因为我们配置的是运行环境, 中间编译过程好久... 偷懒的干脆就直接安装/usr/port ...
- NoSQL数据库的分类
- java中Executor、ExecutorService、ThreadPoolExecutor介绍
源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /** * Executes the given c ...
- 02-cookie案例-显示用户上次访问网站的时间
package cookie; import java.io.IOException;import java.io.PrintWriter;import java.util.Date; import ...