您的位置:首页 > 其它

团体程序设计天梯赛-练习集 L2-010. 排座位

2016-07-06 12:21 357 查看
团体程序设计天梯赛-练习集

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  字符串 并查集