1065. Single Dog(25)

"Single dog" is a Chinese nickname for single people. This question asks you to find out the single guests from the large party of tens of thousands of people in order to give special care.

Input format:

输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数;随后N行,每行给出一对夫妻/伴侣——为方便起见,每人对
应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M(<=10000),为参加派对的总人数;
随后一行给出这M位客人的ID,以空格分隔。题目保证无人重婚或脚踩两条船。

Output format:

首先第一行输出落单客人的总人数;随后第二行按ID递增顺序列出落单的客人。ID间用1个空格分隔,行的首尾不得有多余空格。

Input sample:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

Sample output:

5
10000 23333 44444 55555 88888

Problem- solving idea: Create two large arrays a and b, a is used to store the relationship between husband and wife, and b is used to indicate whether the person with the corresponding id is present. 0 means not present, 1 means present. For the a array, use its own id as the subscript, and the spouse id is stored as the content of the array item. Then when traversing whether it is a single dog, the spouse id obtained from the a array can be used as the subscript of the b array to judge whether it is 1 and whether to attend. If you attend you are not single.

#include <vector> 
#include <iostream>
#include <algorithm>
using namespace std;

int a[100000]={},b[100000]={};
vector<int> v;

int main() {
    int m,n,index,i=0,flag=0;
    cin>>m;
    while(m--){
        cin>>index;
        cin>>a[index];
        a[a[index]]=index;
    }
    cin>>n;
    while(n--){
        cin>>index;
        v.push_back(index);
        if(b[a[index]]==1){
            b[a[index]]=0;
            i--;}
        else {
            b[index]=1;
            i++;}
    }
    cout<<i<<endl;
    sort(v.begin(),v.end());
    for(vector<int>::iterator iter=v.begin();iter!=v.end();iter++){
        if(b[*iter]==1)
            printf("%s%05d",flag==0?"":" ",*iter);
            flag=1;}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325618058&siteId=291194637
Dog