pku 1703(种类并查集)
题目链接:http://poj.org/problem?id=1703
思路;个人觉得本质上还是和带权并查集一样的,只不过多了一个MOD操作,然后就是向量关系图稍微改动一下就变成种类并查集了,对于本题,我们可以用一个kind数组来表示是否属于同一类,其中kind[x]==0表示不是同一类,kind[x]==1表示属于同一类,这样我们就可以得到向量关系式了(若r1=Find(u),r2=Find(v),并且parent[r1]=r2,那么就有kind[v]+1==kind[u]+kind[r1])然后变形后对2取余就可以了,即kind[r1]=(kind[v]-kind[u]+1)%2,这是Union部分,还有路径压缩的时候注意更新一下kind[]就ok了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 100010
int parent[MAXN];
int kind[MAXN];
//0表示不属于同一类,1表示属于同一类
int n,m; void Initiate()
{
for(int i=;i<=n;i++){
parent[i]=i;
}
memset(kind,,(n+)*sizeof(kind[]));
} int Find(int x)
{
if(x==parent[x]){
return parent[x];
}
int tmp=Find(parent[x]);
kind[x]=(kind[x]+kind[parent[x]])%;
return parent[x]=tmp;
} void Union(int u,int v)
{
int r1=Find(u),r2=Find(v);
if(r1==r2)return ;
parent[r1]=r2;
kind[r1]=(kind[v]-kind[u]+)%;
} int main()
{
// freopen("1.txt","r",stdin);
int _case,a,b;
char str[];
scanf("%d",&_case);
while(_case--){
scanf("%d%d",&n,&m);
Initiate();
while(m--){
scanf("%s%d%d",str,&a,&b);
if(str[]=='D'){
Union(a,b);
}else {
int r1=Find(a),r2=Find(b);
if(r1!=r2){
puts("Not sure yet.");
}else if(kind[a]==kind[b]){
puts("In the same gang.");
}else {
puts("In different gangs.");
}
}
}
}
return ;
}
pku 1703(种类并查集)的更多相关文章
- Poj(1703),种类并查集
题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...
- pku 1182(种类并查集)
题目链接:http://poj.org/problem?id=1182 解题思路来自discuss:http://poj.org/showmessage?message_id=152847 #incl ...
- 种类并查集,Poj(1703)
题目链接:http://poj.org/problem?id=1703 第一次做种类并查集,有的地方还不是很清楚,想了一上午,有点明白了,这里记录一下. 这里我参考的红黑联盟的题解. 关键:种类并查集 ...
- POJ1703--Find them, Catch them(种类并查集)
Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 32909Accepted: 10158 Description The polic ...
- poj1703 Find them, Catch them(种类并查集
题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...
- POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集
题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...
- POJ 1182食物链(分集合以及加权两种解法) 种类并查集的经典
题目链接:http://icpc.njust.edu.cn/Problem/Pku/1182/ 题意:给出动物之间的关系,有几种询问方式,问是真话还是假话. 定义三种偏移关系: x->y 偏移量 ...
- NOI2001|POJ1182食物链[种类并查集 向量]
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65430 Accepted: 19283 Description ...
- NOIP2010关押罪犯[并查集|二分答案+二分图染色 | 种类并查集]
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...
随机推荐
- C#中的索引
索引代码示例: 索引的使用示例: 索引在经过编译器编译之后就相当于一个属性,进一步的说就是两个方法. 一个类允许 ...
- hive hwi使用
hwi(hive web interface)是hive命令行接口的补充. 使用方法: 1.配置: 在配置文件hive-site.xml 中,默认有hwi的配置 <property> &l ...
- MongoDB学习笔记-创建、更新、删除文档
创建 MongoDB中使用insert方法来向集合插入文档,然后保存到MongoDB中. db.foo.insert({"hehe":"呵呵"} ...
- [转]insmod
[转]insmod http://www.cnblogs.com/amaoxiaozhu/archive/2013/03/08/2950002.html 在Linux下,驱动程序是内核的一部分,运行在 ...
- 华硕电脑安装ubuntu出现问题及决方案
问 题 一:华硕电脑安装ubuntu时无线网络禁用解决方案:打开终端(Ctrl+alt+t)运行命令sudo rmmod acer-wmi,然后开启无线,连接上后便可以上网(附上ubuntu论坛上讨论 ...
- UIWebView swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- 【BZOJ 1033】 [ZJOI2008]杀蚂蚁antbuster
Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的任 ...
- Careercup - Google面试题 - 4877486110277632
2014-05-08 05:16 题目链接 原题: Given a circle with N defined points and a point M outside the circle, fin ...
- java中的substring用法
String str="我是中国人"; str = str.substring(0, 2) +"_"+str.substring(3, 4); 结果:str=& ...
- sql中having的使用
where 和having有什么区别? where 是group by之前进行筛选,having是group by 之后进行统计的筛选,一般having会和group by一起使用,结合聚合函数,统计 ...