1065 单身狗(25 分)
“单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。
输入格式:
输入第一行给出一个正整数 N(
),是已知夫妻/伴侣的对数;随后 N 行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个 ID 号,为 5 位数字(从 00000 到 99999),ID 间以空格分隔;之后给出一个正整数 M(
),为参加派对的总人数;随后一行给出这 M 位客人的 ID,以空格分隔。题目保证无人重婚或脚踩两条船。
输出格式:
首先第一行输出落单客人的总人数;随后第二行按 ID 递增顺序列出落单的客人。ID 间用 1 个空格分隔,行的首尾不得有多余空格。
输入样例:
3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333
输出样例:
5
10000 23333 44444 55555 88888
解析
用STL爽歪歪啊
是单身狗的条件:
①:没有伴侣,真正的单身。
②:有伴侣,但是伴侣没有来。
所以用map来映射伴侣关系。我这里是这样用的:
// input is :11111 22222
map< int,int> p;
p[11111]=22222;
p[22222]=11111;
然后把每一个到场的客人放在vector。把落单的客人放在set里。
怎么判断落单呢?
要么客人ID不在上面的map里。
要么客人ID在上面的map里,通过map能知道这个客人的伴侣。再在vector里查找客人的伴侣来了没?
code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N,temp1,temp2,i=1;
cin>>N;
map<int,int> p1;
while(N--){
cin>>temp1>>temp2;
p1[temp1]=temp2;
p1[temp2]=temp1;
}
set<int> lost;
cin>>N;
vector<int> data;
while(N--){
cin>>temp1;
data.push_back(temp1);
}
for(auto x:data){
auto iter = p1.find(x);
if(iter ==p1.end())
lost.insert(x);
else{
if(find(data.begin(),data.end(),iter->second) == data.end())
lost.insert(x);
}
}
printf("%d\n",lost.size());
for(auto begin=lost.cbegin();begin!=lost.cend();begin++)
printf("%05d%c",*begin,i++==lost.size()?'\n':' ');
}