(中等) POJ 1703 Find them, Catch them,带权并查集。
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.
题目就是给了一些点不在一个集合内,问某两个点是否在一个集合内。
带权并查集的经典题,要注意两个集合合并是判断是否是一个,然后就是权相加的时候要注意。
不过感觉这个题目有些bug,题目说每个集合至少一个,这样的话就应该判断N=2的情况,但是没判断也过了。。。
代码如下:
// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月17日 星期五 20时24分40秒
// File Name : 1703_1.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; int N;
int fa[MaxN],val[MaxN]; int find(int x,int &num)
{
if(fa[x]==-)
return x; int tn=;
int t=find(fa[x],tn); fa[x]=t;
num=tn+val[x];
val[x]=num; return fa[x];
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T,Q;
int a,b;
int ba,bb;
int x,y;
char s[]; scanf("%d",&T); while(T--)
{
scanf("%d %d",&N,&Q); memset(fa,-,sizeof(fa)); while(Q--)
{
scanf("%s %d %d",s,&a,&b); if(s[]=='D')
{
x=y=;
ba=find(a,x);
bb=find(b,y); if(ba!=bb)
{
fa[ba]=bb;
val[ba]=x+y+;
}
}
else
{
x=y=;
ba=find(a,x);
bb=find(b,y); if(ba!=bb)
puts("Not sure yet.");
else if((y-x)%)
puts("In different gangs.");
else
puts("In the same gang.");
}
}
} return ;
}
(中等) POJ 1703 Find them, Catch them,带权并查集。的更多相关文章
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- [poj 2912] Rochambeau 解题报告 (带权并查集)
题目链接:http://poj.org/problem?id=2912 题目: 题目大意: n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000) 接下来m行形 ...
- poj 1733 Parity game【hash+带权并查集】
hash一下然后用带权并查集做模2下的前缀和 #include<iostream> #include<cstdio> #include<map> #include& ...
- POJ 2492 A Bug's Life 带权并查集
题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...
- 【poj 1182】食物链(图论--带权并查集)
题意:有3种动物A.B.C,形成一个"A吃B, B吃C,C吃A "的食物链.有一个人对N只这3类的动物有M种说法:第一种说法是"1 X Y",表示X和Y是同类. ...
- 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 1988】 Cube Stacking (带权并查集)
Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...
- POJ 2492 A Bug's Life (带权并查集 && 向量偏移)
题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...
随机推荐
- 编写Linux/Unix守护进程
原文: http://www.cnblogs.com/haimingwey/archive/2012/04/25/2470190.html 守护进程在Linux/Unix系统中有着广泛的应用.有时,开 ...
- C++矩阵处理库--Eigen初步使用
项目要进行比较多的矩阵操作,特别是二维矩阵.刚开始做实验时,使用了动态二维数组,于是写了一堆Matrix函数,作矩阵的乘除加减求逆求行列式.实验做完了,开始做代码优化,发现Matrix.h文件里适 ...
- 正确使用#include和前置声明(forward declaration)
http://blog.csdn.net/SpriteLW/article/details/965702
- haxe 嵌入swf 读取里面的内容
首先安装 swf 库,运行命令: 命令提示符: haxelib install swf 在project.xml 加上 <!-- 导入swf类库 --> <haxelib name= ...
- Codeforces 691B s-palindrome
水题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...
- ecshop后台增加模块菜单项详细教程(图文)
有的时候我们会在后台增加新的功能,菜单项是一个程序的入口,是必不可少的,如何在后台增加菜单项呢,大家可以参考下面的教程: 例如:想在后台左侧的菜单栏的"促销管理"下添加一个&q ...
- 第15章 I/O(输入/输出)
在变量.数组和对象中存储的数据是暂时存在的,程序结束后它们就会丢失.为了能够永久地保存创建的数据,需要将其保存在磁盘文件中,这样就可以在其它程序中使用它们.Java的I/O技术可以将数据保存到文本文件 ...
- JSP内置对象--session对象(getId(),getCreationTime(),getLastAccessedTime(),isNew(),invalidate(),setAttribute(),getAttribute())
session对象是javax.servlet.http.HttpSession接口的实例,但是不像HttpServletRequest或HttpServletResponse一样,有父接口,他没有父 ...
- netty最快?
http://www.techempower.com/blog/2013/04/05/frameworks-round-2/
- Ubuntu系统如何修改主机名
1.执行命令 hostname temp_name 这样主机名就改掉了.只不过重启后名字会恢复不一定使我们想要的.机器重启后会重新去读取/etc/hostname里面存储的主机名.所以如果想永久改掉的 ...