POJ 1703 Find themCatch them

这类题有一点类似,那就是需要定义状态,且题目越复杂,状态越多,如果状态多,定义状态的时候要与题目给出的关系有一定联系。
//
//  main.cpp
//  Richard
//
//  Created by 邵金杰 on 16/7/24.
//  Copyright © 2016年 邵金杰. All rights reserved.
//


#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100000+100;
int pa[maxn],re[maxn];
int getroot(int a)
{
    if(pa[a]==a) return a;
    int t=getroot(pa[a]);
    re[a]=(re[a]+re[pa[a]])%2;
    pa[a]=t;
    return pa[a];
}
void Union(int x,int y,int a,int b)
{
    pa[b]=a;
    re[b]=(1+re[y]+re[x])%2;
}
int main()
{
    int t,m,n,x,y;
    char cmd[10];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(int i=1;i<=m;i++) pa[i]=i,re[i]=0;
        for(int i=0;i<n;i++){
            scanf("%s%d%d",cmd,&x,&y);
            int a,b;
            a=getroot(x);
            b=getroot(y);
            if(cmd[0]=='D') Union(x,y,a,b);
            else{
                if(a!=b) printf("Not sure yet.\n");
                else{
                    if(re[x]==re[y]) printf("In the same gang.\n");
                    else printf("In different gangs.\n");
                }
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值