POJ 1703 Find them, Catch them(种类并查集)
这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解。
#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int o[];
int flag[];
int find(int x)
{
if (x == o[x]) return x;
int t = find(o[x]);
flag[x] = (flag[o[x]] + flag[x]) % ;
return o[x] = t;
}
void merge(int x,int y)
{
int tx,ty;
tx = find(x);
ty = find(y);
if(tx != ty)
{
o[tx] = ty;
flag[tx] = (flag[x]+flag[y]+)%;
}
}
int judge(int x,int y)
{
int tx,ty;
tx = find(x);
ty = find(y);
if(tx != ty)
return -;
else
return (flag[x] == flag[y]);
}
int main()
{
int T,sv,ev,i,n,m;
char ch;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%*c",&n,&m);
for(i = ;i <= n;i ++)
{
flag[i] = ;
o[i] = i;
}
for(i = ;i < m;i ++)
{
scanf("%c%*c%d%d%*c",&ch,&sv,&ev);
if(ch == 'A')
{
int temp = judge(sv,ev);
if(temp == -)
printf("Not sure yet.\n");
else if(temp == )
printf("In different gangs.\n");
else
printf("In the same gang.\n");
}
else
{
merge(sv,ev);
}
}
}
return ;
}
POJ 1703 Find them, Catch them(种类并查集)的更多相关文章
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- 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 ...
随机推荐
- ecmascript6入门
ECMAScript 6 入门 阮一峰
- iOS视图边框的简单做法
我们绘制UI界面的时候,一般我们做边框是用layer,然后再给它上面添加阴影什么的,我比较喜欢用下面这个方法, UI弄几张边框的图片,用代码给图片拉伸 - (UIImage *)changeBorde ...
- K大数查询(bzoj 3110)
Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置 ...
- Laravel 报500错误
Laravel报500错误 发生情境: 使用Composer安装Laravel5.1版本到本地wamp环境,可以成功访问框架首页,然后上传到服务器上,报500错误. 解决: (1)在首页public/ ...
- 利用javascript实现在圆周上匀速划动的动画效果
先看下效果:
- 让win7任务条上的文件夹打开是c,d,e,f而不是库
如果资源管理器是打开的,则右键点击资源管理器的图标,在跳出的菜单中,右键点击“Windows资源管理器”,选择“属性”. 在“快捷方式’选项卡,“目标”一栏,默认的是 %windir%\explore ...
- java验证身份证号码是否有效源代码 wn25的头像 wn25 23 2015-01-04 20:09 6 基本信息 Java × 1 浏览
原文:http://www.open-open.com/code/view/1420373343171 1.描述 用java语言判断身份证号码是否有效,地区码.出身年月.校验码等验证算法 2.源代码 ...
- ios Crash Log 分析汇总
方法一: 1.xcode 有自带的symbolicatecrash,可以将.crash文件中的16进制地址转换成可读的函数地址. symbolicatecrash位于: /Applications/X ...
- IOS --关于粘贴板 ,剪切板 ,UILabel的复制
在iOS中下面三个控件,自身就有复制-粘贴的功能: 1.UITextView 2.UITextField 3.UIWebView UIKit framework提供了几个类和协议方便我们在自己的应用程 ...
- 【sourcetree】sourcetree连接远程仓库需要登陆但是一直登陆不上的问题 解决方法
授权类型选用 基础 .只需要登陆你在bitbucket的用户名和密码 如下 .即可成功连接远程仓库