POJ 1703:Find them, Catch them(并用正确的设置检查)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30702 | Accepted: 9447 |
Description
two criminals; do they belong to a same clan?
You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)
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.
题意:
城市里面有2个犯罪团伙,罪犯都属于这两个团伙。如今给你2个罪犯,推断他们是不是一个犯罪团伙。假设当前关系不确定,就输出not sure yet.
并查集的应用,非常A bug's life基本一样。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath> using namespace std; const int maxn = 100005;
int cas, n, m;
int parent[maxn], relation[maxn];
char str[2];
int a, b; int find(int x)
{
if (x == parent[x])
return x;
int px = find(parent[x]);
relation[x] = ((relation[x] + relation[parent[x]])%2);
return parent[x] = px;
} void init()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
parent[i] = i;
relation[i] = 0;
}
} void kruskal(int a, int b)
{
if(n==2 && str[0]=='A')
//题意是说仅仅有两个人时,应该是属于不同的团伙。。可是数据没有这个。。就算不加也A了、、
printf("In different gangs.\n");
else
{
int pa = find(a);
int pb = find(b);
if (pa != pb)
{
if (str[0] == 'D')
{
parent[pa] = pb;
if (relation[b] == 0)
relation[pa] = 1 - relation[a];//表示不同
else
relation[pa] = relation[a];//同样
}
else printf("Not sure yet.\n");
}
else
{
if (str[0] == 'A')
{
if (relation[a] != relation[b])
printf("In different gangs.\n");
else
printf("In the same gang.\n");
}
}
}
} int main()
{
scanf("%d", &cas);
while (cas--)
{
init();
while (m--)
{
scanf("%s%d%d", str, &a, &b);
kruskal(a, b);
}
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
POJ 1703:Find them, Catch them(并用正确的设置检查)的更多相关文章
- HDU 3172 Virtual Friends(并用正确的设置检查)
职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...
- 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(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- poj 1703 Find them, Catch them(并查集)
题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
- POJ 1703 Find them, Catch them(确定元素归属集合的并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 52925 Accepted: ...
随机推荐
- Servlet的学习(二)
本篇接上一篇<Servlet的学习(一)>,讲述如何利用MyEclipse来创建web工程, 同时讲述如何在MyEclipse中配置Tomcat服务器. 在MyEclipse中,新建“We ...
- try..catch..finally执行顺序return
try..catch..finally这个语法大家都很熟悉,就是捕捉异常.处理异常,面试中经常被问到的一个问题是:如果在try...catch中的某某地方return了,那么之后的某某步骤还会不会执行 ...
- redis+tomcat共享session问题(转)
为了让楼主看看Java开发是多么的简单和轻松,你说你耗时一周都没搞好,简直就是胡说八道!!我从没搞过reids和tomcat整合的(我做的项目一直都是去session用cookie,为了验证你在胡说八 ...
- 党建凯,创新工场知乎团队Web前端工程师
Nicholas C. Zakas谈怎样才能成为优秀的前端工程师: 昨天,我负责了Yahoo!公司组织的一次面试活动,感触颇深的是其中的应聘者提问环节.我得说自己对应聘者们提出的大多数问题都相当失望. ...
- POJ 3017 单调队列dp
Cut the Sequence Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8764 Accepted: 2576 ...
- 使用ServletFileUpload实现上传
1.首先我们应该为上传的文件建一个存放的位置,一般位置分为暂时和真是目录,那我们就须要获取这俩个目录的绝对路径,在servlet中我们能够这样做 ServletContext application ...
- CABasicAnimation学习Demo 包含了一些经常使用的动画效果
个人写的一些样例: // // ViewController.m // CABasicAnimationDemo // // Created by haotian on 14-6-13. // Cop ...
- form表单多值提交
$.ajax({ cache: true, type: "POST", url:ajaxCallUrl, data:$('#yourformid').serialize(),// ...
- CURD演示 2
<?php class UserAction extends Action{ public function index(){ echo "你好!"; $m=M('user' ...
- JSTL解析——004——core标签库03
上面章节主要讲解<c:forEach>标签,下面讲解其它标签 1.<c:forTokens>标签 forTokens标签与forEach标签类似,独有begin.end.ste ...