poj 1703 Find them, Catch them(种类并查集和一种巧妙的方法)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 36176 | Accepted: 11090 |
Description
two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)
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.
Input
Output
Sample Input
1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
Sample Output
Not sure yet.
In different gangs. In the same gang. 题意:是一个城市有两个帮派,A 1 2是询问1和2这两个人是不是一个帮派,D 1 2是这两个人不在一个帮派
每一次A就输出一次
思路: 带权并查集,加一个数组re[]表示子节点和其父节点的关系,是一个门派就是0。不是就是1
2015,7,27#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define M 100100
int x[M],re[M];
void init()
{
for(int i=0;i<M;i++){
x[i]=i;
re[i]=0;
}
}
int find(int k)
{
int temp=x[k];
if(x[k]==k) return k;
x[k]=find(x[k]);
if(re[k]==re[temp])//假设和父节点是同类就存0
re[k]=0;
else re[k]=1;
return x[k];
}
void merge(int a,int b,int fa,int fb)
{
x[fa]=fb;
if(re[a]==re[b]) re[fa]=1;
else re[fb]=0;
}
int main()
{
int t,n,m,i,a,b,fa,fb;
char ch;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
while(m--){
cin>>ch;
scanf("%d%d",&a,&b);
fa=find(a);
fb=find(b);
if(ch=='A'){
if(fa!=fb){
printf("Not sure yet.\n");
continue;
}
if(re[a]==re[b]){
printf("In the same gang.\n");
continue;
}
else{
printf("In different gangs.\n");
}
}
else{
if(fa!=fb)
merge(a,b,fa,fb);
}
}
}
return 0;
}
/* 还有一种巧妙方法:大神的解析。是poj食物链那道题的弱化。 可见全部元素个数为2 * N,假设i表示属于帮派A。那么i + N表示属于帮派B。
每次输入两个家伙不在同一帮派的时候。就合并他们分属两个帮派的元素。 */
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define M 200200
int x[M];
void init()
{
for(int i=0;i<M;i++)
x[i]=i;
}
int find(int k)
{
if(x[k]==k) return k;
x[k]=find(x[k]);//压缩路径
return x[k];
}
void merge(int a,int b)
{
int fa=find(a);
int fb=find(b);
if(x[a]!=x[b]) x[fa]=fb;
}
bool same(int a,int b)
{
return find(a)==find(b);
}
int main()
{
int t,n,m,i,a,b,fa,fb;
char ch;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
while(m--){
cin>>ch;
scanf("%d%d",&a,&b);
if(ch=='A'){
if(same(a,b)){
printf("In the same gang.\n");
continue;
}
if(same(a,b+n)){
printf("In different gangs.\n");
}
else{
printf("Not sure yet.\n");
continue;
}
}
else{
merge(a,b+n);//a和b一定不在一个门派,就把a和b+n合并
merge(a+n,b);//同理
}
}
}
return 0;
}
poj 1703 Find them, Catch them(种类并查集和一种巧妙的方法)的更多相关文章
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- poj 1703 Find them, Catch them(并查集)
题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...
- poj 1703 Find them, Catch them 【并查集 新写法的思路】
题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...
- POJ 1703 Find them, Catch them (并查集)
题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派. 思路: 解法一: 编号划分 定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素 ...
- POJ 1703 Find them, Catch them(并查集拓展)
Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...
- POJ 1703 Find them, Catch them(并查集,等价关系)
DisjointSet保存的是等价关系,对于某个人X,设置两个变量Xa,Xb.Xa表示X属于a帮派,Xb类似. 如果X和Y不是同一个帮派,那么Xa -> Yb,Yb -> Xa... (X ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
随机推荐
- [LOJ#530]「LibreOJ β Round #5」最小倍数
[LOJ#530]「LibreOJ β Round #5」最小倍数 试题描述 第二天,LCR 终于启动了备份存储器,准备上传数据时,却没有找到熟悉的文件资源,取而代之的是而屏幕上显示的一段话: 您的文 ...
- 换教室(bzoj 4720)
Description 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节 课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节内容相同的 ...
- linux内核学习之四:进程切换简述【转】
转自:http://www.cnblogs.com/xiongyuanxiong/p/3531884.html 在讲述专业知识前,先讲讲我学习linux内核使用的入门书籍:<深入理解linux内 ...
- C++ 代码静态分析工具cppcheck【转】
转自:http://blog.csdn.net/chen19870707/article/details/42393217 权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] c ...
- LeetCode OJ——Word Ladder2
http://oj.leetcode.com/problems/word-ladder-ii/ class Solution { public: vector<vector<string& ...
- 微信小程序踩坑之一[wx.request]请求模式
最近在做小程序时,使用wx.request()方法请求时, 当使传输string类型时,一定要声明method请求模式为post,否则会一直报错,而不声明时默认为get, 已填坑 =,= wx.req ...
- zookeeper源码 — 四、session建立
目录 session建立的主要过程 客户端发起连接 服务端创建session session建立的主要过程 用一张图来说明session建立过程中client和server的交互 主要流程 服务端启动 ...
- Codeforces Round #321 (Div. 2) Kefa and Park 深搜
原题链接: 题意: 给你一棵有根树,某些节点的权值是1,其他的是0,问你从根到叶子节点的权值和不超过m的路径有多少条. 题解: 直接dfs一下就好了. 代码: #include<iostream ...
- SSM框架CRUD小案例
1.数据库准备 部门tbl_dept 员工tbl_emp 建立员工和部门的外键 2.在IDEA创建SSM项目环境 2.1配置Web模块 最上面的图是错误示范,注意!!! 在Tomcat配置了项目路径, ...
- C# 根据生日获取年龄
C# 根据生日获取年龄 根据生日计算出准确的年龄,不等于0时,返回的是岁,等于0时,返回的是天(以‘-’来区分) public static string GetAgeByBirth(string B ...