题目意思
安排宴席座位,现在问你任意两个人能否安排同席。这里假设朋友的朋友也是朋友,敌人的敌人不一定是敌人,也不一定是朋友,只有单纯直接敌对关系的人才是绝对不能同席的。
解题思路
这就是一道并查集的问题,我们将不是单纯敌对关系的人连成一棵树,最后看两个人是否在同一棵树上即可。
代码部分
#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;
}