PAT(甲级)1121.Damn Single(25)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ASJBFJSB/article/details/89055217

PAT 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.

输入格式:
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.

输出格式:
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.

输入样例:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

输出样例:

5
10000 23333 44444 55555 88888

题目分析:乙级1065的一个题目,具体分析请看乙级1065

注意:输出格式为%05d,否则测试点3会出错。

AC代码:

#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 100000;

bool is_appear[maxn] = {false};

vector<int>v;
unordered_map<int, int> mp;
int main(){
    //freopen("in.txt", "r", stdin);
    int n, a, b;
    scanf("%d", &n);
    for(int i=0; i<n; ++i){
        scanf("%d%d", &a, &b);
        mp.insert(make_pair(a, b));
    }
    int m, tmp;
    scanf("%d", &m);
    for(int i=0; i<m; ++i){
        scanf("%d", &tmp);
        is_appear[tmp] = true;
    }
    for(auto it=mp.begin(); it!=mp.end(); ++it){
        if(is_appear[it->first] && is_appear[it->second]){
            is_appear[it->first] = is_appear[it->second] = false;
        }
    }
    for(int i=0; i<maxn; ++i){
        if(is_appear[i])
            v.push_back(i);
    }
    sort(v.begin(), v.end());
    for(int i=0; i<v.size(); ++i){
        printf("%05d", v[i]);
        if(i!=v.size()-1)printf(" ");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ASJBFJSB/article/details/89055217