题干:

Find them, Catch them

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36176   Accepted: 11090

Description

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given 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

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

Output

For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."

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是认为这两个人不在一个帮派是true的(即认为他俩不在一个帮派) 每一次A就输出一次 思路: 带权并查集,加一个数组rank[]表示子节点和其父节点的关系,是一个门派就是0,不是就是1

(ps:用G++语言提交,不让用rank做数组名,,也是很不友好的)

解题报告:

这题是带权并查集的一个变种,叫做种类并查集,上一次训练的题有一个虫子谈恋爱的,判断是不是同性恋(POJ - 2492),还有最经典的种类并查集食物链,都是种类并查集,权均代表与祖先节点之间的关系。使用的一种向量思维把各种给定的情况用权值分开表示。比如此题rank[0]代表与祖先节点是同一帮派,rank[1]代表与祖先节点是不同帮派。

AC代码1:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>using namespace std;
const int MAX = 1e5 + 5;
int n,m;
bool rank[MAX];
int f[MAX];
int getf(int v) {if(f[v] == v) return v;int tmp=f[v];f[v] = getf(f[v] );rank[v] = (rank[v] + rank[tmp] ) %2;return f[v];
}
void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);if(t1!=t2) {f[t2] = t1;rank[t2] = (rank[u]+rank[v]+1)%2;//放到if外面可以吗   应该不行吧   你让老大和自己的关系 通过u和v这两个小喽啰来改变?应该不行吧,,要改变这个老大的rank 只能让更老大的巨擘来当这个老大的头头。然后rank跟着巨擘来改变。 }
}
void init() {for(int i = 1; i<=n; i++) {f[i]=i;rank[i]=0;}
}
int main()
{int t;char op[5];cin>>t;while(t--) {int u,v; scanf("%d %d",&n,&m);init();while(m--) {scanf("%s",op);scanf("%d %d",&u,&v);if(op[0] == 'D') {merge(u,v); }else {if(getf(u) != getf(v) ) printf("Not sure yet.\n");else {if(rank[u]==rank[v]) printf("In the same gang.\n");else printf("In different gangs.\n");}   }}  }   return 0 ;}

总结:

1. emmm 别忘了输出最后的句点哈、、、、

【POJ - 1703】Find them, Catch them(带权并查集之--种类并查集 权为与父节点关系)相关推荐

  1. POJ 1703 Find them, Catch them(路径压缩并查集)

    POJ 1703 Find them, Catch them(路径压缩并查集) 2014年03月11日 20:13:54 阅读数:881 POJ 1703 Find them, Catch them( ...

  2. POJ 1703 Find them, Catch them(并查集高级应用)

    POJ 1703 Find them, Catch them(并查集高级应用) 手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/sunc ...

  3. POJ 1703 Find them, Catch them

    简单带权并查集0,1关系 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio& ...

  4. POJ 1703 Find them, Catch them 并查集

    题意:给你t组数据,每组数据给你编号为1-n的坏人,这些坏人要么属于团伙A,要么属于团伙B,然后给你m次操作: A操作:询问x和y是不是同一个团伙 D操作:告诉你x和y不是同一个团伙 思路:和POJ ...

  5. POJ 1703 Find them, Catch them【并查集】

    题意: 有 N 个人分属于两个帮派,对应两种操作: A   X Y      询问x,y 是否属于一个帮派,或两者关系不能确定. D   X Y      X和Y 分属不同帮派 分析: 感觉就是简化版 ...

  6. POJ 1703 Find them, Catch them 种类并查集

    题意 给出一堆点和关系 D为两点不同集合 A为查询两点是否不同集合 n<=1e5 code #include<cstdio> #include<iostream> #in ...

  7. Poj(1703),种类并查集

    题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...

  8. 一文带你掌握OBS的两种常见的鉴权方式

    OBS提供了REST(Representational State Transfer)风格API,支持您通过HTTP/HTTPS请求调用.在调用OBS的API前,需要了解OBS的鉴权认证方式.本文就将 ...

  9. c语言编辑87152,POJ 3287 (基础BFS) Catch That Cow

    这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE) (1) 把初始节点S0放入Open表中: (2) 如果Open表为空,则问题无解,失败 退出: (3) 把Open表的 ...

最新文章

  1. Linux----进程概念
  2. 团队作业8——第二次项目冲刺(Beta阶段)--第六天
  3. 代理(Proxy)及常见示例
  4. linux下如何修改根口令
  5. DNS服务器全面解析--转
  6. Spring Boot 配置文件密码加密方法
  7. 【IT资讯】继哈工大Matlab软件被美禁用后,华为、360再遭Docker软件禁令
  8. go-mysql查询单条数据_Golang 从 MySQL 数据库读取一条数据
  9. 33 CO配置-控制-产品成本控制-成本对象控制-期末结算-定义每一工厂的差异码
  10. Linux下通配符总结
  11. Android开发笔记(一百六十四)仿京东首页的下拉刷新
  12. 分享5个宝藏文字转语音配音软件,错过太可惜
  13. 如何将在ad里面添加元器件符号_请教怎么在AD10中的PCB中直接加入自己创建的封装库文件中的元件...
  14. HTML实现文件上传和HTML实现打开文件目录
  15. 浅析web应用防火墙的反向代理部署
  16. vue 表单验证正则_vue表单验证
  17. 定解问题(一)| 通解与特解 + 适定性 | 偏微分方程(五)
  18. centos7 安装显卡驱动及cuda10.2
  19. 基于STL的演讲比赛流程管理系统
  20. 安徽省太和一中2021高考成绩查询分数,2021届安徽省太和一中高三物理高考二模试题 Word版含答案...

热门文章

  1. [Leedcode][JAVA][第25题][K个一组反转链表][链表][递归]
  2. 生产系统服务器是啥意思,生产系统服务器主机名怎么看
  3. 前端对所有文件请求添加header_【前端面试必问】浏览器缓存原理?送你满分答案...
  4. druid 非对称加密_springboot配置文件中mysql的密码进行加密
  5. 隐藏该监视器无法显示模式_【春星开讲 | 9137】达芬奇4K调色监看的好伙伴——明基PD2720U专业显示器...
  6. springboot接收json参数_Springboot + Vue + shiro 实现前后端分离、权限控制
  7. Snap svg:路径变换和相交计算
  8. mysql 操作xm_mysql基本命令使用
  9. 韦东山 IMX6ULL和正点原子_GPIO和Pinctrl子系统的使用在100ASK_IMX6ULL上机实验
  10. Linux下rgmii接口与fpga相连,FPGA控制RGMII接口PHY芯片88E1512网络通信