2019年12月PAT甲级 第三题 Summit(1166)题解

题目

A summit (峰会) is a meeting of heads of state or government. Arranging the rest areas for the summit is not a simple job. The ideal arrangement of one area is to invite those heads so that everyone is a direct friend of everyone.

Now given a set of tentative arrangements, your job is to tell the organizers whether or not each area is all set.

Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 200), the number of heads in the summit, and M, the number of friendship relations. Then M lines follow, each gives a pair of indices of the heads who are friends to each other. The heads are indexed from 1 to N.

Then there is another positive integer K (≤ 100), and K lines of tentative arrangement of rest areas follow, each first gives a positive number L (≤ N), then followed by a sequence of L distinct indices of the heads. All the numbers in a line are separated by a space.

Output Specification:
For each of the K areas, print in a line your advice in the following format:

if in this area everyone is a direct friend of everyone, and no friend is missing (that is, no one else is a direct friend of everyone in this area), print Area X is OK…

if in this area everyone is a direct friend of everyone, yet there are some other heads who may also be invited without breaking the ideal arrangement, print Area X may invite more people, such as H. where H is the smallest index of the head who may be invited.

if in this area the arrangement is not an ideal one, then print Area X needs help. so the host can provide some special service to help the heads get to know each other.

Here X is the index of an area, starting from 1 to K.

Sample Input:
8 10
5 6
7 8
6 4
3 6
4 5
2 3
8 2
2 7
5 3
3 4
6
4 5 4 3 6
3 2 8 7
2 2 3
1 1
2 4 6
3 3 2 1

Sample Output:
Area 1 is OK.
Area 2 is OK.
Area 3 is OK.
Area 4 is OK.
Area 5 may invite more people, such as 3.
Area 6 needs help.

解释

本题与1142同,首先判断是否是各个点彼此相连,若相连判断是否有其它顶点与集合中的顶点都直接相连。最终得到三种情况并输出。

正确答案

#include<iostream>
#include<climits>
#include<cctype>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
bool e[205][205];
int main(){
    int n, m , k, a ,b;
    cin >> n >> m;
    fill(e[0], e[0] + 205*205, false);
    for(int i = 0; i < m; i++){
        cin >> a >> b;
        e[a][b] = true;
        e[b][a] = true;
    }
    cin >> k;
    for(int i = 1; i <= k; i++){
        bool flag1 = true;
        cin >> a;
        vector<int> v(a);
        vector<bool> exist(n + 1, false);
        for(int j = 0; j < a; j++){
            scanf("%d", &v[j]);
            exist[v[j]] = true;
        }
        for(int j = 0; j < a && flag1; j++){
            for(int l = j + 1; l < a && flag1; l++)
                if(!e[v[j]][v[l]]){
                    flag1 = false;
                    break;
                }
        }
        if(!flag1) printf("Area %d needs help.\n", i);
        else{
    bool flag3= true;
            for(int j = 1; j <= n; j++){
                if(!exist[j]){
                            int flag2 = true;
                    for(int l = 0; l <a; l++){
                            if(!e[j][v[l]]){
                                flag2= false;
                                break;
                            }
                    }
                    if(flag2) {
                        printf("Area %d may invite more people, such as %d.\n", i, j);
                        flag3 = false;
                        break;
                    }
                }
            }
            if(flag3)   printf("Area %d is OK.\n", i);
        }
    }
    return 0;
}
发布了8 篇原创文章 · 获赞 0 · 访问量 599

猜你喜欢

转载自blog.csdn.net/qq_36993032/article/details/103823877