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.
实现思路:
就是一道完全连通图的问题,要求给一个序列,这个序列里每一个元素都与其自身以外的人有联系,若该序列以外的id也都与这个序列里每个人有联系则还需要邀请,否则就是OK,若序列里有人不满足与自身以外的其他人都有联系就是需要帮助。这题还有一些小细节就是判断这块需要仔细一点,一开始自己还卡了一下下,因为判断比较多。
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int MAXN=220;
int n,m,G[MAXN][MAXN];
int main() {
fill(G[0],G[0]+MAXN*MAXN,0);
cin>>n>>m;
int a,b;
for(int i=0; i<m; i++) {
scanf("%d%d",&a,&b);
G[a][b]=G[b][a]=1;//建立朋友联系
}
int k,num;
cin>>k;
for(int i=1; i<=k; i++) {
scanf("%d",&num);
vector<int> sq(num);
int hashT[MAXN]= {0};
for(int j=0; j<num; j++) {
scanf("%d",&sq[j]);
hashT[sq[j]]=1;//散列记录
}
bool tag=true;
for(int j=0; j<num; j++) {
int id1=sq[j];
if(!tag) break;//有人是属于同一个圈子的
for(int v=j+1; v<num; v++) {
int id2=sq[v];//查看圈子里某人是否与其他人都有联系
if(G[id1][id2]==0) {
tag=false;
break;
}
}
}
if(tag) {
bool ans=false;
int minVal;
for(int j=1; j<=n; j++) {
if(!hashT[j]) {//没有被包含在输入序列的人进行判断是否与当前圈子的人都有联系
int v=0;
for(; v<num; v++) {
if(G[sq[v]][j]==0) break;//当某人与之前圈子的一个人没有联系则退出
}
if(v==num) {//有一个人和圈子里所有人有联系但是没有被包含在内
minVal=j;
ans=true;
break;
}
}
}
if(!ans) printf("Area %d is OK.",i);
else printf("Area %d may invite more people, such as %d.",i,minVal);
} else printf("Area %d needs help.",i);
printf("\n");
}
return 0;
}

浙公网安备 33010602011771号