Find them, Catch them---poj1703(并查集)
题目链接:http://poj.org/problem?id=1703
可以认为n个人和m句话;
每句话包含A a b;D a b;
刚开始关系不确定;
A a b 就是问ab 是否同类;
D a b就是告诉我们ab不同类;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <algorithm>
using namespace std; const int oo = 0x3f3f3f3f;
const int N = ; int f[N], r[N];///r[i]代表f[i]与i的关系,1代表不同类,0代表同类; int Find(int x)
{
int k=f[x];
if(x!=f[x])
{
f[x] = Find(f[x]);
r[x] = (r[x]+r[k])%;
}
return f[x];
} int main()
{
int t;
scanf("%d", &t); while(t--)
{
char s[];
int n, m, i, a, b, fa, fb; scanf("%d%d", &n, &m); for(i=; i<=n; i++)
{
f[i] = i;
r[i] = ;
} for(i=; i<=m; i++)
{
scanf("%s%d%d", s, &a, &b); if(s[]=='A')
{ fa = Find(a);
fb = Find(b); if(fa!=fb)
printf("Not sure yet.\n");
else
{
if(r[a]==r[b])
printf("In the same gang.\n");
else
printf("In different gangs.\n");
} }
else
{
fa = Find(a);
fb = Find(b); if(fa!=fb)
{
f[fa] = fb;
r[fa] = (r[b] - r[a] + + )%;
}
}
}
}
return ;
}
Find them, Catch them---poj1703(并查集)的更多相关文章
- poj1703 Find them, Catch them(并查集)
https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...
- poj1703 Find them, Catch them(并查集的应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32225 Accepte ...
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- poj1703 Find them,Catch them 【并查集】
做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- POJ_1703 Find them, Catch them 【并查集】
一.题面 POJ1703 二.分析 需要将并查集与矢量法则相结合.par数组用以记录父节点,rank用以记录与父节点的关系.如题意,有两种关系,设定0是属于同一个帮派,1表示不属于同一个帮派. 运用并 ...
- POJ:1703-Find them, Catch them(并查集好题)(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49867 Accepted: 153 ...
- poj1703Find them, Catch them(并查集以及路径压缩)
/* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...
- POJ 1703 Find them, Catch them (并查集)
题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派. 思路: 解法一: 编号划分 定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素 ...
- POJ 1703 Find them, Catch them(并查集拓展)
Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...
随机推荐
- resize2fs: Bad magic number in super-block while trying to open
I am trying to resize a logical volume on CentOS7 but am running into the following error: resize2fs ...
- PHP利用memcache缓存技术提高响应速度
PHP下memcache模块是一个高效的守护进程,提供用于内存缓存的过程式程序和面向对象的方便的接口,特别是对于设计动态web程序时减少对数据库的访问.memcache也提供用于通信对话(sessio ...
- Spring配置文件加载流程
http://blog.csdn.net/dy_paradise/article/details/6038990
- 创建SQL语句_面试
创建一个表:create table if not exists Teachaers(tea_id integer primary key autoincrement,tea_name text,t ...
- Windows7下4种方式快速显示桌面
1.Windows键+D快捷键直接显示桌面: 2.鼠标移到任务栏右下角直接显示桌面: 3.Windows键+空格快捷键显示桌面: 4.任务栏鼠标右键,选择“显示桌面”.
- Unity UI大小动态设置(Resize Unity UI RectTransform)
我们在开发过程中发现,要调整Unity UI元素的大小,RectTransform提供了sizeDelta属性可以用来动态修改RectTransform的大小,但同时我们也google到另外一个修改R ...
- 怎么用MathType编辑带圈字母
在用word公式编辑器MathType编辑公式时,里面涉及到很多的数学物理符号等等,这些符号或者是公式其实都可以利用MathType中的模板进行组合.在这些数学公式中,有时会有一些比较特殊的符号,它们 ...
- MathType编辑双向斜箭头的教程
箭头是一个很常见的符号,不只是在数学中,在各个方面出现的频率都很高,因此在数学公式中出现时,用MathType公式编辑器编辑公式时也要尽量地能够编辑出这些符号.箭头符号在MathType中有很多,使用 ...
- CRC-16校验C#代码
[csharp] view plaincopyprint? using System; using System.Collections.Generic; using System.Text; usi ...
- python 协程(单线程中的异步调用)(转廖雪峰老师python教程)
协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在 ...