题目链接:

K - Find them, Catch them

POJ - 1703

题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人。该城有N个罪犯,编号从1至N(N<=100000。将有M(M<=100000)次操作。
D a b 表示a、b是不同帮派
A a b 询问a、b关系。

具体思路:带权并查集模板题。一般并查集都是相同的放在一个联通块里面。对于这个题,我们可以利用一下这个性质,只要是有联系的,都放进一个连通块里面,然后我们查询的时候,如果说他们的祖先一样的话,就首先能够保证是有关系的。然后就开始判断是敌对关系还是朋友关系。我们再加一个数组r[x],表示x和他的祖先的关系,如果r[x]=1代表和祖先是敌对关系,否则就是朋友关系。然后在寻找路径的过程中,不停地更新就可以了。

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<map>
  4. using namespace std;
  5. # define ll long long
  6. # define inf 0x3f3f3f3f
  7. const int maxn = 2e5+;
  8. int father[maxn];
  9. int r[maxn];
  10. int Find(int t)
  11. {
  12. if(t==father[t])
  13. return t;
  14. int tmp=father[t];
  15. father[t]=Find(father[t]);
  16. r[t]=(r[tmp]+r[t])%;
  17. return father[t];
  18. }
  19. void match(int t1,int t2)
  20. {
  21. int s1=Find(t1);
  22. int s2=Find(t2);
  23. father[s1]=s2;
  24. r[s1]=(r[t1]+r[t2]+)%;//t1和t2是敌对关系
  25. }
  26. int main()
  27. {
  28. int T;
  29. scanf("%d",&T);
  30. while(T--)
  31. {
  32. int n,m;
  33. scanf("%d %d",&n,&m);
  34. for(int i=; i<=n; i++)
  35. {
  36. father[i]=i;
  37. r[i]=;
  38. }
  39. char str[];
  40. int st,ed;
  41. while(m--)
  42. {
  43. scanf("%s",str);
  44. if(str[]=='A')
  45. {
  46. scanf("%d %d",&st,&ed);
  47. if(Find(st)==Find(ed))
  48. {
  49. if(r[st]==r[ed])
  50. {
  51. printf("In the same gang.\n");
  52. }
  53. else
  54. printf("In different gangs.\n");
  55. }
  56. else
  57. {
  58. printf("Not sure yet.\n");
  59. }
  60. }
  61. else
  62. {
  63. scanf("%d %d",&st,&ed);
  64. match(st,ed);
  65. }
  66. }
  67. return ;
  68. }
  69. }

K - Find them, Catch them POJ - 1703 (带权并查集)的更多相关文章

  1. POJ 1703 带权并查集

    直接解释输入了: 第一行cases. 然后是n和m代表有n个人,m个操作 给你两个空的集合 每个操作后面跟着俩数 D操作是说这俩数不在一个集合里. A操作问这俩数什么关系 不能确定:输出Not sur ...

  2. POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】

      The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...

  3. poj 1182 (带权并查集)

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

  4. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  5. Navigation Nightmare POJ - 1984 带权并查集

    #include<iostream> #include<cmath> #include<algorithm> using namespace std; ; // 东 ...

  6. Parity game POJ - 1733 带权并查集

    #include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...

  7. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  8. (中等) POJ 1703 Find them, Catch them,带权并查集。

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

  9. poj 1703 - Find them, Catch them【带权并查集】

    <题目链接> 题目大意: 已知所有元素要么属于第一个集合,要么属于第二个集合,给出两种操作.第一种是D a b,表示a,b两个元素不在一个集合里面.第二种操作是A a b,表示询问a,b两 ...

随机推荐

  1. codeforces472B

    Design Tutorial: Learn from Life CodeForces - 472B One way to create a task is to learn from life. Y ...

  2. Linux 4.20内核得到更新,英特尔CPU 性能降低50%

    根据HKEPC的报道,Linux近日发布了 4.20 内核的一些漏洞修复更新,更新后可能会出现50% 的性能损失,是今年内所有安装Spectre/Meltdown 修补程式中效能跌幅最大的一次. 据报 ...

  3. List泛型集合

    List和数组 相同点: 都可以控制元素类型 不同点: List的长度是可变的,所以list比数组更容易掌控 List属性 1.Count 获取集合中实际包含的元素个数 2.Capcity 集合中可以 ...

  4. webapi返回泛型给easyui

    由于之前遇到的easyui调用webapi的问题. 参见 :http://blog.csdn.net/hanjun0612/article/details/51144991 所以就考虑,封装一个泛型用 ...

  5. CodeForces 464E The Classic Problem | 呆克斯歘 主席树维护高精度

    题意描述 有一个\(n\)点\(m\)边的无向图,第\(i\)条边的边权是\(2^{a_i}\).求点\(s\)到点\(t\)的最短路长度(对\(10^9 + 7\)取模). 题解 思路很简单--用主 ...

  6. Python--Django学习笔记1

    Django:The Web framework for perfectionists with deadlines Django是一个Python语言开发的高级Web框架,采用MVC架构,通过配置可 ...

  7. AutoCompleteTextView 简单用法 实现自定义list adapter

    网上有不少教程,那个提示框字符集都是事先写好的,例如用一个String[] 数组去包含了这些数据,但是,我们也可以吧用户输入的作为历史记录保存       下面先上我写的代码:import andro ...

  8. 【POJ1151】Atlantis

    题目大意:给定 N 个矩形,求这些矩形的面积并. 题解:采用扫描线算法. 首先,按照矩形的横坐标排序,在纵坐标方向上维护一根扫描线被覆盖的长度,在这里采用线段树维护.统计答案时,从左到右扫描 2N 个 ...

  9. 简单认识python(一)

    最近本宝宝被一部小说迷的神魂颠倒的,在网络上四处找免费的小说资源,一直哭唧唧的等待着每天更新的一章.实在是太可怜了,本宝宝决定自己学python,自己抓包小说. 既然知道目的地了,那就和本宝宝一起打怪 ...

  10. es6中的函数

    ES6 允许为函数的参数设置默认值,即直接写在参数定义的后面. function log(x, y = 'World') { console.log(x, y); } log('Hello') // ...