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.
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.
For each of the K areas, print in a line your advice in the following format:
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
.
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,表示友谊关系的数量。接下来是M行,每行给出一对互为朋友的领导人的编号。领导人的编号从1到N。然后给出另一个正整数K,接下来是K行暂定的休息区,每行给出一个正整数L,然后是一系列L个不同的领导人编号。一行中所有数字都用空格分隔。输出格式:对于K个休息区的每一个,请按以下格式将您的建议输出在一行中:如果在这个休息区每个人都互相是直接朋友,并且没有朋友漏掉(即没有其他人是这个休息区每个人的直接朋友),就输出Area X is OK.如果在这个休息区每个人都是每个人的直接朋友,但在不破坏理想安排的情况下,可能还会邀请一些其他领导人,就输出Area X may invite more people, such as H. H是可以被邀请的领导人的最小编号。如果该休息区的安排不理想,则输出Area X needs help. 这样主持人可以提供一些特别的服务帮助领导人们互相了解。
分析:二维数组A作为邻接矩阵存储两个人是否是好朋友,如果是u和v好朋友就将数组A[u][v] = A[v][u] = 1;集合temp存储待检查的序列号。先检查所有的人是不是互相都为好朋友,如果不是的话,直接输出needs help。然后,检查剩下的所有人中,是否有人是他们所有人的好朋友、但是没有被邀请的,如果没有,就输出is OK. 否则输出may invite more people, such as f. 其中f为可以被邀请的领导人的最小编号~
- #include
- #include
- using namespace std;
- int n, m, k, l, u, v, s, e, f, f2, A[201][201];
- int main() {
- cin >> n >> m;
- for (int i = 0; i < m; i++) {
- cin >> u >> v;
- A[u][v] = A[v][u] = 1;
- }
- cin >> k;
- for (int i = 1; i <= k; i++) {
- f = 0;
- set<int> temp;
- cin >> l;
- for (int j = 0; j < l; j++) {
- cin >> e;
- for (auto it : temp)
- if (!A[e][it]) f = 1;
- temp.insert(e);
- }
- cout << "Area " << i;
- if (f) {
- cout << " needs help.\n";
- } else {
- for (int i = 1; i <= n; i++) {
- f2 = 1;
- if (temp.count(i)) continue;
- for (auto it : temp) {
- if (!A[i][it]) {
- f2 = 0;
- break;
- }
- }
- if (f2) {
- f = i;
- break;
- }
- }
- if (!f)
- cout << " is OK.\n";
- else
- cout << " may invite more people, such as " << f << ".\n";
- }
- }
- return 0;
- }