"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:
3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333
Sample Output:
5
10000 23333 44444 55555 88888
题目大意:给定配偶列表,接着给出一堆人,找到没有配偶的人。注意在表中没出现的人就算没有配偶。把找到的单身狗排序输出。
这道题可以把读入的配偶表存放在一个表中,并建立双向映射的关系,然后对于给的人,一一在表中找是否有配偶,没有配偶就放到set中,有配偶就不要放进去,并且在set中找他的配偶,如果之前放进去过,就得删掉。
set自带去重排序功能,直接输出就好。
不会set的看这里:https://www.mmuaa.com/post/b9a637bded62f2a6.html
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <cstdio>
using namespace std;
int G[100000];
set<int> S;
int main(){
fill(G, G+100000, -1);
int cnt;
cin >> cnt;
for(int i = 0; i < cnt; i++){
int l, r;
cin >> l >> r;
G[l] = r, G[r] = l;
}
cin >> cnt;
for(int i = 0; i < cnt; i++){
int n;
cin >> n;
set<int>::iterator it = S.find(G[n]);
if(G[n] != -1 && it != S.end()){
S.erase(it);
}
else S.insert(n);
}
cout << S.size() << endl;
for(set<int>::iterator it = S.begin(); it != S.end(); it++){
if(it != S.begin()) cout << ' ';
printf("%05d", *it);
}
return 0;
}