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

题意:两个坏蛋属于不同的组织,给出两个坏蛋判定是否一个组织。

题解:已知每次输入的两个帮派人员 x, y; 合并 (x, y + N), (x + N, y)。判定时,如果 (x, y) 属于同一个 根,就是同一个组织,(x, y+N) 属于同一个根,则说明是不同组织。不确定则无解。

#include <iostream>
using namespace std; const int maxn = * + ;
int Rank[maxn], par[maxn];
void solve(); void init(const int& n) {
for (int i = ; i < n; i++) {
par[i] = i;
Rank[i] = ;
}
} int find(const int& x) {
if (par[x] == x) {
return x;
}
else {
return par[x] = find(par[x]);
}
} void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y) return ; if (Rank[x] < Rank[y]) {
par[x] = y;
}
else {
par[y] = x;
if (Rank[x] == Rank[y]) {
++Rank[x];
}
}
} bool same(int x , int y)
{
return find(x) == find(y);
} void solve()
{
int T, N, M;
int x, y; char cmd;
scanf("%d", &T); while (T--)
{
scanf("%d%d", &N, &M);
init(N * ); getchar(); while (M--)
{
//忽略每次输入后的回车符
scanf("%c%d%d%*c", &cmd, &x, &y);
//检查
if (cmd == 'A') {
if (same(x, y)) {
printf("In the same gang.\n");
}
else if (same(x, y + N)) {
printf("In different gangs.\n");
}
else {
printf("Not sure yet.\n");
}
}
else {
//合并 (x, y+N) 或 (x+N, y)
unite(x, y + N);
unite(x + N, y);
}
} } } int main()
{
solve();
return ;
}

并查集:POJ No1703 Find them, Catch them的更多相关文章

  1. [并查集] POJ 1703 Find them, Catch them

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

  2. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  3. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  4. [并查集] POJ 1182 食物链

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66294   Accepted: 19539 Description ...

  5. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  6. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

    题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...

  7. 并查集--poj 2492

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  8. 并查集 POJ 1988

    #include <cstdio> #define N 30010 int pa[N]; //parent int siz_tree[N]; //size of tree int d[N] ...

  9. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

随机推荐

  1. vue.js常用指令

    本文摘自:http://www.cnblogs.com/rik28/p/6024425.html Vue.js的常用指令 上面用到的v-model是Vue.js常用的一个指令,那么指令是什么呢? Vu ...

  2. Littleproxy的使用

    介绍 LittleProxy是一个用Java编写的高性能HTTP代理,它基于Netty事件的网络库之上.它非常稳定,性能良好,并且易于集成到的项目中. 项目页面:https://github.com/ ...

  3. 小程序 上啦下拉刷新window配置

    "enablePullDownRefresh": "true"   /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefres ...

  4. Boa服务器移植

    Boa是一种非常小巧的Web服务器,其可执行代码只有大约60KB左右.作为一种单任务Web服务器,Boa只能依次完成用户的请求,而不会fork出新的进程来处理并发连接请求.但Boa支持CGI,能够为C ...

  5. Jvm dump介绍与使用(内存与线程)

    很多情况下,都会出现dump这个字眼,java虚拟机jvm中也不例外,其中主要包括内存dump.线程dump. 当发现应用内存溢出或长时间使用内存很高的情况下,通过内存dump进行分析可找到原因. 当 ...

  6. Kafka日志存储原理

    引言 Kafka中的Message是以topic为基本单位组织的,不同的topic之间是相互独立的.每个topic又可以分成几个不同的partition(每个topic有几个partition是在创建 ...

  7. Unity使用C#实现简单Scoket连接及服务端与客户端通讯

    简介: 网络编程是个很有意思的事情,偶然翻出来很久之前刚开始看Socket的时候写的一个实例,贴出来吧 Unity中实现简单的Socket连接,c#中提供了丰富的API,直接上代码. 服务端代码: [ ...

  8. 【树论 2】Kruskal 的学习和使用

    Tips:本题解是[随便搞搞 1]Prim算法的学习和使用 的姊妹篇,希望先阅读Prim算法. 预习及预备知识: 克鲁斯卡尔(Kruskal)算法是实现图的最小生成树最常用的算法. 大家知道,存储图的 ...

  9. 那些ie6已支持的方法属性,成为事实标准。或者方便大家的api

    很多api,都是ie6实现,后来其他w3c或其他浏览器支持,或者用类似的方法模拟 onselectionchange  判断选区改变 ,chrome已支持 Element.contains   判断元 ...

  10. bzoj : 4504: K个串 区间修改主席树

    4504: K个串 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 268  Solved: 110[Submit][Status][Discuss] ...