A1166 Summit (25 分)
A1166 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.
题目大意:
给来参加峰会的国家或政府首脑安排位置。
input:
- 各个政府首脑的关系图,编号是从 1~N
- 每个区域安排的位置
output:
判断每个区域安排的位置:
- 如果这个区域的人互相都是朋友,并且没有遗漏共同的朋友则输出
Area X is OK.
- 如果这个区域的人互相都是朋友,并且遗漏了共同的朋友,则输出
Area X may invite more people, such as H.
,H表示遗漏的共同朋友中编号最小的那个。 - 如果这个区域的人只要出现有一对不是朋友关系,则输出
Area X needs help.
- 如果这个区域的人互相都是朋友,并且没有遗漏共同的朋友则输出
思路
- 用hash table
unordered_map<int, bool>
存储两个是否是朋友关系 - 遍历给出的位置安排,根据题目给出的条件判断
AC代码
#include <bits/stdc++.h>
using namespace std;
int n, m, k, t;
unordered_map<int, bool> heads;
int judge(vector<int> &arrange) {
// -1: need help; 0: ok; other number invite people;
int len = t;
for (int i = 0; i < len - 1; ++i) {
for (int j = i + 1; j < len; ++j) {
if (!heads[arrange[i] * 1000 + arrange[j]]) {
return -1;
}
}
}
unordered_set<int> st(arrange.begin(), arrange.end());
// 寻找是否还有额外共同的朋友
for (int i = 1; i <= n; ++i) {
if (st.find(i) != st.end()) continue;
bool b = true;
for (int &h : arrange) {
if (!heads[h * 1000 + i]) {
b = false;
break;
}
}
if (b) return i;
}
return 0;
}
int main() {
scanf("%d%d", &n, &m);
int t1, t2;
for (int i = 0; i < m; ++i) {
cin >> t1 >> t2;
heads[t1 * 1000 + t2] = true;
heads[t2 * 1000 + t1] = true;
}
scanf("%d", &k);
for (int i = 1; i <= k; ++i) {
cin >> t;
vector<int> arrange(t);
for (int j = 0; j < t; ++j) {
cin >> arrange[j];
}
int res = judge(arrange);
if (res == 0) printf("Area %d is OK.\n", i);
else if (res == -1) printf("Area %d needs help.\n", i);
else printf("Area %d may invite more people, such as %d.\n", i, res);
}
return 0;
}