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 ...
随机推荐
- spring webflow
最近看了一段时间的spring-webflow,说一下自己的见解吧. 首先说一下关于spring-webflow的技术文档太少了,网上就只有一个入门篇,讲的比较笼统,但还是推荐一下, Spring W ...
- [Linux] ubuntu 安装 Wireshark
Wireshark是一款非常流行的协议分析软件.自然可以网络抓包的需求. sudo apt-get install wireshark 出于安全方面的考虑,普通用户不能够打开网卡设备进行抓包,wire ...
- Openshift 用户,角色和RBAC
OCP中的权限管理沿用的Kubernetes RBAC机制,授权模式主要取决于下面几个因数 Rules 针对主要对象的操作权限,比如建立Pod Sets of permitted verbs on a ...
- hibernate反向工程 (eclipse和myeclipse)【转】
myeclipse下hibernate反向工程: 1.选择myeclipse hibernate视图 2.建立与后台数据库的连接 1).configure database driver: 2).添加 ...
- [转载]Delphi 版 everything、光速搜索代码
近日没啥事情,研究了一下 everything.光速搜索原理.花了一个礼拜时间,终于搞定. 废话不多说,直接上代码: unit uMFTSearchFile; { dbyoung@sina.com 2 ...
- SaltStack–Job管理
官方文档:https://docs.saltstack.com/en/2016.11/ref/modules/all/salt.modules.saltutil.html 在SaltStack里面执行 ...
- python微信公众号开发学习记录
网上有很多微信公众号的开发教程,但是都是好几年前的了,而且很多都是抄袭其他人的,内容几乎一模一样.真的无语了.只好自己总结一下开发的一些简单流程. 一先去注册个微信公众号,这个就不详细说了, 二登录后 ...
- stderr重定向
在测试alljoyn时开启了内部日志,输出太多想重定向到文件中,使用如下命令: ./chat -s aaa >.txt 居然还是打印到屏幕上而不是输出到文件中. 查看alljoyn写日志的代码, ...
- netmap -- ixgbe
利用netmap抓ixgbe网卡上的以太网帧,跟e1000e网卡有区别. 使用e1000.e1000e网卡发以太网帧只要以太网帧的格式正确就可以了.只要格式和 目的MAC.源MAC地址正确,网卡就可以 ...
- 转: Android Studio你不知道的调试技巧
http://tianweishu.com/2015/12/21/android-studio-debug-tips-you-may-not-know/