(中等) 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 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...
随机推荐
- Hibernate 系列教程14-继承-PerTable策略
Employee public class Employee { private Long id; private String name; HourlyEmployee public class H ...
- dhcpv6开源软件配置
ISC-dhcp server for IPv6 1. 下载源码:http://www.isc.org/software/dhcp 2.安装:最好以root身份安装,否则会permission de ...
- 自定义开关ToggleButton
package com.example.test;import android.os.Bundle;import android.app.Activity;import android.view.Me ...
- IDAPython: importing “site” failed
问题:IDA启动时,弹出IDAPython: importing “site” failed对话框. 解决办法:环境变量添加PYTHONHOME,值为python安装路径,比如:C:\Python27
- JavaScript的异步机制
我们经常说JS是单线程的,比如node.js研讨会上大家都说JS的特色之一是单线程的,这样使JS更简单明了,可是大家真的理解所谓JS的单线程机制吗?单线程时,基于事件的异步机制又该当如何 1 先看下两 ...
- java中iofile的路径问题,确定一个未知方法所需要的文件路径
今天遇到一个极其烦躁的问题,一个jar包中的一个方法,要求函数中要求传入一个String类型的参数,用于指示文件所在的路径.但是对于我们来说完全不知道他需要的路径是绝对路径还是相对路径,所以我尝试了很 ...
- 利用php函数mkdir递归创建层级目录
项目开发中免不了要在服务器上创建文件夹,比如上传图片时的目录,模板解析时的目录等.这不当前手下的项目就用到了这个,于是总结了几个循环创建层级目录的方法. php默认的mkdir一次只能创建一层目录,而 ...
- 第19章 网络通信----TCP程序设计基础
TCP网络程序设计是指利用Socket类编写通信程序.利用TCP协议进行通信的两个应用程序是有主次之分的,一个称为服务器程序,另一个称为客户机程序,两者的功能和编写方法大不一样. 1.InetAddr ...
- MJRefresh
automaticallyChangeAlpha 下拉或上拉时,文字颜色逐渐加深
- Kali-linux安装之后的简单设置(转)
1.更新软件源: 先备份软件源文件 cp /etc/apt/sources.list /etc/apt/sources.list.bak 修改sources.list文件: leafpad /etc/ ...