Pat 甲级 训练真题集 1121!

1121. Damn Single (25)

​ 时间限制

​ 300 ms

​ 内存限制

​ 65536 kB

​ 代码长度限制

​ 16000 B

​ 判题程序

​ Standard

​ 作者

​ CHEN, Yue

“Damn Single (单身狗)” is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=50000), the total number of couples. Then N lines of the couples follow, each gives a couple of ID’s which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer M (<=10000) followed by M ID’s of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.

Output Specification:

First print in a line the total number of lonely guests. Then in the next line, print their ID’s in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.

Sample Input:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

Sample Output:

5
10000 23333 44444 55555 88888

此题是找出参加聚会的单身人士

用一个vector数组包含已结婚的人,

在party数组中获得人员编号,遍历一遍,若是vector[人员编号].size()==0;则未结婚,若size()==1;则判断party[*vec[人员编号].begin()]是否为0若为0,则它的另一半未到场。

代码如下:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define max 100000
int married[max];
int party_man[max];
vector<int> vec_marry[max];
int main(){
	int n;
	scanf("%d",&n);
	int m1,m2;
	for(int i=0;i<n;++i){
		scanf("%d%d",&m1,&m2);
		married[m1]=1;
		married[m2]=1;
		vec_marry[m1].push_back(m2);
		vec_marry[m2].push_back(m1);
	}
	int m;
	vector<int> vec;
	vector<int> vec1;
	scanf("%d",&m);
	int men;
	for(int i=0;i<m;++i){
		scanf("%d",&men);
		vec.push_back(men);
		party_man[men]=1;
	}
	for(int i=0;i<m;++i){
		if(married[vec[i]]!=1){
			vec1.push_back(vec[i]);			
		}else{
			if(party_man[*vec_marry[vec[i]].begin()]!=1){
					vec1.push_back(vec[i]);
				}						
		}
	}
	sort(vec1.begin(),vec1.end());
	printf("%d\n",vec1.size());
	if(vec1.size()==0){
		return 0;
	}
	for(int i=0;i<vec1.size()-1;++i){
		printf("%05d ",vec1[i]);
	}	
	printf("%05d",vec1[vec1.size()-1]);
	return 0;
}

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦