A1167 Summit (25分)

散列

1. 原文

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:

1
2
3
4
5
6
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.

2. 解析思路

8个峰会代表人,10个好友组

安排的圆桌会议中

  • 如果所有人都是直接好友,并且加一个代表人a,a跟圆桌内的所有人并不是直接好友,则当前圆桌是完美的
  • 可以加一个a,跟圆桌内的所有人都是直接好友,该圆桌可以增加人数
  • 圆桌内并不是好友,需要调整

用 fre[a*maxn+b]=1 代表a和b是好友

3. 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 210;
map<int,int> fre;
int arr[110]={};
bool visit[110]={};
int main(){
int n,m;
scanf("%d%d",&n,&m);
for (int i = 0; i < m; ++i)
{
int a,b;
scanf("%d%d",&a,&b);
fre[a*maxn+b]=1;
fre[b*maxn+a]=1;
}
int k;
scanf("%d",&k);
for (int i = 1; i <= k; ++i)
{
int l;
scanf("%d",&l);
fill(visit,visit+maxn,false);
for(int j = 0; j < l; j++){

scanf("%d",&arr[j]);
visit[arr[j]]=true;
}
int flag = 0;
for (int j = 0; j < l; j++)
{
for (int k = j+1; k < l; ++k)
{
if (fre[arr[j]*maxn+arr[k]]==0)
{
flag = 1;
break;
}
}
}
if (flag == 1)
{
printf("Area %d needs help.\n",i);
continue;
}
int cnt = 0;
int index = 1<<30;
for (int j = 1; j <= n; ++j)
{
int morepe = 0;
if (visit[j]==false)
{
for (int k = 0; k < l; ++k)
{
if(fre[j*maxn+arr[k]]==0){
morepe = 1;
break;
}
}
if (morepe==0)
{
if (j<index)
{
index = j;
}
cnt++;
}
}
}

if (flag == 0)
{
if(cnt == 0){
printf("Area %d is OK.\n",i);
}
else{
printf("Area %d may invite more people, such as %d.\n",i,index);
}
}
}

return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2020/01/20/A1167/
  • 发布时间: 2020-01-20 18:37
  • 更新时间: 2021-10-29 14:12
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!