加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

1121 Damn Single (25)

发布时间:2020-12-14 04:48:35 所属栏目:大数据 来源:网络整理
导读:"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

"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
22222 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 22222 23333

Sample Output:



题目大意:给出n对数,每一对数表示一对情侣的id;再给出m个id进行查询,找出没有情侣,或者情侣没有出现的人,按顺序输出这些人的id。
思路:1.用数组来记录情侣关系cp[i]=j,cp[j]=i 表示i,j是情侣关系,不存在情侣的记为cp[i]=-1。
2.记录每一个人的出现与否,exist[i]=1,表示i出现,exis[i]=0 表示i没有出现,将出现的人依次记录在present[i]中
   3.满足条件的为没有情侣的--cp[present[i]]=-1,以及情侣没有出现的--exist[cp[present[i]]]==0;
要求按id顺序输出,用set来保存满足条件的id,插入过程中,自动排序,也可以保存在数组中,最后再排序
注意点:当 输入项比较多的时候,应该避免使用cin,cout这两个的效率较低5 10000 23333 44444 55555 88888
 1 #include<iostream>
 2 #include<vector>
 3 #include<set>
 4 using namespace std;
 5 int main(){
 6   vector<int> cp(100001,-1),present(100001),exist(100001,0);
 7   set<int> ans;
 8   int n,m,i,a,b,cnt=0;
 9   cin>>n;
10   for(i=0; i<n; i++){
11     scanf("%d%d",&a,&b);
12     cp[a] = b;
13     cp[b] = a;
14   }
15   cin>>m;
16   for(i=0; i<m; i++){
17     scanf("%d",&present[i]);
18     exist[present[i]] = 1;
19   }
20   for(i=0; i<m; i++){
21     if(!(cp[present[i]]!=-1 && exist[cp[present[i]]])){
22       cnt++;
23       ans.insert(present[i]);
24     }
25   }
26   cout<<cnt<<endl;
27   for(auto it=ans.begin(); it!=ans.end(); it++){
28     if(it==ans.begin())  printf("%05d",*it);
29     else  printf(" %05d",*it);
30   }
31 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读