并查集。

这题错了不少次才过的。

分析见代码。

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. iOS缓存使用的框架

    MagicalRecord  FMDB 都可以在gitHub上找到

  2. Lintcode: Kth Smallest Number in Sorted Matrix

    Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...

  3. ORACLE数据库创建用户名和表空间

    [sql] /*第1步:登录  */  以sys/sys超级用户登录pl/sql      /*第2步:创建临时表空间  */  create temporary tablespace user_te ...

  4. 20145207 《Java程序设计》第4周学习总结

    前言 又到了大家最喜欢的前言时间,哈哈哈.我这个人啊,就是比较爱闲聊.正式在学校呆的第一天时间就在这里敲代码,自己都觉得自己伟大.不过好无聊呀....这周的内容说实话讲我还是挺感兴趣的,因为书上的例子 ...

  5. Java基础(48):归并排序的Java封装含原理,完整可运行,结合VisualGo网站更好理解)

    原理: 归并排序建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用. 将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序, ...

  6. 反射认识_04_反射调用类方法Method

    包1: package ReflectionMethod; public class ReflectionMethod { String str1="str1"; public v ...

  7. Android中使用SurfaceView+MediaPlayer+自定义的MediaController实现自定义的视屏播放器

    效果图如下: (PS本来是要给大家穿gif动态图的,无奈太大了,没法上传) 功能实现:暂停,播放,快进,快退,全屏,退出全屏,等基本功能 实现的思路: 在主布局中放置一个SurfaceView,在Su ...

  8. linux下创建的符号链接的权限

    今天为shell脚本创建符号链接的时候突然发现的, 创建的符号链接文件的默认权限是 777, 而想要这个脚本可执行, 必须为真正的文件加上 x 权限才可以.

  9. NOIP201205Vigenère密码

                                   NOIP201205Vigenère密码 [问题描述]   16 世纪法国外交家Blaise de Vigenère设计了一种多表密码加密 ...

  10. linux设备驱动归纳总结(十一):写个简单的看门狗驱动【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-112879.html linux设备驱动归纳总结(十一):写个简单的看门狗驱动 xxxxxxxxxxx ...