题目链接
解题思路:
并查集的应用
代码如下:
#include<iostream>
using namespace std;
const int maxn=500;
int f[maxn];
int a[maxn][maxn];
int find(int x)
{
if(f[x]==x)
return x;
else
{
f[x]=find(f[x]);
return f[x];
}
}
int hebing(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
{
f[y]=x;
}
}
int main()
{
int n,m,k,p;
int xx,yy;
int i;
int x,y;
cin>>n>>m>>k;
for(i=1;i<=n;i++)
f[i]=i;
for(i=0;i<m;i++)
{
cin>>x>>y>>p;
a[x][y]=p;
a[y][x]=p;
if(p==1)
hebing(x,y);
}
for(i=0;i<k;i++)
{
cin>>x>>y;
xx=find(x);
yy=find(y);//不加这两个不行
if(f[x]==f[y]&&a[x][y]!=-1)
cout<<"No problem"<<endl;
if(f[x]!=f[y]&&a[x][y]!=-1)
cout<<"OK"<<endl;
if(f[x]==f[y]&&a[x][y]==-1)
cout<<"OK but..."<<endl;
if(f[x]!=f[y]&&a[x][y]==-1)
cout<<"No way"<<endl;
}
return 0;
}