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 ...
随机推荐
- 四、MyBatis主配置文件
//备注:该博客引自:http://limingnihao.iteye.com/blog/1060764 在定义sqlSessionFactory时需要指定MyBatis主配置文件: Xml代码 收藏 ...
- Hibernate框架之注解的配置
在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ...
- git version 2.5.0.windows.1中文乱码问题解决方案
UI部分 Options->Text Local:zh_CN,Character set:GBK ~/.GitConfig [gui] encoding = utf-8 [tgit] proje ...
- Force.com微信开发系列(一) 后台配置
为寻找国内免费云资源作为微信后台,花了一天时间试用SinaAppEngine(SAE),调试太不方便用户体验差.新浪作为媒体公司技术功底经不起考验,亚马逊能推出AWS,新浪还不行!更好选项是百度Bai ...
- MyBatis入门(六)---mybatis与spring的整合
一.整合需要 1.1.方法 上一章中的数据 需要spring通过单例方式管理SqlSessionFactory spring和mybatis整合生成代理对象,使用SqlSessionFactory创建 ...
- JAVA基础学习day25--Socket基础二-多线程
一.上传图片 1.1.示例 /* 上传图片 */ import java.net.*; import java.io.*; import java.util.*; import java.text.* ...
- SVN版本更新后,upData工程之后,Xcode 工程文件打不开解决办法
svn更新代码后,打开xcode工程文件,会出现 xxx..xcodeproj cannot be opened becausethe project file cannot be parsed. ...
- nodejs socket
server.js var net = require('net'); var clientList = []; var HOST = '127.0.0.1'; var PORT = 6969; va ...
- JQuery EasyUI中datagrid的使用
在学习过程中,可以参照JQuery EasyUI的官方网站学习.地址:http://www.jeasyui.com/demo/main/index.php 在学习JQuery EasyUI中的Data ...
- JSP 标准标签库(JSTL)之最常用的JSTL标签总结
JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. Apache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-t ...