• 1166 Summit – PAT甲级真题


    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.

    题目大意:为峰会安排休息区,一个理想的安排是邀请这些领导人,每个人互相之间都是直接朋友。给定一套暂定的安排,判断每个区域是否都已准备就绪。输入格式:第一行给一个正整数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为可以被邀请的领导人的最小编号~

    1. #include
    2. #include
    3. using namespace std;
    4. int n, m, k, l, u, v, s, e, f, f2, A[201][201];
    5. int main() {
    6. cin >> n >> m;
    7. for (int i = 0; i < m; i++) {
    8. cin >> u >> v;
    9. A[u][v] = A[v][u] = 1;
    10. }
    11. cin >> k;
    12. for (int i = 1; i <= k; i++) {
    13. f = 0;
    14. set<int> temp;
    15. cin >> l;
    16. for (int j = 0; j < l; j++) {
    17. cin >> e;
    18. for (auto it : temp)
    19. if (!A[e][it]) f = 1;
    20. temp.insert(e);
    21. }
    22. cout << "Area " << i;
    23. if (f) {
    24. cout << " needs help.\n";
    25. } else {
    26. for (int i = 1; i <= n; i++) {
    27. f2 = 1;
    28. if (temp.count(i)) continue;
    29. for (auto it : temp) {
    30. if (!A[i][it]) {
    31. f2 = 0;
    32. break;
    33. }
    34. }
    35. if (f2) {
    36. f = i;
    37. break;
    38. }
    39. }
    40. if (!f)
    41. cout << " is OK.\n";
    42. else
    43. cout << " may invite more people, such as " << f << ".\n";
    44. }
    45. }
    46. return 0;
    47. }

  • 相关阅读:
    python random模块随机抽样专题
    五、同步计数器及源码
    泛型进阶:通配符
    订单正逆向流程
    [docker]笔记-存储管理
    ROS三种通信方式之服务通信
    Kubernetes 介绍
    股指期货开户的条件和流程
    JPA - Hibernate
    JavaWeb__XML、http
  • 原文地址:https://blog.csdn.net/liuchuo/article/details/126225260