POJ 1703 Find them,Catch them ----种类并查集(经典)
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 ----种类并查集(经典)的更多相关文章
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- 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 ...
- poj 1703 Find them, Catch them(并查集)
题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...
- POJ 1703 Find them, Catch them (并查集)
题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派. 思路: 解法一: 编号划分 定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素 ...
- 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 ...
- POJ 1703 Find them, Catch them(并查集,等价关系)
DisjointSet保存的是等价关系,对于某个人X,设置两个变量Xa,Xb.Xa表示X属于a帮派,Xb类似. 如果X和Y不是同一个帮派,那么Xa -> Yb,Yb -> Xa... (X ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- POJ:1703-Find them, Catch them(并查集好题)(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49867 Accepted: 153 ...
随机推荐
- C#6.0语法糖剖析(二)
1.索引初始化 使用代码 ] = ] = ] = "thirteen"}; 编译器生成的代码 Dictionary<int, string> dictionary2 = ...
- Plug-in 'org.eclipse.cdt.ui' contributed an invalid Menu Extension
终于在mac上配置了最新的eclipse和adt(Win和Mac oxs通用),然后就Error Log报这种错误,运行了hello word,没有影响,但是依旧有这种错误! 记录下错误: eclip ...
- 为什么每个浏览器都有Mozilla字样?
你是否好奇标识浏览器身份的)”,于是IE可以收到含有框架的页面了,所有微软的人都嗨皮了,但是网站管理员开始晕了. 因为微软将IE和Windows捆绑销售,并且把IE做得比Netscape更好,于是第一 ...
- 初学Node(六)搭建一个简单的服务器
搭建一个简单的服务器 通过下面的代码可以搭建一个简单的服务器: var http = require("http"); http.createServer(function(req ...
- [ html canvas 模仿支付宝刮刮卡效果 ] canvas绘图属性 模仿支付宝刮刮卡效果实例演示
<!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...
- 上传Android代码到Jcenter(解决了字符映射的问题)
请先阅读:http://blog.saymagic.cn/2015/02/16/release-library-to-jcenter.html 最外面的build.gradle // Top-leve ...
- Python基础(1)--Python编程习惯与特点
1.代码风格 在Python中,每行程序以换行符代表结束,如果一行程序太长的话,可以用“\”符号扩展到下一行.在python中以三引号(""")括起来的字符串,列表,元组 ...
- JavaWeb开发必过关-Servlet学习(一)
一.什么是Servlet servlet其实是一个小程序,它是运行在服务器上的,一个servlet就是一个Java类,可以通过"请求-响应"编程模型来访问这个驻留在服务器内存的Se ...
- 【原】iOSCoreAnimation动画系列教程(一):CABasicAnimation【包会】
本文的最新版本已经发布在简书[编程小翁]上,强烈建议到上查看简书,[点击这里跳转]. 在iOS中,图形可分为以下几个层次: 越上层,封装程度越高,动画实现越简洁越简单,但是自由度越低:反之亦然.本文着 ...
- 深入理解Android的startservice和bindservice
一.首先,让我们确认下什么是service? service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比 ...