Description
The Police OfficeinchTadu City decides to say ends to the chaos, asLaunch actions to root up the gangsinchThe city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given, criminals; DoThey belong to a same clan?You must give your judgment based on incomplete information. (Since The gangsters is always acting secretly.) Assume N (n<=Ten^5) Criminals is currentlyinchTadu City, numbered from 1To N. And of course, at least one of them belongs to Gang Dragon, and the same forGang Snake. You'll be given m (m <=Ten^5) messagesinchSequence, which isinchThe following-kinds:1. D [A] [b]where[A] and [b] is the numbers of the criminals, and they belong to different gangs.2. A [a] [b]where[A] and [b] is the numbers of the criminals. This requires the decide whether a and B belong to a same gang.
Input
The first line of the input contains a single integer T (1 -case as described above.
Output
" a [a] [b] " inch Case " In the same gang. " " In different gangs. " " Not sure yet. "
Sample Input
1 5 5 121 2 1 2 2 414
Sample Output
Not sure yet. In different gangs. In the same gang.
Source
POJ monthly--2004.07.18
and check the set, use a OPP array to record the opposing opponents, and then do the merge with the check set
at first init () function forgot to add, oneself always so careless ... And when judging a, consider the same and different, and finally the rest is uncertain.
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineN 1000066 intn,m;7 intOpp[n],fa[n];8 voidinit () {9 for(intI=0; i<n;i++){Tenopp[i]=0; Onefa[i]=i; A } - } - intFindintx) { the returnfa[x]==x?x:fa[x]=find (Fa[x]); - } - voidMergeintXinty) { - introot1=find (x); + intRoot2=find (y); - if(ROOT1==ROOT2)return; +fa[root1]=Root2; A } at intMain () - { - intT; -scanf"%d",&t); - while(t--){ - init (); inscanf"%d%d",&n,&m); - Chars[3]; to intx, y; + for(intI=0; i<m;i++){ -scanf"%s", s); the if(s[0]=='A'){ *scanf"%d%d",&x,&y); $ if(Find (x) = =find (Y)) {Panax Notoginsengprintf"In the same gang.\n"); -}Else if(Find (x) = =find (Opp[y])) { theprintf"In different gangs.\n"); +}Else{ Aprintf"Not sure yet.\n"); the } + } - Else{ $scanf"%d%d",&x,&y); $ if(opp[x]==0&& opp[y]==0){ -opp[x]=y; -opp[y]=x; the } - Else if(opp[x]==0){Wuyiopp[x]=y; the merge (X,opp[y]); - } Wu Else if(opp[y]==0){ -opp[y]=x; About merge (Y,opp[x]); $ } - Else{ - merge (X,opp[y]); - merge (Y,opp[x]); A } + } the } - } $ return 0; the}
View Code
POJ 1703 Find Them, Catch them (and set)