poj1703 Find them, Catch them
并查集。
这题错了不少次才过的。
分析见代码。
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的更多相关文章
- poj1703 Find them, Catch them 并查集
poj(1703) Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26992 ...
- poj1703 Find them, Catch them(并查集)
https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...
- poj1703 Find them, Catch them(带权并查集)
题目链接 http://poj.org/problem?id=1703 题意 有两个帮派:龙帮和蛇帮,两个帮派共有n个人(编号1~n),输入m组数据,每组数据为D [a][b]或A [a][b],D[ ...
- poj1703 Find them, Catch them(并查集的应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32225 Accepte ...
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- poj1703 Find them,Catch them 【并查集】
做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...
- poj1703 Find them, Catch them(种类并查集
题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...
- 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 ...
- POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集
题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...
随机推荐
- 学习jsp(3)
HttpServletRequest和HttpServletResponse: response.setContentType("text/html;charset=UTF-8") ...
- Leetcode: Palindrome Pairs
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...
- spark-submit常用参数
yarn模式默认启动2个executor,无论你有多少的worker节点 standalone模式每个worker一个executor,无法修改executor的数量 partition是RDD中的一 ...
- java数组转化成集合
package com.shb.web; import java.util.Arrays;import java.util.List; import com.sun.xml.internal.ws.u ...
- dtree的使用和扩展
相信用过dtree的童靴的不在少数,网络上流传的JS树有很多,例如雪花树MzTreeView,EXT.Struts2出来之后,也有自己的树控件,但是这么多风姿卓约的倩影中,我独爱,独爱dtree那一棵 ...
- access调用联系
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; us ...
- 【转】The Attached Behavior Pattern
原文:http://www.bjoernrochel.de/2009/08/19/the-attached-behavior-pattern/ The Attached Behavior Patter ...
- 关于 屏幕阅读器 和 sr-only
.sr-only = screen reader only 用于读屏器识别使用.
- 作为WEB工程师,我们是不是应该积极的推进一下用户浏览器的使用体验?
为什么会写这篇文章,其实是有原因的.目前我工作的公司的Web网站仅支持IE8以上的版本,然后我们经常接到客户的反馈,说为什么在他浏览器当中flash怎么显示不了,或者为什么在他浏览器中有这样那样的问题 ...
- iOS 学习笔记 六 (2015.03.28)常见错误
2015.03.28 1. property's synthesized getter follows Cocoa naming convention for returning 'owned' ob ...