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. 功放AUX接口解析

    功放上的AUX输入端口就是音频输入端口,用来连接音频输出设备:PC机上声卡.MP3播放器.CD机等

  2. javascript --- 将共享属性迁移到原型中去

    当我们用一个构造函数创建对象时,其属性就会被添加到this中去.并且被添加到this中的属性实际上不会随着实体发生改变,这时,我们这种做法显得会很没有效率.例如: function her(){ th ...

  3. C# 线程基础

    1. 线程的基本概念 简单的讲进程就是程序分配在内存当中,等待处理器进行处理,请记住线程会消耗大量的操作系统资源.多个线程共享一个物理处理器将导致处理器忙于处理管理这些进程,而无法运行程序. 使用线程 ...

  4. 使用Sharepoint定时运行Excel中宏程序

    需求:因为Excel中数据量很大,其中包含了几个宏程序从其他数据源读取数据,运行一次宏需要比较长的时间,为了不影响使用,要求每天半夜运行一次Excel中的宏(无参数),Excel存放在共盘上. 解决方 ...

  5. Android项目实战(十五):自定义不可滑动的ListView和GridView

    不可滑动的ListView (RecyclweView类似) public class NoScrollListView extends ListView { public NoScrollListV ...

  6. NavigationController的使用整理

    1.设置NavigationBar的背景色: self.navigationController.navigationBar.barTintColor = [UIColor redColor]; 2. ...

  7. IOS-UITextField-全解

    IOS-UITextField-全解   //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFrame: ...

  8. Swift面向对象基础(上)——Swift中的类和结构体(下)

    学习来自<极客学院> import Foundation class User { var name:String var age:Int init(name:String,age:Int ...

  9. win7操作技巧

    Q : 打开文件夹默认最大化A :随便打开一个文件夹 鼠标移动到左上角 然后点击鼠标左键 选择“最大化” 后关闭 之后每次打开就是最大化了

  10. android studio annotation 配置过程

    参考了好些配置,发现总有这样,那样的问题. 环境:androidstudio 1.5 preview 2 sdk 6.0 1.首先新建一个android项目. 过程略 2.配置project的buil ...