Find them, Catch them
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 41463 | Accepted: 12753 |
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
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.
Source
题目链接:POJ 1703
主要思路:题目跟食物链不一样,都是真话,也不会存在前后矛盾的情况,那么只要考虑题目中说的两种情况;
1、A a b,问你a与b的的关系,那显然分成两种情况考虑
a与b的根不同即无关(根都不同了都不在一个集合内肯定无关)
a与b根相同即在一个集合内(有关系),用公式求关系值判断这俩的帮派是1(不同)还是0(相同)
2、D a b,指定了a与b不同帮派即这俩关系之间的值要用1代入,那合并的时候a->b的值用1代即可,然后就可以水过了
代码:
#include<iostream> #include<algorithm> #include<cstdlib> #include<sstream> #include<cstring> #include<bitset> #include<cstdio> #include<string> #include<deque> #include<stack> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define INF 0x3f3f3f3f #define CLR(x,y) memset(x,y,sizeof(x)) #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) typedef pair<int,int> pii; typedef long long LL; const double PI=acos(-1.0); const int N=100010; struct info { int rela; int pre; }; info node[N]; void init(int n) { for (int i=0; i<=n; ++i) { node[i].rela=0; node[i].pre=i; } } int find(int n) { if(node[n].pre==n) return n; int tpre=node[n].pre; node[n].pre=find(node[n].pre); node[n].rela=(node[n].rela+node[tpre].rela)%2; return node[n].pre; } void joint(int a,int b) { int fa=find(a),fb=find(b); node[fb].pre=fa; node[fb].rela=(node[a].rela+1-node[b].rela+2)%2; } int same(int a,int b) { int fa=find(a),fb=find(b); if(fa==fb) return (node[a].rela==node[b].rela); else return -1; } int main(void) { int tcase,i; int n,m,a,b; char ops[3]; scanf("%d",&tcase); while (tcase--) { scanf("%d%d",&n,&m); init(n); for (i=0; i<m; ++i) { scanf("%s%d%d",ops,&a,&b); if(ops[0]=='A') { switch(same(a,b)) { case 0:puts("In different gangs.");break; case 1:puts("In the same gang.");break; case -1:puts("Not sure yet.");break; } } else joint(a,b); } } return 0; }