题目
“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 | 3 |
Sample Output:
1 | 5 |
题解
思路
- 这题真实迷惑
- 题目给出互为夫妻的两个人
- 给出参与派对的人
- 问你哪些人是单身的
- 迷惑的是,当一个人是已婚的,他对象没出现在party中,也当作单身处理
数据结构
- couple为一个哈希表,键和值互为一对couple
- ans 存放答案
- guests 是一个集合,存所有的宾客。(为了方便查找设置成了哈希集)
算法
- 将成对成对的情侣添加到couple里
- 遍历所有宾客
- 如果宾客不在couple里或者他的对象不在couple里
- 把它添加到ans里
- 如果宾客不在couple里或者他的对象不在couple里
- 对ans排序并输出
代码
- 由于使用Python能够AC,因此使用Python作题解。
1 | n = int(input()) |