"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 (≤ 50,000), 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 (≤ 10,000) 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.
输入第一行给出一个正整数 N(≤ 50 000),是已知夫妻/伴侣的对数;随后 N 行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个 ID 号,为 5 位数字(从 00000 到 99999),ID 间以空格分隔;之后给出一个正整数 M(≤ 10 000),为参加派对的总人数;随后一行给出这 M 位客人的 ID,以空格分隔。题目保证无人重婚或脚踩两条船。
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.
首先第一行输出落单客人的总人数;随后第二行按 ID 递增顺序列出落单的客人。ID 间用 1 个空格分隔,行的首尾不得有多余空格。
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
解题思路
- 读入夫妻状态并记录;
- 读入来宾序列,并记录来宾情况;
- 对所有来宾检查其单身状况(单身或非单身但独身前来);
- 排序并按照题目要求的顺序输出;
- 返回零值。
代码
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 100010
int N,M,vis[maxn],couple[maxn],vivid[maxn],seq[maxn],num;
vector<int> Single;
void Init(){
int a,b;
scanf("%d",&N);
while(N--){
scanf("%d%d",&a,&b);
couple[a]=b;
couple[b]=a;
vivid[a]=1;
vivid[b]=1;
}
scanf("%d",&M);
while(M--){
scanf("%d",&a);
vis[a]=1;
seq[num++]=a;
}
}
void Check(int v){
if(vivid[v]==0){
Single.push_back(v);
return;
}//单身
if(vis[couple[v]]==0){
Single.push_back(v);
return;
}//独身前来
}
int main(){
int i;
Init();
for(i=0;i<num;i++){
Check(seq[i]);
}
sort(Single.begin(),Single.end());
num=Single.size();
printf("%d\n",num);
for(i=0;i<num;i++){
printf("%05d",Single[i]);
if(i<num-1){
printf(" ");
}else{
printf("\n");
}
}
return 0;
}
运行结果