题目链接

这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解。

 #include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int o[];
int flag[];
int find(int x)
{
if (x == o[x]) return x;
int t = find(o[x]);
flag[x] = (flag[o[x]] + flag[x]) % ;
return o[x] = t;
}
void merge(int x,int y)
{
int tx,ty;
tx = find(x);
ty = find(y);
if(tx != ty)
{
o[tx] = ty;
flag[tx] = (flag[x]+flag[y]+)%;
}
}
int judge(int x,int y)
{
int tx,ty;
tx = find(x);
ty = find(y);
if(tx != ty)
return -;
else
return (flag[x] == flag[y]);
}
int main()
{
int T,sv,ev,i,n,m;
char ch;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%*c",&n,&m);
for(i = ;i <= n;i ++)
{
flag[i] = ;
o[i] = i;
}
for(i = ;i < m;i ++)
{
scanf("%c%*c%d%d%*c",&ch,&sv,&ev);
if(ch == 'A')
{
int temp = judge(sv,ev);
if(temp == -)
printf("Not sure yet.\n");
else if(temp == )
printf("In different gangs.\n");
else
printf("In the same gang.\n");
}
else
{
merge(sv,ev);
}
}
}
return ;
}

POJ 1703 Find them, Catch them(种类并查集)的更多相关文章

  1. POJ 1703 Find them,Catch them ----种类并查集(经典)

    http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...

  2. poj 1703 Find them, Catch them 【并查集 新写法的思路】

    题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...

  3. poj 1703 Find them, Catch them(并查集)

    题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...

  4. POJ 1703 Find them, Catch them (并查集)

    题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派. 思路: 解法一: 编号划分 定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素 ...

  5. 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 ...

  6. POJ 1703 Find them, Catch them(并查集,等价关系)

    DisjointSet保存的是等价关系,对于某个人X,设置两个变量Xa,Xb.Xa表示X属于a帮派,Xb类似. 如果X和Y不是同一个帮派,那么Xa -> Yb,Yb -> Xa... (X ...

  7. POJ1703Find them, Catch them[种类并查集]

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

  8. [poj1703]Find them, Catch them(种类并查集)

    题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...

  9. POJ:1703-Find them, Catch them(并查集好题)(种类并查集)

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

随机推荐

  1. hlgoj 1766 Cubing

    模拟.下图是我做的小模型. #include <iostream> #include <stdio.h> #include <queue> #include < ...

  2. EXPDP/IMPDP任务的查看与管理

    EXPDP/IMPDP相比传统的exp/imp的最本质区别在于服务器端执行,客户端发出指定后,通过API启动服务器的备份job,在执行过程中,可以拿下Ctrl+C组合键,退出当前交互模式,退出之后,导 ...

  3. POJ 1006 生理周期【数论】

    这题是有中文版的(右上角选项卡里把default改成简体中文)然后看到他把biorhythms翻成生理周期我可耻的笑了......23333 如果没有限定从日期d开始,完全可以从第一天起开始计时,因此 ...

  4. JMS API学习总结(一)

    三.JMS API简析 顶级接口 P2P Pub/sub 备注 ConnectionFactory QueueConnectionFactory TopicConnectionFactory 基于工厂 ...

  5. Linux(13):期中架构(5)--- 前端部分:keepalived高可用 & HTTPS & iptables防火墙

    keepalived 高可用集群 1. keepalived服务概念说明 # 1.1 keepalived软件的作用? Keepalived软件起初是专为LVS负载均衡软件设计的, 用来管理并监控LV ...

  6. Python脚本实现单据体背景色及字段前景色设置

    #PythonEntitySetColor clr.AddReference('Kingdee.BOS.Core') from Kingdee.BOS.Core.DynamicForm.PlugIn. ...

  7. android开发里跳过的坑——TimePickerDialog onTimeSet不回调

    在android6.0.1上测试发现TimePickerDialog的onTimeSet和DatePickerDialog的onDateSet不回调,查看SDK源码发现,TimePickerDialo ...

  8. ElasticSearch 多索引

    1.用逗号将索引隔开,如: $ curl -XPOST http://localhost:9200/aaa,website/_search/ { "took": 1, " ...

  9. CPU 和内存虚拟化原理

    前面我们成功地把 KVM 跑起来了,有了些感性认识,这个对于初学者非常重要.不过还不够,我们多少得了解一些 KVM 的实现机制,这对以后的工作会有帮助. CPU 虚拟化 KVM 的虚拟化是需要 CPU ...

  10. 巴蜀1088 Antiprime数

    Description 如果一个自然数n(n>=1),满足所有小于n的自然数(>=1)的约数个数都小于n的约数个数,则n是一个Antiprime数.譬如:1, 2, 4, 6, 12, 2 ...