PAT(甲級)2019年冬季考試 7-3 Summit

7-3 Summit (25分)

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.ios

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.ide

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.this

Output Specification:

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

  • 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.spa

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.

題目限制:

image.png

題目大意:

現給定一個N個頂點,M條邊的無向圖,給出K個查詢,每個查詢是一個頂點集合,須要判斷當前頂點結合是不是一個徹底子圖,若是不是,輸出Area X needs help. 其中X爲查詢的編號,從1開始,不然判斷是否存在一個頂點與該集合中的全部頂點都邊相連,若是有,輸出Area X may invite more people, such as H. 其中H爲那個頂點。若是沒有,輸出Area X is OKrest

算法思路:

使用鄰接矩陣G判斷任意兩點是否連通,arrange存儲每次查詢的頂點集合,isExist標記查詢的頂點集合,每一次查詢的時候,首先使用isAllConnected判斷arrange中的每個點是否都有邊相連,若是是返回true,不然返回false,printf("Area %d needs help.\n",i);,代碼以下:code

bool isAllConnected(){
    for(int i:arrange){
        for(int j:arrange){
            if(i!=j&&G[i][j]==0){
                return false;
            }
        }
    }
    return true;
}

而後再使用getMorePeople判斷當前的頂點集合是否能夠再添加其餘人加入,若是能夠返回頂點編號,不然返回-1,若是返回-1,printf("Area %d is OK.\n",i);不然printf("Area %d may invite more people, such as %d.\n",i,a); (a爲返回值),代碼以下:orm

int getMorePeople(){
    for (int i = 1; i <= N; ++i) {
        // 判斷當前人i是否能夠添加到集合arrange中
        bool possible = true;
        // i在集合arrange中了
        if(isExist[i]) continue;
        for(int j:arrange){
            if(G[i][j]==0){
                possible = false;
            }
        }
        if(possible){
            // i加入集合arrange中後與全部人都連通
            return i;
        }
    }
    return -1;
}

提交結果:

image.png

AC代碼:

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<vector>

using namespace std;

int G[205][205];
vector<int> arrange;
int N,M;
bool isExist[205];

bool isAllConnected(){
    for(int i:arrange){
        for(int j:arrange){
            if(i!=j&&G[i][j]==0){
                return false;
            }
        }
    }
    return true;
}

int getMorePeople(){
    for (int i = 1; i <= N; ++i) {
        // 判斷當前人i是否能夠添加到集合arrange中
        bool possible = true;
        // i在集合arrange中了
        if(isExist[i]) continue;
        for(int j:arrange){
            if(G[i][j]==0){
                possible = false;
            }
        }
        if(possible){
            // i加入集合arrange中後與全部人都連通
            return i;
        }
    }
    return -1;
}

int main(){

    scanf("%d %d",&N,&M);
    for (int i = 0; i < M; ++i) {
        int a,b;
        scanf("%d %d",&a,&b);
        G[a][b] = G[b][a] = 1;
    }
    int K;
    scanf("%d",&K);
    for(int i=1;i<=K;++i){
        int num;
        scanf("%d",&num);
        arrange.clear();
        memset(isExist,0, sizeof(isExist));
        for (int j = 0; j < num; ++j) {
            int a;
            scanf("%d",&a);
            arrange.push_back(a);
            isExist[a] = true;
        }
        // 判斷是否徹底連通
        if(!isAllConnected()){
            // 不是徹底連通
            printf("Area %d needs help.\n",i);
        } else {
            int a = getMorePeople();
            if(a==-1){
                printf("Area %d is OK.\n",i);
            } else {
                printf("Area %d may invite more people, such as %d.\n",i,a);
            }
        }
    }
    return 0;
}
相關文章
相關標籤/搜索