1121. Damn Single (25)
来源:互联网 发布:win8系统修复软件 编辑:程序博客网 时间:2024/05/22 18:39
1121. Damn Single (25)
"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 (<=50000), 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 (<=10000) 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:311111 2222233333 4444455555 66666755555 44444 10000 88888 22222 11111 23333Sample Output:
510000 23333 44444 55555 88888
分析:圣诞节做single dog的题真带感(雾
先记录下 夫妻的名单,输入客人时,若单身,则加入set中;若已婚并对象未出现,则加入set中;若已婚并对象已出现,则利用set的find,删除他们。
set的find还是很快的,20000个也不过14次查找。虽然暴力了一点,但实现很方便。
另外需要注意的是,因为00000也是客人的编号,所以夫妻名单要初始化成-1,用0的话会产生歧义(一夫多多多多妻)
代码:
#include<cstdio>#include<set>#include<cstring>using namespace std;int couple[100050];set<int> single;int main(){int n,m,a,b;scanf("%d",&n);memset(couple,-1,sizeof(int)*100050);for(int i=0;i<n;i++){scanf("%d %d",&a,&b);couple[a]=b;couple[b]=a;}scanf("%d",&m);for(int i=0;i<m;i++){scanf("%d",&a);if(couple[a]==-1) single.insert(a);else{if(single.find(couple[a])!=single.end())single.erase(single.find(couple[a]));else single.insert(a);}}printf("%d\n",single.size());set<int>::iterator it;for(it=single.begin();it!=single.end();it++)it==single.begin()?printf("%05d",*it):printf(" %05d",*it);}
结尾:感觉pat里对set考察挺多的