题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1213

题意

给出N个人 M对关系 找出共有几对连通块

思路

并查集

AC代码

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <ctype.h>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <climits>
  7. #include <ctime>
  8. #include <iostream>
  9. #include <algorithm>
  10. #include <deque>
  11. #include <vector>
  12. #include <queue>
  13. #include <string>
  14. #include <map>
  15. #include <stack>
  16. #include <set>
  17. #include <numeric>
  18. #include <sstream>
  19. #include <iomanip>
  20. #include <limits>
  21. #define CLR(a, b) memset(a, (b), sizeof(a))
  22. #define pb push_back
  23. using namespace std;
  24. typedef long long ll;
  25. typedef long double ld;
  26. typedef unsigned long long ull;
  27. typedef pair <int, int> pii;
  28. typedef pair <ll, ll> pll;
  29. typedef pair<string, int> psi;
  30. typedef pair<string, string> pss;
  31. const double PI = acos(-1.0);
  32. const double E = exp(1.0);
  33. const double eps = 1e-30;
  34. const int INF = 0x3f3f3f3f;
  35. const int maxn = 5e4 + 10;
  36. const int MOD = 1e9 + 7;
  37. int pre[maxn];
  38. int find(int x)
  39. {
  40. int r = x;
  41. while (pre[r] != r)
  42. r = pre[r];
  43. int i = x, j;
  44. while (i != r)
  45. {
  46. j = pre[i];
  47. pre[i] = r;
  48. i = j;
  49. }
  50. return r;
  51. }
  52. void join(int x, int y)
  53. {
  54. int fx = find(x), fy = find(y);
  55. if (x != fy)
  56. pre[fx] = fy;
  57. }
  58. void init()
  59. {
  60. for (int i = 0; i < maxn; i++)
  61. pre[i] = i;
  62. }
  63. int main()
  64. {
  65. int t;
  66. cin >> t;
  67. while (t--)
  68. {
  69. init();
  70. int n, m;
  71. scanf("%d%d", &n, &m);
  72. int x, y;
  73. for (int i = 0; i < m; i++)
  74. {
  75. scanf("%d%d", &x, &y);
  76. join(x, y);
  77. }
  78. map <int, int> M;
  79. for (int i = 1; i <= n; i++)
  80. M[find(i)]++;
  81. cout << M.size() << endl;
  82. }
  83. }

HDU - 1213 How Many Tables 【并查集】的更多相关文章

  1. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  2. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  3. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

  4. HDU 1213 How Many Tables (并查集,常规)

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  5. HDU 1213 How Many Tables 并查集 寻找不同集合的个数

    题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...

  6. hdu 1213 求连通分量(并查集模板题)

    求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...

  7. HDU 1213 How Many Tables(模板——并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...

  8. HDU 1213 How Many Tables(并查集模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...

  9. HDU 1213 How Many Tables (并查集)

    How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...

随机推荐

  1. ubuntu归档管理器消失了?

    Ubuntu - gnome 下的归档管理器软件名称:    file-roller如果没有看看是不是这个包让你删除掉了?用这个命令查: dpkg -l | grep file-roller 如果有这 ...

  2. ubuntu 添加用户

    sudo useradd 用户名 创建用户 sudo passwd 用户名 修改用户密码 添加sudo权限 sudo usermod -aG sudo(要添加的用户组,也可以是root) 用户名 su ...

  3. 关于Web项目的pom文件处理

    pom文件的方式需要修改的是 <packaging>war</packaging> <profiles> <profile> <id>com ...

  4. python 制作wordcloud词云

    pip install wordcloud 需要用到numpy  pillow matplotlib 安装完成以后 wordcloud_cli --text in.txt --imagefile ou ...

  5. CentOS下配置iptables防火墙 linux NAT(iptables)配置

    CentOS下配置防火墙 配置nat转发服务CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/syscon ...

  6. 【Web API系列教程】1.2 — Web API 2中的Action Results

    前言 本节的主题是ASP.NET Web API怎样将控制器动作的返回值转换成HTTP的响应消息. Web API控制器动作能够返回下列的不论什么值: 1. void 2. HttpResponseM ...

  7. 转 FreeBSD 安装JDK

    cd /usr/ports/java/openjdk6make install clean 默认什么都不用选,因为我们配置的是运行环境, 中间编译过程好久... 偷懒的干脆就直接安装/usr/port ...

  8. NoSQL数据库的分类

  9. java中Executor、ExecutorService、ThreadPoolExecutor介绍

    源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /**      * Executes the given c ...

  10. 02-cookie案例-显示用户上次访问网站的时间

    package cookie; import java.io.IOException;import java.io.PrintWriter;import java.util.Date; import ...