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.
题目大意:为峰会安排休息区,一个理想的安排是邀请这些领导人,每个人互相之间都是直接朋友。给定一套暂定的安排,判断每个区域是否都已准备就绪。抽象一下可以看做有m条边,n个顶点的无向图,给定k次查询,每次查询给出点集中点的数量,以及顶点编号,问这些点是否两两连通,如果不联通输出needs help;如果是的话,是否存在其它顶点加入后仍然保持两两连通,有这样的点的话输出最小的点编号,否则输出OK。
分析:由于点的数量很少,可以直接用暴力的方法检查是否是连通子图,如果是的话再检查是否有其它点加入后仍然连通。
cpp
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;
int main(void)
{
#ifdef test
freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
clock_t start=clock();
#endif //test
int n,m;scanf("%d%d",&n,&m);
int num[n+5][n+5];
for(int i=0;i<=n;++i)
for(int j=0;j<=n;++j)
num[i][j]=0;
for(int i=0;i<m;++i)
{
int a,b;scanf("%d%d",&a,&b);
num[a][b]=num[b][a]=1;
}
int k;scanf("%d",&k);
for(int Case=1;Case<=k;++Case)
{
printf("Area %d ",Case);
int cnt,f=1;scanf("%d",&cnt);
int ques[cnt+5]={0};
for(int i=0;i<cnt;++i)
{
scanf("%d",&ques[i]);
if(i&&f)
{
for(int j=0;j<i;++j)
{
if(!num[ques[i]][ques[j]])
{
f=0;break;
}
}
}
}
if(!f)printf("needs help.\n");
else
{
int index=1;
for(int i=1;i<=n;++i)
{
f=1;index=i;
for(int j=0;j<cnt;++j)
{
if(num[i][ques[j]]==0)
{
f=0;break;
}
}
if(f)break;
}
if(!f)printf("is OK.\n");
else printf("may invite more people, such as %d.\n",index);
}
}
#ifdef test
clockid_t end=clock();
double endtime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\n\n\n\n\n");
cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位
cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位
#endif //test
return 0;
}