L2-010 Row of seats (and look up)

topic link

https://pintia.cn/problem-sets/994805046380707840/problems/994805066135879680

ideas

For the relationship between friends and friends, we can maintain it through union search , and then for the relationship between enemies, we only need to use a two-dimensional array or map to maintain it, because only the direct hostile relationship is the enemy, and then For each inquiry, we can classify and discuss according to their friendship and hostility.

code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define endl "\n"
#define PII pair<int,int>
#define INF 0x3f3f3f3f

const int N = 1e2+10;

int fa[N],n,m,k;
bool enmy[N][N];
int find(int x){
    
    
	return x==fa[x]?x:find(fa[x]);
}

int main()
{
    
    
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m>>k;
	for(int i = 1;i <= n; ++i) fa[i] = i;
	int u,v,w;
	for(int i = 1;i <= m; ++i) {
    
    
		cin>>u>>v>>w;
		if(w + 1) {
    
    
			u = find(u);
			v = find(v);
			fa[v] = u;
		} else {
    
    
			enmy[u][v] = enmy[v][u] = true;
		}
	}
	for(int i = 1;i <= k; ++i) {
    
    
		cin>>u>>v;
		if(enmy[u][v]) {
    
    
			if(find(u) == find(v)) cout<<"OK but..."<<endl;
			else cout<<"No way"<<endl;
		} else {
    
    
			if(find(u) == find(v))
				cout<<"No problem"<<endl;
			else  cout<<"OK"<<endl;
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_46201544/article/details/123805950