poj1703 Find them, Catch them(并查集的应用)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32225 | Accepted: 9947 |
Description
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input
Output
Sample Input
1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
Sample Output
Not sure yet.
In different gangs.
In the same gang. 题目大意:N 表示共有 N 个帮派;下面有 M 条个询问
有两个帮派,现在告诉你一些互相敌对的点对,然后让你判断某两个点之间的关系,并首先判两个点关系是否确定;
用并查集的思想只要两个点在一个集合里就可以了,直接用并查集然后时候在同一个集合;
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
// 0 代表木有关系,1表示不同类,2代表同类
const int N = 1e5+;
int m;
int f[N],c[N];
int init()
{
for(int i=; i<=m; i++)
{
f[i] = i;
c[i] = ;
}
}
int xfind(int x)
{
if(f[x] == x)
return x;
int t = f[x];
f[x] =xfind(f[x]);
c[x] = (c[x]+c[t])%;
return f[x];
}
int solve(char ch,int x,int y )
{
int fx = xfind(x);
int fy = xfind(y);
if(fx != fy)
{
if(ch == 'A') return ; //返回值0,表示不确定
f[fx] =fy;
if((c[x]+c[y])% ==)
c[fx] = ;
}
else
{
if(ch == 'A')
{
if(c[x] != c[y]) return ; //返回值1,表示在不同的帮派
if(c[x] == c[y]) return ; //返回值2,表示在相同的帮派
}
}
if(ch == 'D') return ;//这种情况不同在考虑的
}
int main( )
{
int T,n,x,y;
char s;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&m,&n);
init(); //初始化并查集
for(int i=; i<n; i++)
{
scanf(" %c %d %d",&s,&x,&y);
int flag = solve(s,x,y);
if(flag == ) printf("Not sure yet.\n");
else if(flag == ) printf("In different gangs.\n");
else if(flag == ) printf("In the same gang.\n");
}
}
return ;
}
poj1703 Find them, Catch them(并查集的应用)的更多相关文章
- poj1703 Find them, Catch them 并查集
poj(1703) Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26992 ...
- POJ-1703 Find them, Catch them(并查集&数组记录状态)
题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- poj1703--Find them, Catch them(并查集应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32073 Accepted: ...
- POJ1703-Find them, Catch them 并查集构造
Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- poj1703_Find them, Catch them_并查集
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42451 Accepted: ...
- poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
随机推荐
- Android进阶笔记:Messenger源码详解
Messenger可以理解为一个是用于发送消息的一个类用法也很多,这里主要分析一下再跨进程的情况下Messenger的实现流程与源码分析.相信结合前面两篇关于aidl解析文章能够更好的对aidl有一个 ...
- Java读写文件,中文乱码解决
读文件:使用new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); Strin ...
- 网站怎么布局能解决不同浏览器对CSS解析的差异,使用css reset
很多地方都提到过CSS Reset这个概念,而且细心的朋友会发现,许多大网站的CSS文件中也含有CSS Reset内容. CSS Reset是什么? 在HTML标签在浏览器里有默认的样式,例如 p 标 ...
- [转]Sql Server参数化查询之where in和like实现详解
本文转自;http://www.cnblogs.com/lzrabbit/archive/2012/04/22/2465313.html 文章导读 拼SQL实现where in查询 使用CHARIND ...
- Firefox的缓存问题
使用Firefox调试代码时,JS或CSS文件修改后,怎么刷新都不生效.原来是Firefox缓存的问题.但Firefox没有像IE一样有个每次读取最新的设置. 设置Firefox不缓存页面: 新建标签 ...
- java缓存适合使用的情况
并非所有的情况都适合于使用二级缓存,需要根据具体情况来决定.同时可以针对某一个持久化对象配置其具体的缓存策略. 适合于使用二级缓存的情况: 1.数据不会被第三方修改 一般情况下,会被hibernate ...
- Maven Web项目配置Mybatis
一.添加Mybatis和数据库相关的包 1 pom.xml中添加的包有mybatis,mybatis-spring,druid,MySQL-connector-Java,commons-io,refl ...
- 【Linux】apt-get install 怎么阻止弹出框,使用脚本默认自动安装?
You can do a couple of things for avoiding this. Setting the DEBIAN_FRONTEND variable to noninteract ...
- 纯 PHP 代码最好在文件末尾删除 PHP 结束标记
如果文件内容是纯 PHP 代码,最好在文件末尾删除 PHP 结束标记.这可以避免在 PHP 结束标记之后万一意外加入了空格或者换行符,会导致 PHP 开始输出这些空白,而脚本中此时并无输出的意图. & ...
- Swing的GUI组件得到焦点
Swing的GUI组件如JButtin,JTextArea,JRadioButton,JComboBox等,可以使用requestFocus()方法来获得焦点.