并查集。

这题错了不少次才过的。

分析见代码。

http://poj.org/problem?id=1703

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + ;
const char str1[] = "Not sure yet.";
const char str2[] = "In different gangs.";
const char str3[] = "In the same gang.";
int fa[maxn], belong[maxn], nex[maxn];
int n, m, u, v, k;
char op; int father(int u){
//WHAT WOULD CAUSE AN INFINITE LOOP
if(fa[u] == -) return u;
int tem = father(fa[u]);
belong[u] = belong[tem];
fa[u] = tem;
return tem;
} void solve(){
if(op == 'D'){
if(belong[u] == - && belong[v] == -){
//this is the simplest case
belong[u] = k++;
belong[v] = k++;
nex[u] = v;
nex[v] = u;
return;
}
if(belong[u] != - && belong[v] == -){
//notice that v is never mentioned, but u is already processed
//since is u is vistied, u got its partner
int fu = father(u), fu1 = father(nex[u]);
//draw a line from v to u1
fa[v] = fu1;
nex[v] = fu;
belong[v] = belong[fu1];
return;
//match a virtue partner for node u
}
if(belong[u] == - && belong[v] != -){
int fv = father(v), fv1 = father(nex[v]);
fa[u] = fv1;
nex[u] = fv;
belong[u] = belong[fv1];
return;
}
if(belong[u] != - && belong[v] != -){
//notice that both u and v is already visited
int fu = father(u), fu1 = father(nex[u]);
int fv = father(v), fv1 = father(nex[v]);
int bu = belong[fu] >> ;
int bv = belong[fv] >> ;
if(bu > bv){
// v is relatively primitive
fa[fu] = fv1;
fa[fu1] = fv;
return;
}
if(bu < bv){
fa[fv] = fu1;
fa[fv1] = fu;
return;
}
}
}
if(op == 'A'){
int fu = father(u);
int fv = father(v);
if(belong[fu] == - || belong[fv] == -) puts(str1);
else if(belong[fu] == belong[fv]) puts(str3);
else if(belong[fu] == ( ^ belong[fv])) puts(str2);
else puts(str1);
}
} int main(){
freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
memset(belong, -, sizeof belong);
memset(fa, -, sizeof fa);
k = ;
for(int i = ; i < m; i++){
scanf(" %c%d%d", &op, &u, &v);
solve();
}
}
}

poj1703 Find them, Catch them的更多相关文章

  1. poj1703 Find them, Catch them 并查集

    poj(1703) Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26992   ...

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

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

  3. poj1703 Find them, Catch them(带权并查集)

    题目链接 http://poj.org/problem?id=1703 题意 有两个帮派:龙帮和蛇帮,两个帮派共有n个人(编号1~n),输入m组数据,每组数据为D [a][b]或A [a][b],D[ ...

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

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

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

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

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

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

  7. poj1703 Find them, Catch them(种类并查集

    题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...

  8. 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 th ...

  9. POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集

    题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...

随机推荐

  1. 通用窗口类 Inventory Pro 2.1.2 Demo1(下续篇 ),物品消耗扇形显示功能

    本篇想总结的是Inventory Pro中通用窗口的具体实现,但还是要强调下该插件的重点还是装备系统而不是通用窗口系统,所以这里提到的通用窗口类其实是通用装备窗口类(其实该插件中也有非装备窗口比如No ...

  2. npm 介绍

    安装NPM NPM的全称是Node Package Manager,如果你熟悉ruby的gem,Python的PyPL.setuptools,PHP的pear,那么你就知道NPM的作用是什么了.没 错 ...

  3. C++Primer 第五章

    //1.表达式语句的作用:执行表达式并丢弃求值结果 ; value + ; //执行,并丢弃结果 //2.复合语句是指用花括号括起来的语句和声明的序列,复合语句称为块.一个块就是一个作用域.块不以分号 ...

  4. SQL 数据库 子查询、主外键

    子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这时它可以用在 ...

  5. android studio不能创建android项目,或者不能识别android项目的解决方法

    1.先点击file->close project回到android studio 欢迎界面,然后按如下图步骤添加相关的plugin就行了,然后重启. 2.如图 3. 4.

  6. 夺命雷公狗ThinkPHP项目之----企业网站16之文章列表页的完善(关联查询)

    我们栏目的所属栏目不能总是以数字来显示吧??这样的话,估计老板会让您直接卷铺盖滚蛋噢,嘻嘻... 所以我们需要对她进行关联查询,控制器代码如下所示: public function lists(){ ...

  7. SQl中drop与truncate的区别

    在对SQL的表操作时,我们因不同的需求做出相应的操作. 我来对比一下truncate table '表明'与drop table '表格名'的区别,跟大家一起学习. drop table '表格名'- ...

  8. 出现“不能执行已释放的Script代码”错误的原因及解决办法

    很多web开发者或许都遇到过这样的问题,程序莫名奇怪出现“不能执行已释放Script的代码”,错误行1,列1.对于这种消息描述不着边,行列描述更是让人迷茫的js错误,相信是所有调试js程序的朋友们最郁 ...

  9. android 项目学习随笔十五(ShareSDK开放平台)

    ShareSDK开放平台http://www.mob.com/#/

  10. Java随学随记

    1.一个Java源文件可包含三个“顶级”要素: (1)一个包(package)声明(可选) (2)任意数量的导入(import)语句 (3)类(class)声明 该三要素必须以上顺序出现.即,任何导入 ...