L2-010. 排座位(并查集)

【题目链接】
https://www.patest.cn/contests/gplt/L2-010

题目意思

给一群人的关系有敌对和友好,朋友的朋友也是朋友,现在你任意两个人的输出相应的语句。

解题思路

用并查集来处理朋友关系,用vector来存储敌对关系,判断时两个都对比下就好了

代码部分

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f
const int maxn=1e5+5;
int pre[maxn];
int unions(int root) ///查询最高父亲节点
{
    int son,tmp;
    son=root;
    while(root!=pre[root])
        root=pre[root];
    while(son!=root) ///压缩路径
    {
        tmp=pre[son];
        pre[son]=root;
        son=tmp;
    }
    return root;
}
void join(int root1,int root2) ///合并
{
    int x,y;
    x=unions(root1);
    y=unions(root2);
    if (x!=y)
        pre[x]=y;
}
int main()
{
    int n,m,k;
    vector<int>d[maxn];
    cin>>n>>m>>k;
    for (int i=1;i<=n;i++)
        pre[i]=i;
    for (int i=0;i<m;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        if (c==1)
            join(a,b);
        else
        {
            d[a].push_back(b);
            d[b].push_back(a);
        }
    }
    for (int i=0;i<k;i++)
    {
        int a,b,t1=0,t2=0;
        cin>>a>>b;
        if(unions(a)==unions(b))
            t1=1;
        for (int j=0;j<d[a].size();j++)
        {
            if (d[a][j]==b)
                t2=1;
        }
        if (t1==1&&t2==0)
            cout<<"No problem"<<endl;
        else if(t1==0&&t2==0)
            cout<<"OK"<<endl;
        else if(t1==1&&t2==1)
            cout<<"OK but..."<<endl;
        else cout<<"No way"<<endl;
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值