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

20 篇文章 0 订阅

//AC

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <ctype.h>
using namespace std;
typedef long long ll;
int pre[110];
int Map[110][110];
int find (int n){
	int r;
	r =n;
	while(pre[r]!=r){
		r=pre[r];
	}
	return r;
}
int Union(int x,int y){
	int xx;
	int yy;
	xx=find(x);
	yy=find(y);
	if(xx!=yy){
		pre[xx]=yy;
	}
}
int main() {
    int n,m,k;
    int a,b,c;
    int x,y; 
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
    	pre[i]=i;
	}
	memset(Map,0,sizeof(Map));  //原始为0状态默认朋友关系 或者无关系 
	while(m--){
		cin>>a>>b>>c;
		   if(c==1){
			Union(a,b);
          }
          else{
          	Map[a][b]=1;  //  标记1 为敌对关系 
            Map[b][a]=1;  //  标记1 为敌对关系 
		  }
	}
	while(k--){
		cin>>x>>y;
		if(Map[x][y]==0&&find(x)==find(y)){      //朋友关系(不敌对)上司一样 
			cout<<"No problem"<<endl;
		}
		else if(Map[x][y]==0&&find(x)!=find(y)){    //不是朋友(不敌对)关系上司不一样 
			cout<<"OK"<<endl;
		}
		else if(Map[x][y]==1){    
			if(find(x)==find(y)) {          //有敌对,有共同朋友 
			  cout<<"OK but..."<<endl;                    
			}
	        else
			cout<<"No way"<<endl;          //只有敌对关系 
		}
	} 
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值