A1121 Damn Single (25 point(s))

map充当散列,记录访客对应couple

1. 原文

“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.

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:

1
2
3
4
5
6
3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

Sample output:

1
2
5
10000 23333 44444 55555 88888

2. 解析

couple存入map couple[a]=b; couple[b]=a;

记录couple exist[couple[a]]++;

当 exist[*i]==0 时就说明不存在它的couple

3. AC代码

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
#include<cstdio>
#include<map>
#include<set>
using namespace std;
map<int,int> couple,match;
set<int> order,ans;
int main()
{
int n;
scanf("%d",&n);
int a,b;
for (int i = 0; i < n; ++i)
{
scanf("%d%d",&a,&b);
couple[a]=b;
couple[b]=a;
}
int m;
scanf("%d",&m);
for (int i = 0; i < m; ++i)
{
scanf("%d",&a);
order.insert(a);
match[couple[a]]++;
}
for (set<int>::iterator i = order.begin(); i != order.end(); ++i)
{
if (match[*i]==0)
{
ans.insert(*i);
}
}
printf("%lu\n",ans.size());
for (set<int>::iterator i = ans.begin(); i != ans.end(); ++i)
{
if (i!=ans.begin())
{
printf(" ");
}
printf("%05d",*i);
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1121/
  • 发布时间: 2019-09-03 08:34
  • 更新时间: 2021-10-29 14:08
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!