(并查集 带关系)Find them, Catch them -- poj -- 1703
链接:
http://poj.org/problem?id=1703
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 36768 | Accepted: 11294 |
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.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <algorithm>
using namespace std; #define N 100005
#define INF 0x3f3f3f3f int f[N], r[N], vis[N]; 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--)
{
int n, m, i, a, b;
char s[]; scanf("%d%d", &n, &m); memset(r, , sizeof(r));
memset(vis, , sizeof(vis));
for(i=; i<=n; i++)
f[i] = i; for(i=; i<=m; i++)
{
scanf("%s%d%d", s, &a, &b); if(s[]=='D')
{
int fa = Find(a);
int fb = Find(b); if(fa!=fb)
{
f[fa]=fb;
r[fa] = (r[a]-r[b]+) % ;
}
vis[a] = vis[b] = ;
}
else
{
if(vis[a]== || vis[b]==)
printf("Not sure yet.\n");
else
{
int fa = Find(a);
int 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");
}
}
}
} }
return ;
}
(并查集 带关系)Find them, Catch them -- poj -- 1703的更多相关文章
- K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...
- 浅谈并查集&种类并查集&带权并查集
并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...
- Find them, Catch them(POJ 1703 关系并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38668 Accepted: ...
- 种类并查集——带权并查集——POJ1182;HDU3038
POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...
- Poj1182 食物链(并查集/带权并查集)
题面 Poj 题解 这里采用并查集的补集. \(x\)表示同类集合,\(x+n\)表示敌人集合,\(x+n\times2\)表示敌人的敌人集合. 如果当前给出的是一对同类关系,就判断\(x\)是否吃\ ...
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
- 【并查集&&带权并查集】BZOJ3296&&POJ1182
bzoj1529[POI2005]ska Piggy banks [题目大意] n头奶牛m种语言,每种奶牛分别掌握一些语言.问至少再让奶牛多学多少种语言,才能使得它们能够直接或间接交流? [思路] ( ...
- HDU 3038 How Many Answers Are Wrong 并查集带权路径压缩
思路跟 LA 6187 完全一样. 我是乍一看没反应过来这是个并查集,知道之后就好做了. d[i]代表节点 i 到根节点的距离,即每次的sum. #include <cstdio> #in ...
- 食物链--poj1182(并查集含有关系)
http://poj.org/problem?id=1182 题意应该就不用说了 再次回到食物链这道题,自己写了一遍,一直wa...原因竟然是不能用多实例,我也是醉了,但是我真的彻底的理解了,那 ...
随机推荐
- python 命令行参数获取
import argparse parser = argparse.ArgumentParser(description='Real-Time Filtering evaluation script ...
- NET基础篇——反射的奥妙
反射是一个程序集发现及运行的过程,通过反射可以得到*.exe或*.dll等程序集内部的信息.使用反射可以看到一个程序集内部的接口.类.方法.字段.属性.特性等等信息.在System.Reflectio ...
- Makefile编写 二
变量 1. Makefile中变量和函数的展开(除规则命令行中的变量和函数以外),是在make读取makefile文件时进行的,包括“define”定义的变量. 2. 变量可以用来代表一个文件名列表. ...
- Ubuntu下VIM使用指南
基本命令: Esc:VIM中的万能功能键之一,基本上任何时候按这个键,都可以返回VIM的普通状态. i:在普通状态下按i可以进入“插入”编辑状态,这个时候按方向键移动光标,在想要输入的地方输入字符,用 ...
- mysql安装过程及注意事项
1.1. 下载: 我下载的是64位系统的zip包: 下载地址:https://dev.mysql.com/downloads/mysql/ 下载zip的包: 下载后解压:D:\软件安装包\mysql- ...
- poj-3253-Fence Repair(哈夫曼)
/* Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19914 Accepted: 6314 Desc ...
- 20165233 Java第四章学习总结
20165233 2017-2018-2 <Java程序设计>第三周学习总结 教材学习内容总结 基础 类:包括类声明和类体. 其中类声明的变量被称作对象变量,简称对象. 类体中包括两部分: ...
- OpenCL NativeKernel 计算矩阵乘法
▶ 使用函数 clEnqueueNativeKernel 来调用 C/C++ 本地函数(在 OpenCL 中将其看做回调函数),使用本地编译器(而不是 OpenCL 编译器)来编译和执行内核 ● 代码 ...
- 使用API调用外部程序并监控程序状态
Public Type SHELLEXECUTEINFO cbSize As Long fMask As Long hwnd As Long lpVerb As String ...
- Git----远程仓库01
到目前为止,我们已经掌握了如何在Git仓库里对一个文件进行时光穿梭,你再也不用担心文件备份或者丢失的问题了 可是用过集中式版本控制系统SVN的童鞋们会站出来说,这些功能在SVN里早就有了,没看出Git ...