(中等) 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 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...
随机推荐
- html 页面视图中的资源文件(css/js/image)的路径问题。
说到html 页面视图中的资源文件的路径引用问题,这个问题以前一直没去弄明白.今天,我将公司新开发的一个项目完全移植到我本地搭建的php 环境中来,遇到了这个问题,想了一下,然后也不是很困难的就把它给 ...
- OpenGL红宝书例3.1 -- glBufferSubData使用
代码实现 1.1 C++部分 GLFWwindow *window; GLuint shader_program; GLuint VAO; void init() { static const GLf ...
- linux(视频学习)2
第二部分(javaee的开发环境的搭建): 1. 安装jdk的过程: 安装ios的镜像文件,挂载到/mnt目录下.挂载: mount /mnt/cdrom卸载: umount /mnt/cdrom ...
- Python -- Web -- 使用框架
Python的web框架有很多: Flask,Django,Zope2,Web.py,Web2py,Pyramid,Bottle, Tornado... Flask 轻量级,比较简单 from fla ...
- 转:Java eclipse下 Ant build.xml实例详解
在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...
- plat模板修改记录
每个栏目显示文章数 homepage.hph 94行 $loop = new WP_Query('post_type=post&showposts=6&category_name=xi ...
- 优化のzencart URL &zenid=.....
zencart URL后面带有一串&zenid=.....解决方案 发布时间:2013年3月16日 次浏览:106 经木木测试,此方法可用. ================= 最近一个客户的 ...
- js实现类似于add(1)(2)(3)调用方式的方法
群里有人说实现类似add(1)(2)(3)调用方式的方法,结果马上有人回答: var add = function(a){ return function(b){ return function(c) ...
- linux找回密码
开机启动系统,在进入系统之前按键盘上面的Esc键,会进入下面的界面 按键盘上的e键,出现下面界面 用键盘上的方向键移动光标到第二项,然后再按键盘上面的e,会出现下面的界面 输入一个空格键,然后在输入数 ...
- js中将 整数转成字符,,将unicode 编码后的字符还原出来的方法。
一.将整数转成字符: String.fromCharCode(17496>>8,17496&0xFF,19504>>8,19504&0xFF,12848> ...