算法笔记总目录
关键英语单词解释
本题代码与PAT甲级2017冬7-3 1142 Maximal Clique (25分)除输出不一样外,其他不变。
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.
题意:
7-3首脑会议(25分)
首脑会议是国家元首或政府首脑的会议。安排峰会的休息区并不是一件简单的工作。一个地区的理想安排是邀请那些头儿,这样每个人都是每个人的直接朋友。
现在给出一组暂定的安排,你的工作是告诉组织者每个区域是否都已设置好。
输入规格:
每个输入文件包含一个测试用例。对于每一种情况,第一行给出两个正整数N(≤200),表示首脑人数,M表示友好关系人数。接着M行,每行给出一对彼此是朋友的头部的索引。头部的索引范围是从1到N。
然后是另一个正整数K(≤100),接下来是K行暂定的休息区排列,每行先给出一个正数L(≤N),然后是一系列L个不同的头部指数。一行中的所有数字都用空格隔开。
输出规格:
对于每个K区域,请按以下格式在一行中打印您的建议:
如果在这个区域中每个人都是每个人的直接朋友,并且没有朋友丢失(也就是说,没有其他人是这个区域中每个人的直接朋友),那么打印区域X就可以了。。
如果在这方面,每个人都是每个人的直接朋友,但也有一些其他的头像可能会被邀请,而不会破坏理想的安排,那么打印区域X可能会邀请更多的人,例如H,其中H是可能被邀请的头像的最小索引。
如果在这个区域的排列不理想,则打印区域X需要帮助。因此,主人可以提供一些特殊的服务,以帮助头部相互了解。
这里X是一个区域的索引,从1到K。
思路:
给N个点,M条边。K个询问。每个询问给出L个点,问这L个点是不是两两相连的。
如果两两相连:
存不存在一个其它的点,与这L个点都有连接:
有:Area i may invite more people, such as 这个点.
没有:Area i is OK.
不是两两相连:Area i needs help
#include<bits/stdc++.h>
using namespace std;
int e[205][205];
int a[205];
int v[205];
int n,m;
int main(){
cin>>n>>m;
memset(e,0,sizeof(e));
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
e[u][v]=e[v][u]=1;//邻接矩阵存储
}
int k;
cin>>k;
int num;
for(int i=1;i<=k;i++){//k个询问
cin>>num;
memset(v,0,sizeof(v));//v来标记所询问的num个点
for(int j=1;j<=num;j++) {
cin>>a[j];
v[a[j]]=1;//做上标记
}
int f=1;//是不是两两相连
for(int j=1;j<=num;j++){
for(int p=j+1;p<=num;p++){
if(e[a[j]][a[p]]!=1) f=0;
break;
}
}
if(!f) cout<<"Area "<<i<<" needs help.";
else{//如果是两两相连
int ans=-1;//是否存在
for(int j=1;j<=n;j++){//查询存不存在一个点与这num个点都相连
if(v[j]) continue;//本身是num个点里的不算
int ff=1;
for(int p=1;p<=num;p++){
if(e[a[p]][j]!=1){
ff=0;
break;
}
}
if(ff) {//满足与num中的每个点都相连
ans=j;//存在
break;
}
}
if(ans!=-1){//存在
cout<<"Area "<<i<<" may invite more people, such as "<<ans<<".";
}else{//不存在
cout<<"Area "<<i<<" is OK.";
}
}
if(i!=k) cout<<endl;//行末无空行
}
return 0;
}
#include<iostream>
#include<climits>
#include<cctype>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
bool e[205][205];
int main(){
int n, m , k, a ,b;
cin >> n >> m;
fill(e[0], e[0] + 205*205, false);
for(int i = 0; i < m; i++){
cin >> a >> b;
e[a][b] = true;
e[b][a] = true;
}
cin >> k;
for(int i = 1; i <= k; i++){
bool flag1 = true;
cin >> a;
vector<int> v(a);
vector<bool> exist(n + 1, false);
for(int j = 0; j < a; j++){
scanf("%d", &v[j]);
exist[v[j]] = true;
}
for(int j = 0; j < a && flag1; j++){
for(int l = j + 1; l < a && flag1; l++)
if(!e[v[j]][v[l]]){
flag1 = false;
break;
}
}
if(!flag1) printf("Area %d needs help.\n", i);
else{
bool flag3= true;
for(int j = 1; j <= n; j++){
if(!exist[j]){
int flag2 = true;
for(int l = 0; l <a; l++){
if(!e[j][v[l]]){
flag2= false;
break;
}
}
if(flag2) {
printf("Area %d may invite more people, such as %d.\n", i, j);
flag3 = false;
break;
}
}
}
if(flag3) printf("Area %d is OK.\n", i);
}
}
return 0;
}