3-2 rows of seats (20 points)

The most subtle thing about setting up a banquet is to arrange seats for all the guests who come to the banquet. In any case, you can't line up two rivals at the same banquet table! This arduous task is now entrusted to you. For any pair of guests, please write a program to tell the host whether they can be arranged to sit together.

Input format:
Enter the first line to give 3 positive integers: N (≤100), that is, the total number of guests who came to the banquet, then these people are numbered from 1 to N; M is the known relationship between the two guests Number; K is the number of queries. Then M lines, each line gives the relationship between a pair of guests, the format is: guest 1 guest 2 relationship, where the relationship is 1 means friends, -1 means dead opponents. Note that two people cannot be both friends and enemies. In the last K lines, each line gives a pair of guest numbers that need to be queried.

It is assumed here that a friend of a friend is also a friend. But the enemy of the enemy is not necessarily a friend, and the enemy of a friend is not necessarily an enemy. Only a purely direct hostile relationship is absolutely impossible to sit together.

Output format:
output a row of results for each query: if two guests are friends and there is no hostile relationship, then output No problem; if they are not friends but not hostile, then output OK; if they are not hostile If there is hostility between them, but there are also friends in common, then output OK but...; if they only have hostile relationship, output No way.

Input sample:

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

Sample output:

No problem
OK
OK but…
No way

A real question in the ladder competition in previous years, it is not difficult, and to check the application of the collection, if the relationship is 1, then the collection is merged, otherwise it is stored, and the hostile relationship between the two is recorded with a two-dimensional array.
#include <iostream>
using namespace std;

int f[101]; //并查集数组
int di[101][101];

void init() {
    
    
    for (int i = 1; i <= 100; i++) {
    
    
        f[i] = i;
    }
}

int find(int x) {
    
    
    if (x == f[x])
        return x;
    else
        return f[x] = find(f[x]);
}

void merge(int x, int y) {
    
    
    int a = find(x);
    int b = find(y);
    if (a != b) {
    
    
        f[b] = a;
    }
}

int main() {
    
    
    int n, m, k;
    cin >> n >> m >> k;
    init();
    int g1, g2, ship;
    while (m--) {
    
    
        cin >> g1 >> g2 >> ship;
        if (ship == 1)
            merge(g1, g2);
        else
            di[g1][g2] = di[g2][g1] = 1;
    }
    while (k--) {
    
    
        cin >> g1 >> g2;
        int a = find(g1);
        int b = find(g2);
        if (di[g1][g2] == 1) {
    
    
            if (a == b)
                cout << "OK but..." << endl;
            else
                cout << "No way" << endl;
        } else {
    
    
            if (a == b)
                cout << "No problem" << endl;
            else
                cout << "OK" << endl;
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/111569442