PTA L2-010 row seats (combined search)

L2-010 row of seats (25 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, it is impossible to 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 relationship 1 means friend, -1 means dead opponent. Note that two people cannot be both friends and enemies. In the last K lines, each line gives a pair of guest numbers to be queried.

It is assumed here that friends of friends are also friends. 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 one line of result for each query: if the 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 example:
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
Output example:
No problem
OK
OK but...
No way

analysis:

The most troublesome thing in this question is the phrase "a friend of a friend is also a friend". It is not clearly stated in the question. In fact, it is:
①If a friend of a friend is an enemy, it is not
a friend of a friend. Enemies) are all friends,
so only when these two rules are met can we use the combined search. If it is not clear at the beginning, it will be done as an ordinary simulation, so it is wrong many times.

Code:


#include <stdio.h>

using namespace std;

int father[107]; int Find(int a) {
    
    
	if (a != father[a])
		return father[a] = Find(father[a]);
	return a;
}

int main() {
    
    
	for (int i = 0; i<107; i++)
	{
    
    
		father[i] = i;
	}
	int n, m, k;
	bool en[107][107] = {
    
     0 };
	scanf("%d%d%d", &n, &m, &k);
	while (m--)
	{
    
    
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		if (c == 1)
		{
    
    
			int fa = Find(a);
			int fb = Find(b);
			if (fa != fb) father[fa] = fb;
		}
		else en[a][b] = en[b][a] = 1;
	}

	while (k--)
	{
    
    
		int a, b;
		scanf("%d%d", &a, &b);
		int fa = Find(a);
		int fb = Find(b);
		if (en[a][b])
		{
    
    
			if (fa == fb)puts("OK but...");
			else puts("No way");
		}
		else
		{
    
    
			if (fa == fb)puts("No problem");
			else puts("OK");
		}
	}
	return 0;
}


Guess you like

Origin blog.csdn.net/qq_43700916/article/details/88364245