poj1703(种类并查集)
题意:有两个犯罪集团,现在有两种操作,D [a] [b]表示a和b是属于不同犯罪集团的,A [a] [b] 是询问你a和b的关系,如果ab属于同一个犯罪集团,输出In the same gang. 如果ab属于不同犯罪集团,输出In different gangs. 否则输出 Not sure yet.
思路:赤裸裸的种类并查集,0代表ab属于同一集团,1代表不同,要是不在同个树里面,就是不确定.......
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define M 100005
int father[M],rank[M];
int find(int x)
{
if(x==father[x])
return x;
int tmp=father[x];
father[x]=find(tmp);
rank[x]=(rank[x]+rank[tmp])%2;
return father[x];
}
void liantong(int x,int y)
{
int tmp=find(x);
int tmp1=find(y);
if(tmp!=tmp1)
{
father[tmp1]=tmp;
rank[tmp1]=(2-1+2-rank[y]+rank[x])%2;
} }
int main()
{
int text;
scanf("%d",&text);
while(text--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
{
father[i]=i;
rank[i]=0;
}
while(m--)
{
char ch[10];
int tmp,tmp1;
scanf("%s%d%d",ch,&tmp,&tmp1);
if(ch[0]=='D')
liantong(tmp,tmp1);
else
{
int x=find(tmp);
int y=find(tmp1);
if(x==y)
{
int r=(2-rank[tmp]+rank[tmp1])%2;
if(r==0)
printf("In the same gang.\n");
else
printf("In different gangs.\n");
}
else
printf("Not sure yet.\n");
}
}
}
return 0;
}
poj1703(种类并查集)的更多相关文章
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- 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 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...
- NOIP2010关押罪犯[并查集|二分答案+二分图染色 | 种类并查集]
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- NOI2001|POJ1182食物链[种类并查集 向量]
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65430 Accepted: 19283 Description ...
- poj1417(种类并查集+dp)
题目:http://poj.org/problem?id=1417 题意:输入三个数m, p, q 分别表示接下来的输入行数,天使数目,恶魔数目: 接下来m行输入形如x, y, ch,ch为yes表示 ...
- poj1733(种类并查集+离散化)
题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
随机推荐
- VirtualBox虚拟机增加CentOS根目录容量 LVM扩容
对于目前的网络开发者来说,比较好的搭档就是Win7+VirtualBox+CentOS的组合,既可以发挥Linux强大的网络服务功能,也可以有效的隔离各项服务拖慢系统,影响系统的运行,对于新手来说可以 ...
- mytop安装,使用mytop监控MySQL性能
本文主要描述mytop安装,安装过程中可能出现的报错,以及使用mytop监控MySQL性能. 欢迎转载,请注明作者.出处. 作者:张正 blog:http://space.itpub.net/2635 ...
- C#文件夹权限操作工具类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...
- Firefox 网络调试工具
1.Firefox 简介 Firefox 官网下载地址 Firefox 其它下载地址 Firefox 58.0.2 for Mac,密码:346y. Firefox 36.0.4 for Mac,密码 ...
- Python 爬虫 使用正则去掉不想要的网页元素
在做爬虫的时候,我们总是不想去看到网页的注释,或者是网页的一些其他元素,有没有好的办法去掉他们呢? 例如:下面的问题 第一种情况<ahref="http://artso.artron. ...
- There is no Action mapped for namespace / and action name . - [unknown location]
今天碰到了这个问题,原因不明白. 在webContent下建立了index.jsp后重启项目不报错了. 原因未知.
- mybatis与mysql中的Date和String之间转换
在javaweb开发过程中,难免会使用日期类型,在持久化时java的日期格式可以是String,Date.mysql格式可以是varchar,datetime.他们之间如何进行相互转化? 1 java ...
- django -- 为模式增加方法
在django中模式中的方法是行级的.也就是说它操作是表里的行.不是整个表 一.模式定义: from django.db import models # Create your models here ...
- talend hive数据导入到mysql中
thiveInput->tmap->tMysqloutput thiveInput: tmap: tmysqlOutput:注意编码问题:noDatetimeStringSync=true ...
- ubuntu 安装python mysqldb
sudo apt-get install python-mysqldb #!/usr/bin/python #-*-coding:utf-8-*- ''' This file include all ...