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
/**
本题题意
求单身狗,..给出n对夫妇, 以及他们的id
现在再给出k个数字, 需要求出这k个数字有多少个不是成对的数字(如果有配对的数字, 但是另一个数字不存在
也属于单身狗).
本题思路:
要找到单身狗 首先需要知道 数字是否成对的数字, 然后要找到成对的数字, 是否都出现在k个数中
用map<int, int> m 存储成对的数字, 双向存储, eg: m[1] = 2; m[2] = 1;
用a[i] 存储出现过的数字 a[m[i]] == 1就表示数字存在
设定一个vector<int> v存储 k个数,
最后将满足条件的数字 放入res 中 并排序
遇到的问题:
1.map<int, int> m 不能初始为0, 处理时 将 0 设为 题中 的 > 5位数的一位数
(因此本题最好不用map, 单独设定一个数组 存储) 然后初始化数组为 -1, 因为本题可以取到1)
2.本题需要以5位格式输出 id, 否则有一个测试点出错
3.vector v(size) 提前赋值后, 不能push_back, 使用 v[i] = 值
**/
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
const int maxsize = 1e6 + 5000;
int n, a[maxsize];
map<int, int> m;
int main(){
int p1, p2;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d%d", &p1, &p2);
if(p1 == 0)
p1 = (int) 1e6 + 2;
if(p2 == 0)
p2 = (int) 1e6 + 2;
m[p1] = p2;
m[p2] = p1;
}
int k, id;
scanf("%d", &k);
vector<int> v(k), res;
for(int i = 0; i < k; i++){
scanf("%d", &id);
if(id == 0)
id = (int) 1e6 + 2;
a[id] = 1;
v[i] = id;
}
for(int i = 0; i < k; i++){
if(m[v[i]] == 0 || a[m[v[i]]] != 1){
res.push_back(v[i]);
}
}
sort(res.begin(), res.end());
printf("%d\n", res.size());
for(int i = 0; i < res.size(); i++){
printf("%s%05d", i == 0 ? "" : " ", res[i]);
}
return 0;
}
set实现:
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
const int maxsize = 1e6 + 5000;
int n, a[maxsize], couple[maxsize];
int main(){
int p1, p2;
scanf("%d", &n);
fill(couple, couple + maxsize, -1);
for(int i = 0; i < n; i++){
scanf("%d%d", &p1, &p2);
couple[p1] = p2;
couple[p2] = p1;
}
int k, id;
scanf("%d", &k);
vector<int> v(k);
set<int> res;
for(int i = 0; i < k; i++){
scanf("%d", &id);
a[id] = 1;
v[i] = id;
}
for(int i = 0; i < k; i++){
if(couple[v[i]] == -1 || a[couple[v[i]]] != 1){
res.insert(v[i]);
}
}
printf("%d\n", res.size());
for(auto i = res.begin(); i != res.end(); i++){
printf("%s%05d", i == res.begin() ? "" : " ", *it);
}
return 0;
}