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

可以认为n个人和m句话;

每句话包含A a b;D a b;

刚开始关系不确定;

A a b 就是问ab 是否同类;

D a b就是告诉我们ab不同类;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <algorithm>
using namespace std; const int oo = 0x3f3f3f3f;
const int N = ; int f[N], r[N];///r[i]代表f[i]与i的关系,1代表不同类,0代表同类; int Find(int x)
{
int k=f[x];
if(x!=f[x])
{
f[x] = Find(f[x]);
r[x] = (r[x]+r[k])%;
}
return f[x];
} int main()
{
int t;
scanf("%d", &t); while(t--)
{
char s[];
int n, m, i, a, b, fa, fb; scanf("%d%d", &n, &m); for(i=; i<=n; i++)
{
f[i] = i;
r[i] = ;
} for(i=; i<=m; i++)
{
scanf("%s%d%d", s, &a, &b); if(s[]=='A')
{ fa = Find(a);
fb = Find(b); if(fa!=fb)
printf("Not sure yet.\n");
else
{
if(r[a]==r[b])
printf("In the same gang.\n");
else
printf("In different gangs.\n");
} }
else
{
fa = Find(a);
fb = Find(b); if(fa!=fb)
{
f[fa] = fb;
r[fa] = (r[b] - r[a] + + )%;
}
}
}
}
return ;
}

Find them, Catch them---poj1703(并查集)的更多相关文章

  1. poj1703 Find them, Catch them(并查集)

    https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...

  2. poj1703 Find them, Catch them(并查集的应用)

    Find them, Catch them   Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32225   Accepte ...

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

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

  4. poj1703 Find them,Catch them 【并查集】

    做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...

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

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

  6. POJ_1703 Find them, Catch them 【并查集】

    一.题面 POJ1703 二.分析 需要将并查集与矢量法则相结合.par数组用以记录父节点,rank用以记录与父节点的关系.如题意,有两种关系,设定0是属于同一个帮派,1表示不属于同一个帮派. 运用并 ...

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

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

  8. poj1703Find them, Catch them(并查集以及路径压缩)

    /* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...

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

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

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

随机推荐

  1. linux下简单好用的端口映射转发工具rinetd 转

    linux下简单好用的工具rinetd,实现端口映射/转发/重定向 官网地址http://www.boutell.com/rinetd 软件下载 wget http://www.boutell.com ...

  2. Json.net 时间格式处理

    json.net转json时生成的时间格式是这种 2015-11-14T06:59:59+08:00 格式化为这种2015-11-14 后台代码: IsoDateTimeConverter timeF ...

  3. ueditor1_4_3_3编辑器的应用

    教程使用的是ueditor1_4_3_3版本. 首先到官网http://ueditor.baidu.com/website/download.html下载jsp  utf-8版 下载好以后,解压,把解 ...

  4. centos7 安装hadoop 集群遇到的问题

    集群安装之后,hdfs 不能上传文件,也提示rute等错误,其实是防火墙问题,关闭防火墙即可. CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙.firewa ...

  5. [转] Windows局域网通过NTP设置时间同步

    NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议. 如果局域网计算机(Windows系统)可以连接Internet,可以通过“控制面板”— ...

  6. kendo-ui的使用和开发自己的组件

    摘要: 前面介绍了一款非常不错的前端框架kendo-ui,如果你想阅读,请点这里.通过使用它一段时间,感觉是非常好用.下面就介绍一下如何使用它和开发自己的组件 引入: 只需要引进下面三个文件即可 ke ...

  7. php-fpm打开错误日志的配置

    nginx与apache不一样,在apache中可以直接指定php的错误日志,那样在php执行中的错误信息就直接输入到php的错误日志中,可以方便查询. 在nginx中事情就变成了这样:nginx只对 ...

  8. 【RF库Collections测试】Create Dictionary

    Name:Create DictionarySource:Collections <test library>Arguments:[ *key_value_pairs ]

  9. Linux ulimit 命令

    ulimit命令用来限制系统用户对 shell 资源的访问,常见用法如下: [root@localhost ~]$ ulimit -a # 查看当前所有的资源限制 [root@localhost ~] ...

  10. Android中的渐变

    LinearGradient的用法 LinearGradient linearGradient; linearGradient = new LinearGradient(0, 0, 0, getHei ...