1166 Summit (25 point(s)) PAT甲级

1166 Summit (25 point(s))

题目

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.

思路

题意

n个人参加会议,用m对关系表示他们两两是朋友。现在给出k个场地(测试数据),每个场地有l个人,判断场地是否OK。场地OK的标准是这l个人两两互相认识且没有另外一个人与场中所有人认识(即l个点构成最大全联通子图),如果有这样的场外朋友存在(即l个点构成全联通子图但不是最大的)则输出可以邀请的人的最小序号。否则场地需要帮助。

思路

可以化解为简单的全联通图问题。图不大,可以用二维数组直接存储。用IsOK函数判断子图的所有点是否全联通,具体只要二维数组的上三角部分均联通即可。再用Invite()函数判断子图再加入一个新点是否还是全联通子图,具体用一个set存储子图的结点,从小到大判断新的不在set中的结点是否能与子图中原来的所有结点相连。返回第一个符合条件的结点,如果遍历完图没有满足条件的结点,返回负值。

解法

#include<bits/stdc++.h>
using namespace std;

int n, m, k, l;
int edge[205][205] = { 0 };

bool IsOK(vector<int> index) {
	for (int i = 0; i < index.size(); i++) {
		for (int j = i + 1; j < index.size(); j++) {
			if (edge[index[i]][index[j]] == 0) return false;
		}
	}
	return true;
}

int Invite(set<int> s, vector<int> index) {
	for (int i = 1; i <= n; i++) {
		if (s.find(i) == s.end()) {//s中没有i
			int j = 0;
			for (j = 0; j < index.size(); j++) {
				if (edge[i][index[j]] == 0)//加入的i与原来的index[j]不相连
					break;
			}
			if(j == index.size()) return i;
		}
	}
	return -1;
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d%d", &a, &b);
		edge[a][b] = edge[b][a] = 1;
	}
	scanf("%d", &k);
	for (int i = 1; i <= k; i++) {
		scanf("%d", &l);
		vector<int> index(l);
		for (int j = 0; j < l; j++)
			scanf("%d", &index[j]);
		if (IsOK(index)) {//判断是否所有点都相连
			set<int> s;
			for (int j = 0; j < l; j++)
				s.insert(index[j]);
			int num = Invite(s, index);
			if (num > 0)//判断是否所有
				printf("Area %d may invite more people, such as %d.\n", i, num);
			else
				printf("Area %d is OK.\n", i);
		}
		else
			printf("Area %d needs help.\n", i);
	}
	return 0;
}

注意

  • index是1到N,不是从0开始
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值