AcWing 1614. 单身狗---哈希表
原题链接
简单
作者:
巨鹿噜噜噜路
,
2020-06-02 15:33:25
,
所有人可见
,
阅读 830
C++ 代码
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
using namespace std;
unordered_map<int, int> mp;
unordered_set<int> party;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x1, x2;
cin >> x1 >> x2;
mp[x1] = x2;
mp[x2] = x1;
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int x;
cin >> x;
party.insert(x);
}
vector<int> ans;
for (auto& item : party) {
//有伴侣,但是没参加聚会
if (mp.count(item)) {
if (party.count(mp[item]) == 0) {
ans.push_back(item);
}
}
else {
//无伴侣
ans.push_back(item);
}
}
printf("%d\n", ans.size());
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++) {
if (i) printf(" ");
printf("%05d", ans[i]);
}
return 0;
}