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