输入样例
7 8 4
5 6 1
2 7 -1
1 3 1
3 4 1
6 7 -1
1 2 1
1 4 1
2 3 -1
3 4
5 7
2 3
7 2
输出样例
No problem
OK
OK but...
No way
c++代码
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110;
int n, m, k;
int p[N], g[N][N];
//并查集
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) p[i] = i;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
g[a][b] = g[b][a] = c;
if (c == 1) {
if (find(a) != find(b)) p[find(a)] = find(b);
}
}
while (k--) {
int a, b;
cin >> a >> b;
if (find(a) == find(b) && g[a][b] != -1) cout << "No problem" << endl;
else if (find(a) != find(b) && g[a][b] != -1) cout << "OK" << endl;
else if (find(a) == find(b) && g[a][b] == -1) cout << "OK but..." << endl;
else if (find(a) != find(b) && g[a][b] == -1) cout << "No way" << endl;
}
return 0;
}