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条臭 ...
随机推荐
- C# 在vs2010中打开vs2012的项目(转)
在vs2010中打开vs2012的项目 今天在自己的电脑上装了vs2010然后要打开之前在vs2012上创建的sln文件 被提示-- 无法打开在新版本上创建的sln--解决方案--文件 其实vs201 ...
- CF 86D Powerful array
离线+分块 将n个数分成sqrt(n)块. 对所有询问进行排序,排序标准: 1. Q[i].left /block_size < Q[j].left / block_size (块号 ...
- PowerDesigner 逆向工程 从SQL文件转换成PDM 从PDM转成CDM
从SQL文件逆向工程到PDM: ①选择file -> Reverse Engineer - > Database ②在General选项卡中选择MySQL数据库,点击确定. ③using ...
- java 多线程 CountDownLatch用法
CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); pu ...
- 关于JS中变量的作用域-实例
先看问题,如下,自己运行一下吧! if (!('_qyzA' in window)) { var _qyzA = 1; } alert(_qyzA);//undefined 分析:首先,所有的全局变量 ...
- vc2005中没有classwizard这个命令
vc2005中没有classwizard这个命令了 2005下怎么添加鼠标事件 vc2005中没有classwizard这个命令了 取代classwizard 中的添加消息映射,添加类,等等的功能主要 ...
- Android 动态改变布局属性RelativeLayout.LayoutParams.addRule()
我们知道,在 RelativeLayout 布局中有很多特殊的属性,通常在载入布局之前,在相关的xml文件中进行静态设置即可. 但是,在有些情况下,我们需要动态设置布局的属性,在不同的条件下设置不同的 ...
- List应用举例
1.集合的嵌套遍历 学生类: package listexercise; /** * Created by gao on 15-12-9. */ public class Student { priv ...
- UVa 10075 - Airlines
航线算球面距离,需要经纬度转空间坐标. 任意两点间距离用Floyd求出来,查询时直接查表. #include <cstdio> #include <map> #include ...
- Cookie的具体使用之来存储对象
1.创建一个新的cookie,并赋值. HttpCookie cookie; cookie=new HttpCookie("user"); cookie.D ...