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 (≤ 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
int marry[100000]; 保存誰和誰是一對
int sign[100000]= {0};保存聚會的都有誰
代碼
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
int main()
{
int N;
cin>>N;
int marry[100000];
fill(marry,marry+100000,-1);
for(int i=0; i<N; i++)
{
int a,b;
cin>>a>>b;
marry[a]=b;
marry[b]=a;
}
int M;
cin>>M;
int sign[100000]= {0};
int sn[M];
for(int i=0; i<M; i++)
{
int a;
cin>>a;
sn[i]=a;
sign[a]=1;
}
vector<int> vec;
for(int i=0; i<M; i++)
{
//沒有結婚 或者結婚了,但是對象沒來的就是單身了
if(marry[sn[i]]==-1)
vec.push_back(sn[i]);
else if(sign[marry[sn[i]]]==0)
vec.push_back(sn[i]);
}
sort(vec.begin(),vec.end());
cout<<vec.size()<<endl;
for(int i=0; i<vec.size(); i++)
{
if(i==0)
printf("%05d",vec[i]);
else
printf(" %05d",vec[i]);
}
return 0;
}