题目
代码
参考代码(含注释)
// 不需要并查
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> edge(250, vector<int>(250, 0));
int main() {
int n, m;
cin >> n >> m;
for(int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
edge[x][y] = edge[y][x] = 1;
}
int k;
cin >> k;
for(int i = 1; i <= k; i++) {
int l;
cin >> l;
vector<int> v(l);
for(int j = 0; j < l; j++)
cin >> v[j];
bool flag1 = true;
for(int j = 0; j < l; j++) {
for(int k = j + 1; k < l; k++){//判断是否两两相连
//cout << v[j] << " " << v[k] << " " << edge[v[j]][v[k]] << endl;
if(edge[v[j]][v[k]] == 0) {
flag1 = false;
break;
}
}
if(flag1 == false) break;
}
bool flag2 = false;
int minn = -1;
for(int j = 1; j <= n; j++) {//从最小的序号开始找
int k;
for(k = 0; k < l; k++) {
if(v[k]==j) break;//结点已经在给定的集合中,退出
if(v[k]!=j && edge[j][v[k]] == 0) break;//结点不在,但和集合中的点不全相连,退出
}
if(k == l) {//如果上面那个循环顺利结束
flag2 = true;
minn = j;
break;
}
}
if(flag1 == false) printf("Area %d needs help.\n", i);
else if(flag2) printf("Area %d may invite more people, such as %d.\n", i, minn);
else printf("Area %d is OK.\n", i);
}
}
复现代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,k;
cin>>n>>m;
int vis[n+1][n+1];
memset(vis,0,sizeof(vis));
while(m--){
int x,y;
cin>>x>>y;
vis[x][y]=vis[y][x]=1;
}
cin>>k;
for(int t=1;t<=k;t++){
int l,flag1=1,flag2=1;
cin>>l;
int a[l];
for(int i=0;i<l;i++){
cin>>a[i];
}
for(int j=0;j<l;j++){//判断是否两两相连
for(int k=j+1;k<l;k++){
if(vis[a[j]][a[k]]==0){
flag1=0;
break;
}
}
if(flag1==0) break;
}
if(!flag1){
printf("Area %d needs help.\n",t);
continue;
}
int pos=-1;
for(int i=1;i<=n;i++){//从序号最小的点开始判断
int j;
for(j=0;j<l;j++){
if(a[j]==i||vis[i][a[j]]==0){//在集合中或者不相连,退出
break;
}
}
if(j==l){
pos=i;
flag2=0;
break;
}
}
if(flag2==0) printf("Area %d may invite more people, such as %d.\n",t,pos);
else printf("Area %d is OK.\n",t);
}
}