19年冬季第二题 PAT甲级 1166 Summit (25分) 与题库1142类似的一般难度题

我会带着你远行 2023-07-02 07:23 105阅读 0赞

汇总贴在博客的置顶:
2020年3月PAT甲级满分必备刷题技巧

题目

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.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.

这道题没有什么坑,题意如下:

不超过200个人,编号从1到N
先给出朋友关系
再给出聚会人员编号问是不是全是朋友?
不是的话就求助;是的话,再分成两种
一种是还能再请来聚会,就请编号小的
另一种是请不到人

满分代码

  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. int n,m,k;
  5. int main(){
  6. scanf("%d %d\n",&n,&m);
  7. int G[n+5][n+5];
  8. vector<vector<int>> va(n+5);
  9. va.clear();
  10. int u,v,l;
  11. for(int i=0;i<m;i++){
  12. scanf("%d %d\n",&u,&v);
  13. G[u][v]=G[v][u]=1;
  14. va[u].push_back(v);
  15. va[v].push_back(u);
  16. }
  17. scanf("%d\n",&k);
  18. for(int i=0;i<k;i++){
  19. scanf("%d",&l);
  20. int a[l];
  21. set<int> s;
  22. for(int j=0;j<l;j++){
  23. scanf("%d",&a[j]);
  24. s.insert(a[j]);
  25. }
  26. if(l==1&&va[a[0]].size()==0){
  27. printf("Area %d is OK.\n",i+1);
  28. continue;
  29. }
  30. bool f1=true,f2=true;
  31. for(int j=0;j<l-1;j++){
  32. for(int k=j+1;k<l;k++){
  33. int x=a[j],y=a[k];
  34. if(G[x][y]!=1){
  35. f1=false;
  36. }
  37. }
  38. }
  39. if(!f1){
  40. printf("Area %d needs help.\n",i+1);
  41. }else{
  42. int minn=n+1;
  43. for(int k=1;k<=n;k++){
  44. bool f3=true;
  45. for(auto it:s){
  46. if(G[k][it]!=1){
  47. f3=false;
  48. }
  49. }
  50. if(f3 && s.find(k)==s.end() && k<minn){
  51. minn=k;
  52. f2=false;
  53. continue;
  54. }
  55. }
  56. if(f2){
  57. printf("Area %d is OK.\n",i+1);
  58. }else{
  59. printf("Area %d may invite more people, such as %d.\n",i+1,minn);
  60. }
  61. }
  62. }
  63. return 0;
  64. }

发表评论

表情:
评论列表 (有 0 条评论,105人围观)

还没有评论,来说两句吧...

相关阅读