PTA row seats (25 points) (combined search)

It is the human mind that releases infinite light, and it is also the human mind that creates boundless darkness. Light and darkness are intertwined and fight together. This is the world for which we are nostalgic and helpless.

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 Nnumbered from 1 to number; Mis the number of known relationships between two guests; Kis the query item number. In the subsequent Mlines, each line gives the relationship between a pair of guests. The format is:, 宾客1 宾客2 关系where 关系1 means friend, -1 means dead opponent. Note that two people cannot be both friends and enemies. In the last Kline, 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 a row of results 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 there is hostility between them, but there is also Common friends are output OK but...; if they only have a hostile relationship, they are 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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
typedef long long ll;
using namespace std;
int const mod=1e9+7;
const int maxn=1e4+10;
int n,m,k,x,y,z,f[110],mp[110][110];
void init(){
    for(int i=1;i<=n;i++)
        f[i]=i;
}
int fd(int xx){
    if(f[xx]==xx)
        return f[xx];
    f[xx]=fd(f[xx]);
    return f[xx];
}
void hb(int xx,int yy){
    int fx=fd(xx);
    int fy=fd(yy);
    if(fx!=fy)
        f[fy]=fx;
}
int main(){
    int cx,cy;
    cin>>n>>m>>k;
    init();
    while(m--){
        cin>>x>>y>>z;
        if(z==-1)
            mp[x][y]=mp[y][x]=-1;
        else
            hb(x,y);
    }
    for(int i=0;i<k;i++){
        cin>>cx>>cy;
        if(fd(cx)==fd(cy)&&mp[cx][cy]==0)
            cout<<"No problem"<<endl;
        else if(fd(cx)!=fd(cy)&&mp[cx][cy]==0)
            cout<<"OK"<<endl;
        else if(fd(cx)==fd(cy)&&mp[cx][cy]==-1)
            cout<<"OK but..."<<endl;
        else if(fd(cx)!=fd(cy)&&mp[cx][cy]==-1)
            cout<<"No way"<<endl;
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44170305/article/details/108563303