PAT L2-010 排座位【并查集】

题目链接

题目意思

安排宴席座位,现在问你任意两个人能否安排同席。这里假设朋友的朋友也是朋友,敌人的敌人不一定是敌人,也不一定是朋友,只有单纯直接敌对关系的人才是绝对不能同席的。

解题思路

这就是一道并查集的问题,我们将不是单纯敌对关系的人连成一棵树,最后看两个人是否在同一棵树上即可。

代码部分
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int pre[150];
int maps[150][150];
int Find(int x)
{
    if(x!=pre[x])
        pre[x]=Find(pre[x]);
    return pre[x];
}
void mix(int x,int y)
{
    x=Find(pre[x]);
    y=Find(pre[y]);
    if(x==y)
        return;
    else
        pre[x]=y;
}
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1; i<=n; i++)
        pre[i]=i;
    int x,y,v;
    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d",&x,&y,&v);
        maps[x][y]=maps[y][x]=v;
        if(v==1)
            mix(pre[x],pre[y]);
    }
    int judge1,judge2;
    for(int i=0; i<k; i++)
    {
        scanf("%d%d",&judge1,&judge2);
        if(maps[judge1][judge2]==1)
            cout<<"No problem"<<endl;
        else if(maps[judge1][judge2]==-1)
        {
            if(Find(pre[judge1])==Find(pre[judge2]))
                cout<<"OK but..."<<endl;
            else
                cout<<"No way"<<endl;
        }
        else
            cout<<"OK"<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值