poj1703

来源:互联网 发布:qq群怎么优化排名 编辑:程序博客网 时间:2024/05/21 09:37
Find them, Catch them
Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 32383
Accepted: 10002

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

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.

Source

POJ Monthly--2004.07.18



题意:有两个帮派,给出D  a   b 表示a和b不属于同一个帮派,给出A  a   b 要求输出a和b的关系,分别为a  b                同属于一个帮派、a  b  为不同帮派、尚不明确。

思路: 此题不同之处在于只给出了两个帮派,可以开出两倍的数组,分别表示i号成员在第一帮派和第               二帮派的存在情况(可以理解为i号的正面和反面 ,若i号和j号不在同一个帮派,那么i的正面和j的反面在同意帮派,i的反面和j的正面在同一帮派)。 因此给出D a b则a和b+n,b和a+n在同一 帮派。 初始化的时候数组ith赋值为i。

代码如下:
#include<iostream>#include<string.h>#include<stdio.h>using namespace std;int f[200050];                    //开出两倍n的数组int find(int m)                  //寻找所在的帮派{     return f[m]=(f[m]==m)? m:find(f[m]);}void mix(int a,int b)            //合并两个在同一个帮派的人{     f[find(a)]=find(b);}int main(){     int i,m,n,t,b,c;     char a;     scanf("%d",&t);     while(t--)     {          scanf("%d%d",&n,&m);          for(i=0;i<=n*2;i++)               f[i]=i;          while(m--)          {               getchar();               scanf("%c%d%d",&a,&b,&c);               if(a=='D')               {                    mix(b,c+n);         //b和c+n在同一帮派                    mix(c,b+n);        //c和b+n在同一帮派               }               else               {                    if(find(c)==find(b))                         printf("In the same gang.\n");                    else if(find(c)==find(b+n)||find(c+n)==find(b))                         printf("In different gangs.\n");                    else                         printf("Not sure yet.\n");               }          }     }     return 0;}


poj2492
#include <queue>#include <stack>#include <math.h>#include <vector>#include <limits.h>#include <stdio.h>#include <iostream>#include <string.h>#include <algorithm>#include <functional>using namespace std;int f[4050];int find(int m){     return f[m]=(m==f[m])? m:find(f[m]);}void mix(int a,int b){     f[find(a)]=find(b);}int main(){    int t,j,m,n,i;    scanf("%d",&t);    for(i=1;i<=t;i++)    {         scanf("%d%d",&n,&m);         for(j=0;j<=n*2;j++)              f[j]=j;         bool flag=false;         while(m--)         {              int a,b;              scanf("%d%d",&a,&b);              if(flag)                   continue;              if(find(a)==find(b))                   flag=true;              else                    mix(a,b+n),mix(b,a+n);         }         if(i>1)             printf("\n");         printf("Scenario #%d:\n",i);         if(flag)              printf("Suspicious bugs found!\n");         else              printf("No suspicious bugs found!\n");    }    return 0;}




0 0
原创粉丝点击