PAT_A1121#Damn Single

Source:

PAT A1121 Damn Single (25 分)

Description:

"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

Keys:

Code:

 1 /*
 2 Data: 2019-08-14 16:32:45
 3 Problem: PAT_A1121#Damn Single
 4 AC: 13:30
 5 
 6 题目大意:
 7 找出独自前往派对的人
 8 输入:
 9 第一行给出,伴侣数N<=5e4(意味着最多1e5个人)
10 接下来N行,p1,p2
11 接下来一行,出席人数M<=1e4
12 接下来一行,给出M个id(5位)
13 输出:
14 独自参加派对的人数
15 id递增
16 
17 基本思路:
18 有一点需要注意下,id值可能为0,因此哈希表的初始值置-1就可以了;
19 */
20 #include<cstdio>
21 #include<vector>
22 #include<algorithm>
23 using namespace std;
24 const int M=1e5;
25 int cp[M],mp[M]={0};
26 vector<int> pt,sg;
27 
28 int main()
29 {
30 #ifdef ONLINE_JUDGE
31 #else
32     freopen("Test.txt", "r", stdin);
33 #endif // ONLINE_JUDGE
34 
35     int n,v1,v2;
36     scanf("%d", &n);
37     fill(cp,cp+M,-1);
38     for(int i=0; i<n; i++)
39     {
40         scanf("%d%d", &v1,&v2);
41         cp[v1]=v2;
42         cp[v2]=v1;
43     }
44     scanf("%d", &n);
45     while(n--)
46     {
47         scanf("%d", &v1);
48         pt.push_back(v1);
49         mp[v1]=1;
50     }
51     for(int i=0; i<pt.size(); i++)
52         if(cp[pt[i]]==-1 || mp[cp[pt[i]]]==0)
53             sg.push_back(pt[i]);
54     sort(sg.begin(),sg.end());
55     printf("%d\n", sg.size());
56     for(int i=0; i<sg.size(); i++)
57         printf("%05d%c", sg[i],i==sg.size()-1?'\n':' ');
58 
59     return 0;
60 }

 

posted @ 2019-07-19 18:26  林東雨  阅读(161)  评论(0编辑  收藏  举报