并查集。

这题错了不少次才过的。

分析见代码。

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. mysql 常见的几个错误问题

    Mysql常见的几个错误问题及解决方法: 1.问题: mysql DNS反解:skip-name-resolve 错误日志有类似警告: 点击(此处)折叠或打开 120119 16:26:04 [War ...

  2. 如果类的属性是copy的NSString类型 用set方法拦截时

    @property (nonatomic, copy) NSString *badgeValue; - (void)setBadgeValue:(NSString *)badgeValue { _ba ...

  3. PostgreSQL auto_explain

    The auto_explain module provides a means for logging execution plans of slow statements automaticall ...

  4. volatile 用法

    volatile 影响编译器编译的结果,指出,volatile 变量是随时可能发生变化的,与volatile变量有关的运算,不要进行编译优化,以免出错,(VC++ 在产生release版可执行码时会进 ...

  5. PHP的基本语法

    PHP的基本语法和c#的基本语法是差不多的,在这里只和大家聊一下PHP和C#语法不同的地方. 首先 PHP和c#的标记方式不一样,PHP他是一门脚本语言,JS也是脚本语言,只不过JS是运行在客户端的, ...

  6. javascript 异常处理和事件处理

    异常捕获 1.异常:当javascript引擎执行JS代码时,发生了错误,导致程序停止运行. 2.异常抛出:当异常产生,并且将这个异常生成一个错误信息 3.异常捕获: try{发生异常的代码块:}ca ...

  7. Calculation控制台

    接口 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace s ...

  8. 将Excel导入数据库

    在Control 中: public ActionResult ImportExcel() { return View(); } //客户导入 [HttpPost] public ActionResu ...

  9. sql 登录注入

    DataTable dt= SqlHelper.ExecuteDataTable(System.Data.CommandType.Text, String.Format("select * ...

  10. AR 应收 表

    AR 应收 应收事务处理相关表 SELECT * FROM ar.ar_batches_all;                  --事务处理批 SELECT * FROM ar.ra_custom ...