PTA Advanced 1166 Summit C++

目录

题目

Input Specification:

Output Specification:

Sample Input:

Sample Output:

领悟

知识补充

代码

题目

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.

领悟

一开始想的太复杂了,看了
1166 Summit – PAT甲级真题 – 柳婼 の blog
的思路,才恍然大悟,这其实就是简单的分支判断罢了

知识补充

对于set的遍历

  • set1.begin()+1 这种类似的形式是不合法的,对于set1.end()也是如此
  • for循环的判出条件也只能是 iter!=set1.end(),写iter<set1.end()是不合法的

代码

#include <iostream>
#include <set>

using namespace std;

int relation[201][201]; 
set<int> hasHead;// 当前区域的领导人 
int heads[200];// 当前区域的领导人(便于判断领导人之间是否是朋友) 

int main(int argc, char** argv) {
	int n,m;// n-领导人数量;m-领导人朋友关系的数量 
	cin>>n>>m;
	
	for(int i=0;i<m;i++){
		int h1,h2;
		cin>>h1>>h2;
		relation[h1][h2]=1;
		relation[h2][h1]=1;
	}
	
	int k;// 区域数量 
	cin>>k;
	for(int i=1;i<=k;i++){
		int l;// 该区域的领导人数量 
		cin>>l; 
		
		// 输入该区域的领导人 
		hasHead.clear();
		for(int j=0;j<l;j++){
			int h;
			cin>>h;
			heads[j]=h; 
			hasHead.insert(h);
		} 
		
		// 检查领导人是否互相为朋友
		bool isFriend=true;
		for(int j=0;j<l-1;j++){
			for(int k=j+1;k<l;k++){
				if(relation[heads[j]][heads[k]]==0){
					isFriend=false;
					cout<<"Area "<<i<<" needs help."<<endl;
					break;
				}
			}
			if(!isFriend) break;
		}
		if(!isFriend) continue;
		
		// 是朋友,判断是否有其他的符合条件的朋友
		int j;
		for(j=1;j<=n;j++){
			if(hasHead.count(j)==1) continue;
			int k;
			for(k=0;k<l;k++){
				if(relation[j][heads[k]]==0) break;
			}
			if(k>=l){
				cout<<"Area "<<i<<" may invite more people, such as "<<j<<"."<<endl;
				break;
			}
		}
		
		if(j>n){
			cout<<"Area "<<i<<" is OK."<<endl;
		} 
	} 
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值