1121. Damn Single (25) map,set

xiaoxiao2021-02-27  198

"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 (<=50000), 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 (<=10000) 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

这题我记得当时考PAT乙的最后一题。。当时还不知道set,map。写的特别复杂。很多嵌套的循环(代码没保存,当时AC了,后面就没做)

用map建立关系,如果map【a】!=0,而且map[a]不存在set中,就是单身狗

或者说map【a】==0,肯定是单身狗

最后保存输出

#include<cstdio> #include<cmath> #include<algorithm> #include<iostream> #include<cstring> #include<queue> #include<vector> #include<set> #include<map> #include<stack> using namespace std; int main(){ int n; cin>>n; map<int,int> m; for(int i=0;i<n;i++){ int a,b; cin>>a>>b; m[a+1]=b+1; m[b+1]=a+1; } int k; cin>>k; set<int> s; for(int i=0;i<k;i++){ int num; cin>>num; num++; s.insert(num); } int flag=1; int out[100001],cnt=0; for(set<int>::iterator it=s.begin();it!=s.end();it++){ int num=*it; if(m[num]==0){ out[cnt++]=num-1; } else{ if(s.find(m[num])!=s.end()){ continue; } else{ out[cnt++]=num-1; } } } cout<<cnt<<endl; for(int i=0;i<cnt;i++){ if(i==0) printf("d",out[i]); else printf(" d",out[i]); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-9687.html

最新回复(0)