POJ 1703 Find them, Catch them

来源:互联网 发布:ppt 图标 知乎 编辑:程序博客网 时间:2024/05/29 14:50

种类并查集,高端大气上档次吧~~


题目大意:

一共有N个人,给出M个操作分为两种:

1、A   a  b  :提问a和b是否是同一个帮派的。有三种答案:是,不是和不确定

2、D  a  b   :a和b不是同一个帮派的。


解题思路:

种类并查集,在一个集合里的证明他们之间有关系,种类里有0,和1两种。0代表着同父节点相同势力,1代表不同。


下面是代码:

#include <stdio.h>int n,q;int fath[100005];int status[100005];void init(){    for(int i=1; i<=n; i++)    {        fath[i]=i;        status[i]=0;    }}int findd(int x){    if(fath[x]==x)return x;    else    {        int y=findd(fath[x]);        status[x]=(status[x]+status[fath[x]])&1;//x的阵营与fath[x]不同        fath[x]=y;        return y;    }}void SetUnion(int a ,int b){    int x=findd(a);    int y=findd(b);    if(x==y)return ;    fath[x]=y;    if(status[b])     {        status[x]=status[a];    }    else    {        status[x]=1-status[a];    }}void judge(int a,int b){    int x=findd(a);    int y=findd(b);    if(x!=y)printf("Not sure yet.\n");    else    {        if(status[a]==status[b])printf("In the same gang.\n");        else printf("In different gangs.\n");    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&q);        init();        char s[3];        int a ,b;        for(int i=0; i<q; i++)        {            scanf("%s%d%d",s,&a,&b);            if(s[0]=='D')            {                SetUnion(a,b);            }            else if(s[0]=='A')            {                judge(a,b);            }        }    }    return 0;}


0 0