PAT-2019年冬季考试-甲级-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.

思路&分析

判断强连通图,如果是,再判断不在给定节点里的节点,有没有加进来依然属于强连通图的。题目规模很小,用邻接矩阵比较方便。每一组方案用bool变量来保存确定的判断结果以及及时break

注意点:

区分节点序号1-K与数组标号。

提交代码(AC)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3fffffff

using namespace std;


int main()
{
//    freopen("1.txt","r",stdin);
    int n,m,a,b;
    cin>>n>>m;
    int gmap[210][210];
    fill(gmap[0],gmap[0]+210*210,0);
    for(int i=0;i<m;i++){
        cin>>a>>b;
        gmap[a][b]=gmap[b][a]=1;
    }

    int k,num;
    cin>>k;
    for(int cnt=1;cnt<=k;cnt++){
        scanf("%d",&num);
        vector<int> arr(num+1);
        for(int i=0;i<num;i++) scanf("%d",&arr[i]);
        arr[num]=0;
        bool flag=true;
        for(int i=0;i<num;i++){
            for(int j=i+1;j<num;j++){
                if(gmap[arr[i]][arr[j]]==0){
                    flag=false;
                    break;
                }
            }
            if(!flag) break;
        }
        if(flag){
            bool missflag=false;
            int res=-1;
            map<int,int> mp;
            for(int i=0;i<num;i++) mp[arr[i]]=1;
            for(int i=1;i<=n;i++){
                if(mp[i]==0){
                    bool thisflag=true;
                    for(int j=0;j<num;j++){
//                        cout<
                        if(gmap[arr[j]][i]==0){
                            thisflag=false;
                            break;
                        }
                    }
                    if(thisflag){
                        missflag=true;
                        res=i;
                        break;
                    }
                }
            }
            if(missflag) printf("Area %d may invite more people, such as %d.\n",cnt,res);
            else printf("Area %d is OK.\n",cnt);
        }else printf("Area %d needs help.\n",cnt);
    }

    return 0;
}

你可能感兴趣的:(pat)