首页 > 代码库 > poj 1703

poj 1703

//  hnldyhy(303882171)  17:07:57//  poj 1703#include <stdio.h>int p[200005];void init(int n){    for (int i=1;i<=2*n;i++)    p[i]=i;}int find(int x){    if (x==p[x]) return x;    return  p[x]=find(p[x]);}int main(){    int n,m,a,b,test;    char c[10];    scanf("%d",&test);    while (test--)    {        scanf("%d%d",&n,&m);        init(n);        for (int i=1;i<=m;i++)        {            scanf("%s%d%d",c,&a,&b);            if (c[0]==A)            {                if ( find(a)!=find(b) && find(a)!=find(b+n) )                    printf("Not sure yet.\n");                else if  ( find(a)==find(b) ) printf("In the same gang.\n");                else  printf("In different gangs.\n");            }            else  //‘D  a  b‘            {                if ( find(a)!=find(b+n) )                {                    p[find(a)]=find(b+n); //不在同一集合                    p[find(b)]=find(a+n); //不在同一集合                }            }        }    }    return  0;}**********************************************************************芯馨(396842689)  17:25:53#include<iostream>#include<cstdio>using namespace std;#define N 500001int ei[N],rank[N];void init(int n){    for (int i=1;i<=n;i++)        ei[i]=i,rank[i]=0;}int find(int x){    if (ei[x]==x) return x;    int tt=ei[x];    ei[x]=find(ei[x]);    rank[x]=(rank[x]+rank[tt])%2;    return ei[x];}void set(int x,int y){    int xx=find(x),yy=find(y);    if (xx!=yy)    {        ei[yy]=xx;        rank[yy]=(rank[x]+rank[y]+1)%2;    }}int main(){    char ch;    int i,x,y;    int n,m;    int t;    scanf("%d",&t);    while (t--)    {        scanf("%d%d",&n,&m);        init(n);        for (i=1;i<=m;i++)        {            scanf("\n%c %d %d",&ch,&x,&y);            if (ch==D)                set(x,y);            else            {                int xx=find(x),yy=find(y);                if (xx!=yy)                    printf("Not sure yet.\n");                else                {                    if (rank[x]==rank[y])                        printf("In the same gang.\n");                    else                        printf("In different gangs.\n");                }            }        }    }    return 0;}