7-9 rows of seats (25 minutes)

The subject have a question, do not understand why the puts replace printf ( "*** \ n") is not correct, black magic? ? ? ?

 

 

 

Arrangement banquet most delicate thing, is to give you a reference banquet guests to the seating arrangements. In any case, we can not put two rival discharged with a banquet table! This arduous task is given to you now, for any of the guests, please write programs tell the owner whether they can be arranged with the seats.

Input formats:

The first input line 3 is given a positive integer: Nthe total number (≦ 100), i.e., to come to the reference dinner guests, these people from 1 to Nnumber; Mis a known relationship between the number of pairwise guests; Ka strip query number. Subsequently Mlines, each line gives guests the relationship between a pair of the form: 宾客1 宾客2 关系, where 关系1 indicates that a friend, -1 indicates a rival. Note that two people can not be both friend and foe. The last Klines of numbers given one pair of guests to be queried.

It is assumed that friend of a friend is a friend. But the enemy of my enemy is not necessarily a friend, a friend of the enemy is not necessarily the enemy. Only simple direct hostile relations is absolutely not the same seats.

Output formats:

The results for each query output line: If the guests are between two friends, and not an adversarial relationship, the output No problem; if not friends among them, but not hostile, then the output OK; if there is hostility between them, but there are mutual friends, the output OK but...; if only hostile relations between them, the output No way.

Sample input:

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<cmath>
#include<algorithm>
using namespace std;
const int N=110;
int s[N][N];
int f[N];
int find(int x){
    if(x==f[x])return x;
    return f[x]=find(f[x]);
}
void merge(int x,int y){
    int t1=find(x);
    int t2=find(y);
    if(t1!=t2)f[t1]=t2;
}
int main(){
    int n,m,k,x,y,z;
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)f[i]=i;
    while(m--){
        cin>>x>>y>>z;
        s[x][y]=s[y][x]=z;
        if(z==1)merge(x, y);
    }
    while(k--){//疑问:不明白为什么将puts换为printf("****\n")就不对了
        cin>>x>>y;
        if(s[x][y]==1)puts("No problem");
        else if(s[x][y]==-1){
            if(find(x)==find(y))puts("OK but...");
            else puts("No way");
        }else{
            if(find(x)==find(y))printf("No problem");
            else puts("OK");
        }
        
    }
    return 0;
}

 

Published 32 original articles · won praise 7 · views 793

Guess you like

Origin blog.csdn.net/Young_Naive/article/details/104185743