“单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。
输入格式:
输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数;随后N行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M(<=10000),为参加派对的总人数;随后一行给出这M位客人的ID,以空格分隔。题目保证无人重婚或脚踩两条船。
输出格式:
首先第一行输出落单客人的总人数;随后第二行按ID递增顺序列出落单的客人。ID间用1个空格分隔,行的首尾不得有多余空格。
输入样例:3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333
输出样例:
5
10000 23333 44444 55555 88888
============================================================================
思路:
使用一个数组来记录该人的情侣【编号】,注意这是双向的。
再初始化一个数组记录来宾,下标代表来宾编号,来了对应值就减1。最后如果该人值为-1那么铁定是单身狗。
注意这里有个特殊情况:自己来了但伴侣没来的现充也算单身狗。
另外还有个坑,不要手贱最后加个换行,不然有个测试点会PE。
【另外你看我数组/变量起名是不是很灵性233】
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
int main()
{
map<int,int> couple;
int arr[100000]={0};
int single[100000]={0};
int i,a,b,n,m,temp,dog=0;
scanf("%d",&n);
while(n--)
{
scanf("%d %d",&a,&b);
couple[a] = b;
couple[b] = a;
arr[a] = 1;
arr[b] = 1;
}
scanf("%d",&m);
while(m--)
{
scanf("%d",&temp);
arr[temp]--;
}
for(i=0;i<=99999;i++)
{
if(arr[i] == -1)
single[dog++] = i;
else if(arr[i]==1 && arr[couple[i]] != 1)
single[dog++] = couple[i];
}
sort(single,single+dog);
printf("%d\n",dog);
for(i=0;i<dog;i++)
{
printf("%05d",single[i]);
if(i < dog-1)
printf(" ");
}
//printf("\n");
return 0;
}