题目链接:http://poj.org/problem?id=1703

题意:两个坏蛋属于不同的组织,给出两个坏蛋判定是否一个组织。

题解:已知每次输入的两个帮派人员 x, y; 合并 (x, y + N), (x + N, y)。判定时,如果 (x, y) 属于同一个 根,就是同一个组织,(x, y+N) 属于同一个根,则说明是不同组织。不确定则无解。

  1. #include <iostream>
  2. using namespace std;
  3. const int maxn = * + ;
  4. int Rank[maxn], par[maxn];
  5. void solve();
  6. void init(const int& n) {
  7. for (int i = ; i < n; i++) {
  8. par[i] = i;
  9. Rank[i] = ;
  10. }
  11. }
  12. int find(const int& x) {
  13. if (par[x] == x) {
  14. return x;
  15. }
  16. else {
  17. return par[x] = find(par[x]);
  18. }
  19. }
  20. void unite(int x, int y)
  21. {
  22. x = find(x);
  23. y = find(y);
  24. if (x == y) return ;
  25. if (Rank[x] < Rank[y]) {
  26. par[x] = y;
  27. }
  28. else {
  29. par[y] = x;
  30. if (Rank[x] == Rank[y]) {
  31. ++Rank[x];
  32. }
  33. }
  34. }
  35. bool same(int x , int y)
  36. {
  37. return find(x) == find(y);
  38. }
  39. void solve()
  40. {
  41. int T, N, M;
  42. int x, y;
  43. char cmd;
  44. scanf("%d", &T);
  45. while (T--)
  46. {
  47. scanf("%d%d", &N, &M);
  48. init(N * );
  49. getchar();
  50. while (M--)
  51. {
  52. //忽略每次输入后的回车符
  53. scanf("%c%d%d%*c", &cmd, &x, &y);
  54. //检查
  55. if (cmd == 'A') {
  56. if (same(x, y)) {
  57. printf("In the same gang.\n");
  58. }
  59. else if (same(x, y + N)) {
  60. printf("In different gangs.\n");
  61. }
  62. else {
  63. printf("Not sure yet.\n");
  64. }
  65. }
  66. else {
  67. //合并 (x, y+N) 或 (x+N, y)
  68. unite(x, y + N);
  69. unite(x + N, y);
  70. }
  71. }
  72. }
  73. }
  74. int main()
  75. {
  76. solve();
  77. return ;
  78. }

并查集:POJ No1703 Find them, Catch them的更多相关文章

  1. [并查集] POJ 1703 Find them, Catch them

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: ...

  2. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  3. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  4. [并查集] POJ 1182 食物链

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66294   Accepted: 19539 Description ...

  5. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  6. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

    题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...

  7. 并查集--poj 2492

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  8. 并查集 POJ 1988

    #include <cstdio> #define N 30010 int pa[N]; //parent int siz_tree[N]; //size of tree int d[N] ...

  9. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

随机推荐

  1. Spring下使用开发webservice

    依赖包 <!-- CXF Dependencies --> <dependency> <groupId>org.apache.cxf</groupId> ...

  2. JS计算两个日期之间的天数,时间差计算

    1.日期之间的天数计算 //计算天数差的函数,通用 function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2017-9-25格式 var aDate, ...

  3. 优先队列的一种实现--堆ADT

    二叉堆的两个重要性质: 1.结构性,为完全二叉树,可以用数组方便地表示.2.堆序性:树中每个节点的关键字值大于或等于其父节点的关键字值. 二叉堆的数据结构声明如下: struct HeapStruct ...

  4. Python入门:数据结构的3个小技巧

    这是关于Python的第11篇文章,主要介绍下数据结构的3个小技巧. 排序: 使用sorted函数实现排序. sorted函数按照长短.大小.英文字母的顺序给每个列表的元素进行排序.这个函数经常在数据 ...

  5. yii 验证码 CCaptcha的总结(转)

    今天用到yii的验证码 ccaptcha,经过在网上搜寻 找到以下例子: 1.在controller中加入代码 (1)启用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 &l ...

  6. soap 简单的例子

    首先确保你的soap模块开启 客户端代码 <?php try { $client = new SoapClient(null, array('location' =>"http: ...

  7. 13个实用的Linux find命令示例

    除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作,使你的命令行之旅更加简易. 本文将介绍15种无论是于新手还是老鸟都非常有用的Linux find命令. 首先,在你 ...

  8. sublimeText3的一些操作记录

    # 给绿色版的sublimeText3添加右键菜单,其中@=“Sublime Text 3” 是右键展示的文字, 后面的icon是图标将下面代码保存为.reg文件执行 Windows Registry ...

  9. contextmap相当于session之类的 用于设置属性 投放到页面上 contextmap的数据存储在map中

    contextmap相当于session之类的 用于设置属性 投放到页面上

  10. 打开eclipse编译后的.class文件

    众所周知,用文本编辑器打开.class文件会乱码.我们可以使用命令行打开.class文件项目结构: 代码: public class Synchronized { public static void ...