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.算法
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
For each of the K areas, print in a line your advice in the following format:idea
Area X is OK.
.Area X may invite more people, such as H.
where H
is the smallest index of the head who may be invited.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
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
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.
现给定一个N个顶点,M条边的无向图,给出K个查询,每个查询是一个顶点集合,须要判断当前顶点结合是不是一个彻底子图,若是不是,输出Area X needs help.
其中X为查询的编号,从1开始,不然判断是否存在一个顶点与该集合中的全部顶点都边相连,若是有,输出Area X may invite more people, such as H.
其中H为那个顶点。若是没有,输出Area X is OK
。rest
使用邻接矩阵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; }
#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; }