http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100100 int fa[N],same[N]; void makeset(int n)
{
for(int i=;i<=n;i++)
{
fa[i] = i;
same[i] = ;
}
} int findset(int x)
{
if(x != fa[x])
{
int tmp = fa[x];
fa[x] = findset(fa[x]);
same[x] = (same[x] + same[tmp])%;
}
return fa[x];
} int unionset(char s,int a,int b)
{
int x = findset(a);
int y = findset(b);
if(x == y) //属于同一个集合
{
if(s == 'A')
{
if(same[a] == same[b])
{
return ; //same
}
else
{
return ; //different
}
}
}
else //不属于同一个集合
{
if(s == 'A')
{
return ; //unsure
}
else if(s == 'D')
{
fa[x] = y;
same[x] = (same[a] + same[b] + )%;
}
}
return ; //要加,这是对其他情况的处理,因为此函数必须返回一个值
} int main()
{
int t,n,m,i;
char ss[];
int a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
makeset(n);
for(i=;i<m;i++)
{
scanf("%s %d %d",ss,&a,&b);
if(unionset(ss[],a,b) == )
{
cout<<"In the same gang."<<endl;
}
else if(unionset(ss[],a,b) == )
{
cout<<"In different gangs."<<endl;
}
else if(unionset(ss[],a,b) == )
{
cout<<"Not sure yet."<<endl;
}
}
}
return ;
}

POJ 1703 Find them,Catch them ----种类并查集(经典)的更多相关文章

  1. POJ 1703 Find them, Catch them(种类并查集)

    题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...

  2. poj 1703 Find them, Catch them 【并查集 新写法的思路】

    题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...

  3. poj 1703 Find them, Catch them(并查集)

    题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...

  4. POJ 1703 Find them, Catch them (并查集)

    题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派. 思路: 解法一: 编号划分 定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素 ...

  5. POJ 1703 Find them, Catch them(并查集拓展)

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

  6. POJ 1703 Find them, Catch them(并查集,等价关系)

    DisjointSet保存的是等价关系,对于某个人X,设置两个变量Xa,Xb.Xa表示X属于a帮派,Xb类似. 如果X和Y不是同一个帮派,那么Xa -> Yb,Yb -> Xa... (X ...

  7. POJ1703Find them, Catch them[种类并查集]

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

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

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

  9. POJ:1703-Find them, Catch them(并查集好题)(种类并查集)

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

随机推荐

  1. mongodb学习2---常用命令解析

    1,mongodb insert()和save()的相同点和区别区别:若新增的数据中存在主键 ,insert() 会提示错误,而save() 则更改原来的内容为新内容.insert({_id : 1, ...

  2. ahjesus C# 4.0 Parallel 并行运算

    Parallel.For - for 循环的并行运算 Parallel.ForEach - foreach 循环的并行运算 Parallel.Invoke - 并行调用多个任务 Task - 任务,基 ...

  3. SQL数据库基础(二)

    数据类型: --类似于C#中的数据类型 Datetime   范围是:1753.1.1—— 9999.12.31 Smalldatetime      1900.1.1 ——2079.6.6 操作: ...

  4. JavaScript一个经典问题

    看下面代码 <ul id="demo"> <li></li> <li></li> <li></li&g ...

  5. log4net 日志框架的配置

    log4net 日志框架的配置——静态文件(一) 添加对log4net程序集的引用 选择程序集文件添加引用即可,需要注意的是需要添加相应程序版本的程序集,如果你的应用是基于.netFramework2 ...

  6. Snort - manual 笔记(二)

    1.5 Packet Acquisition Snort 2.9 引入 DAQ 代替直接调用 libpcap . 有两种网卡特性会影响 Snort : "Large Receive Offl ...

  7. 创建第一个Android 5.0应用程序

    1.新建一个Andriod项目,并配置相关参数 2.接着下一步 3.接着下一步,配置相关参数 4.完成后出现下面界面 5.运行此项目 6.你可以在菜单中看到刚才新建的Helloworld app图标

  8. JAVA基础学习day24--Socket基础一UDP与TCP的基本使用

    一.网络模型 1.1.OIS参考模型 1.2.TCP/IP参考模型 1.3.网络通讯要素 IP地址:IPV4/IPV6 端口号:0-65535,一般0-1024,都被系统占用,mysql:3306,o ...

  9. UnityShader之固定管线Fixed Function Shader【Shader资料3】

    Fixed function shader简介:  属于固定渲染管线 Shader, 基本用于高级Shader在老显卡无法显示时的情况.使用的是ShaderLab语言,语法与微软的FX files 或 ...

  10. layout_weight的使用说明

    近期学习了Mars老师的视频,看了十二课的有关layout_weight的讲解,就做了些总结. layout_weight用于分配剩余的布局空间. 首先,先看段代码,它定义了两个textview控件 ...