Find them, Catch them(POJ-1703)
来源:互联网 发布:输出素数的c语言程序 编辑:程序博客网 时间:2024/06/01 21:53
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."
Sample Input
15 5A 1 2D 1 2A 1 2D 2 4A 1 4
Sample Output
Not sure yet.In different gangs.In the same gang.
题意:如题,找到他们,杀死他们。。。。
警察要肃清黑帮,有两个帮派,看看这D是两个人是对立的,A是查询是不是一个帮派的;
思路:对立的并查集;
ps;数组开小了,WA了两次。。。
#include<iostream>#include<cstdio>using namespace std;int a[100005],b[100005];int find(int x){ if(a[x]==x) return x; int r=find(a[x]); b[x]=(b[x]+b[a[x]])&1; a[x]=r; return a[x];}void link(int x,int y){ int fx=find(x),fy=find(y); if(fx!=fy){ a[fx]=fy; b[fx]=(b[x]+b[y]+1)&1; }}int main(){ int t,m,n,x,y; char s,c; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(int i=0;i<=n;i++){ a[i]=i; b[i]=0; } for(int i=0;i<m;i++){ scanf("%*c%c",&s); scanf("%d%d",&x,&y); if(s=='A'){ int fx=find(x),fy=find(y); if(fx!=fy) printf("Not sure yet.\n"); else{ if(b[x]==b[y]) printf("In the same gang.\n"); else printf("In different gangs.\n"); } } if(s=='D'){ link(x,y); } } } return 0;}
阅读全文
0 0
- Find them, Catch them(POJ-1703)
- POJ 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ-1703 Find them, Catch them
- Poj 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ 1703 - Find them, Catch them
- POJ 1703 Find them, Catch them
- POJ 1703(Find them, Catch them)
- POJ 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ 1703: Find them, Catch them
- poj 1703 find them,catch them
- POJ--1703--Find them, Catch them
- LeetCode 206. Reverse Linked List *****三指针 (pre,cur,next)
- js基本算法--冒泡排序法、快速排序法、插入排序法、二分查找法(递归、非递归)
- 51nod 1154 回文串划分(dp)
- hdoj1016(深度遍历)
- spring事务
- Find them, Catch them(POJ-1703)
- PHP开发之字符串长度以及字符串子串截取相关函数总结
- 重拾Hibernate框架——实体类、主键生成策略、session绑定本地线程
- TensorFlow学习笔记(三):深层神经网络
- java中extends和implements的不同
- Python学习(8)、定义函数
- 解码大数据征信
- jquery实现上下滑动图片
- RF(随机森林)算法原理及其派生算法