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.

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.

题目限制:

image.png

题目大意:

现给定一个N个顶点,M条边的无向图,给出K个查询,每一个查询是一个顶点集合,需要判断当前顶点结合是否是一个完全子图,如果不是,输出Area X needs help. 其中X为查询的编号,从1开始,否则判断是否存在一个顶点与该集合中的所有顶点都边相连,如果有,输出Area X may invite more people, such as H. 其中H为那个顶点。如果没有,输出Area X is OK

算法思路:

使用邻接矩阵G判断任意两点是否连通,arrange存储每次查询的顶点集合,isExist标记查询的顶点集合,每一次查询的时候,首先使用isAllConnected判断arrange中的每一个点是否都有边相连,如果是返回true,否则返回false,printf("Area %d needs help.\n",i);,代码如下:

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为返回值),代码如下:

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
#include
#include
#include
#include

using namespace std;

int G[205][205];
vector 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;
}

你可能感兴趣的:(算法-数据结构,c++)