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.
whereH
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.
题目描述:安排峰会的休息地是一个不简单的活,每个安排的休息地中,需要所有的人都彼此认识。
输入:第一行n,m分别表示有n个人参加峰会,m对好友关系。接下来m行,分别表示这两个人是好友关系。
接下来有一个k值,表示需要查询的休息地个数。往下的k行,第一个值表示该休息地有L个人,接着同一行有L个数。
输出:
1)如果查询的休息地中,有任意两个人不是相互认识的,则输出格式为Area X needs help.,其中X表示第几次查询的编号;
2)如果查询的休息地中,任意两个人都相互认识,并且能够再找出一个人,这人加入进来后,彼此仍旧相互认识,则输出Area X may invite more people, such as other-id.其中X表示第几次查询的编号;若这样的人有多个,输出编号最小一个即可;
3)如果查询的休息地中,任意两个人都相互认识,并且无法再加入其他人。则输出Area X is OK.,其中X表示第几次查询的编号。
解题思路:不需要存图,但需要保存好友关系,方便查询。
#include<bits/stdc++.h>
#include<map>
using namespace std;
#define ll long long
int n, m;
vector<int> v;
unordered_map<int,unordered_map<int,int> > p;
void solve(int index)
{
bool vis[400];
memset(vis,false,sizeof(vis));
int l = v.size();
bool f1 = true;
for(int i=0; i<l&&f1; i++){
vis[i] = true;
for(int j=i+1; j<l; j++){
if(p[v[i]][v[j]]==0){
f1 = false;
break;
}
}
}
if(f1==false){
printf("Area %d needs help.\n", index);
return ;
}
int ans;
for(int i=1; i<=n&&f1; i++){
if(vis[i]==false){
f1 = false;
for(int j=0; j<l; j++){
if(p[i][v[j]]==0){
f1 = true;
break;
}
}
if(f1==false){
ans = i;
}
}
}
if(f1==false){
printf("Area %d may invite more people, such as %d.\n", index, ans);
}
else{
printf("Area %d is OK.\n", index);
}
}
int main()
{
int a, b;
scanf("%d %d", &n, &m);
for(int i=0; i<m; i++){
scanf("%d %d", &a, &b);
p[a][b] = p[b][a] = 1;
}
int k;
scanf("%d", &k);
for(int i=0; i<k; i++){
scanf("%d", &m);
for(int j=0; j<m; j++){
scanf("%d", &a);
v.push_back(a);
}
solve(i+1);
v.clear();
}
return 0;
}