POJ 1173 Find them, Catch them
题意:有两个帮派,每个人只属于一个帮派,m次操作,一种操作告诉你两个人不是一个帮派的,另一种操作问两个人是不是在一个帮派。
解法:并查集+向量偏移。偏移量表示和根节点是不是同一帮派,是为0,不是为1。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long using namespace std; int father[100005], delta[100005];
void init()
{
memset(delta, 0, sizeof delta);
for(int i = 0; i < 100005; i++)
father[i] = i;
}
int Find(int a)
{
if(father[a] != a)
{
int tmp = Find(father[a]);
delta[a] = (delta[a] + delta[father[a]]) % 2;
father[a] = tmp;
}
return father[a];
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
init();
int n, m;
scanf("%d%d", &n, &m);
while(m--)
{
char o[2];
int x, y;
scanf("%s%d%d", o, &x, &y);
int c = Find(x), d = Find(y);
if(o[0] == 'A')
{
if(c != d) puts("Not sure yet.");
else
{
if(delta[x] == delta[y]) puts("In the same gang.");
else puts("In different gangs.");
}
}
else
{
if(c == d) continue;
father[c] = d;
delta[c] = (delta[y] - delta[x] + 1) % 2;
}
}
}
return 0;
}
POJ 1173 Find them, Catch them的更多相关文章
- 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: 10000K Total Submissions: 43132 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(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- 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: ...
- poj 1703 Find them, Catch them(种类并查集和一种巧妙的方法)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36176 Accepted: ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- 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 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)
题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...
- URAL 1073 Square Country(DP)
题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 ...
- 【转载】Eclipse自动编译问题
今天调试的时候发现问题:调试的时候竟然在我注释的里面走,当时那个郁闷啊,每次都要clean下才可以,晚上感觉不对劲,上网查了查,原来是bulid automatically这个我把勾去掉了,下面是原文 ...
- 黑马程序员-- .net基础加强8之委托,事件,程序集
---------------------- ASP.Net+Android+IOS开发..Net培训.期待与您交流! ---------------------- 一.委托 ============ ...
- 套题T6
过节(festival.cpp/c/pas) Dxy帮老师们过教师节的时候需要购买礼物.货架上有n 种礼物,每种礼物有不同的个数.每种礼物有价值和花费两种属性,帮他算出最大可以得到的价值.M是带的钱数 ...
- [hackerrank]Service Lane
https://www.hackerrank.com/challenges/service-lane 用RMQ做的,其实暴力也能过~ #include <iostream> #includ ...
- PHP输入流php://input [转]
对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述. "php://input allows you to read raw POST data. It is ...
- 移植 FFMPEG-2.2.4 -(编译)
源码下载:http://www.ffmpeg.org/download.html编译安装: http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu su ...
- Python 中的 TK编程
可爱的 Python:Python 中的 TK编程 http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-12/ python che ...
- PCB工艺镀金(电金)和沉金(化金)的区别
1.镀金和沉金的别名分别是什么? 镀金:硬金,电金(镀金也就是电金) 沉金:软金,化金 (沉金也就是化金) 2.别名的由来: 镀金:通过电镀的方式,使金粒子附着到pcb板上,所以叫电金,因为附着 ...