【题目链接】
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;
}