1065. 单身狗(25)

题目描述

“单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。

输入格式:

输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数;随后N行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M(<=10000),为参加派对的总人数;随后一行给出这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

提交代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n, m;
bool flag = false;
string tmp1, tmp2;
map<string, string> couple;
map<string, bool> participant;
vector<string> single_dog;

cin >> n;
while (n--) {
cin >> tmp1 >> tmp2;
couple[tmp1] = tmp2;
couple[tmp2] = tmp1;
}
cin >> m;
while (m--) {
cin >> tmp1;
participant[tmp1] = true;
}
for (auto it = participant.begin(); it != participant.end(); it++) {
if (!participant[couple[it->first]]) {
single_dog.push_back(it->first);
}
}
sort(single_dog.begin(), single_dog.end());
cout << single_dog.size() << endl;
for (int i = 0; i < single_dog.size(); i++) {
if (flag) {
cout << " ";
}
cout << single_dog[i];
flag = true;
}
if (flag) {
cout << endl;
}

return 0;
}

个人思考

  1. 直接输出 single_dog[0] 会有一个测试点报段错误,可能没有落单的客人。而且不用多输出一个换行符,之前有一道题是如果统计量为零的话还是要多输出一个换行符的。
  2. 终于发现之前记录的 map 的遍历写错了,不是 it->left,而是 it->first