团体程序设计天梯赛-练习集
L2-010. 排座位
https://www.patest.cn/contests/gplt/L2-010
先用并查集,再分类讨论。
#include<iostream>
#include <cstdio>
using namespace std;
bool enemy[105][105];
int pre[105];
int Find(int a) {
if (a != pre[a])pre[a] = Find(pre[a]);
return pre[a];
}
void uion(int a, int b) {
int t1 = Find(a), t2 = Find(b);
if (t1 != t2)pre[t1] = t2;
}
void make(int n) {
for (int i = 1; i <= n; i++)pre[i] = i;
}
int main(void) {
int n, m, k;
scanf_s("%d%d%d", &n, &m, &k);
make(n);
while (m--) {
int a, b, g;
scanf_s("%d%d%d", &a, &b, &g);
if (g == 1)uion(a, b);
else enemy[a][b] = enemy[b][a] = 1;
}
while (k--) {
int a, b;
scanf_s("%d%d", &a, &b);
int ta = Find(a), tb = Find(b);
if (enemy[a][b]) {
if (ta == tb)printf("OK but...\n");
else printf("No way\n");
}
else {
if (ta == tb)printf("No problem\n");
else printf("OK\n");
}
}
return 0;
}