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."

题目大意:有n个人,D a b表示a b位于不同的集合,A a b则代表问a b是否属于同一个集合(在线提问)。

思路:并查集拓展的简单应用,不多讲。就用rela[x]表示x与当前父节点(不一点是根节点)是否相同。

代码(313MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = ; int fa[MAXN];
bool rela[MAXN]; int get_set(int x) {
if(x == fa[x]) return x;
else {
int ret = get_set(fa[x]);
rela[x] ^= rela[fa[x]];
return fa[x] = ret;
}
} void unionSet(int x, int y) {
int fx = get_set(x);
int fy = get_set(y);
fa[fy] = fx;
rela[fy] = ^ (rela[x] ^ rela[y]);
} void query(int x, int y) {
int fx = get_set(x);
int fy = get_set(y);
if(fx != fy) puts("Not sure yet.");
else if(rela[x] == rela[y]) puts("In the same gang.");
else puts("In different gangs.");
} int main() {
int T, n, m, x, y;
char c;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
fa[i] = i, rela[i] = ;
while(m--) {
scanf(" %c%d%d", &c, &x, &y);
if(c == 'D') unionSet(x, y);
else query(x, y);
}
}
}

最新文章

  1. Linux系统有7个运行级别(runlevel)
  2. ssh scp ssh-copy-id 非22端口的操作方法
  3. c++之RTTI介绍
  4. 洛谷 P1508 Likecloud-吃、吃、吃
  5. Eclipse启动时报需要安装&quot;Java SE 6 Runtime&quot;致无法启动解决方案
  6. VMware网络选项分析
  7. Linux Tomcat7.0安装配置实践总结
  8. 回文(manacher)
  9. 在家用机上搭建 Git https 服务器
  10. 谱聚类(Spectral clustering)(2):NCut
  11. OWIN 自宿主模式WebApi项目,WebApi层作为单独类库供OWIN调用
  12. POJ 2472 106 miles to Chicago(Dijstra变形——史上最坑的最长路问题)
  13. fireBug引入JQuery,方便书写jq调试代码
  14. java基础小项目练习之1----3天做出飞机大战
  15. mybatis缓存机制
  16. C# Json.Net解析实例
  17. 【Git】 GitLab配置优化及汉化
  18. Virtual DOM的简单实现
  19. Jury Compromise(poj 1015)
  20. python网络编程相关

热门文章

  1. vue2高仿饿了么app
  2. 菜鸟笔记 -- Chapter 6.1&#160;面向对象概述
  3. win7 bat copy 一个文件 到另外的文件夹内,路径得用引号哦
  4. 使用 seafile搭建私有云盘
  5. ORACLE 账户解除锁定
  6. ios appstore 上架应用被拒绝原因
  7. django-模板层基础2
  8. multimap的使用
  9. 关于 &#39;list&#39; object has no attribute &#39;select&#39;
  10. 针对angularjs下拉菜单第一个为空白问题处理