我是靠谱客的博主 飘逸太阳,最近开发中收集的这篇文章主要介绍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;
}

最后

以上就是飘逸太阳为你收集整理的PTA Advanced 1166 Summit C++的全部内容,希望文章能够帮你解决PTA Advanced 1166 Summit C++所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部