1065 单身狗 (25 分)

单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。

输入格式:

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

输出格式:

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

输入样例:

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

输出样例:

5
10000 23333 44444 55555 88888
//思路是一个数组互相存储代表夫妻的id
//一个bool数组来表示是否是单身狗
//如果spouse[id] == 0 代表一定是单身狗
//否则,bool[id] 先改成true
//如果bool[spouse[id]]为true,说明对象已经有了,就把cnt--,并且把它改为false
//第三个测试点过不去,可能会某个值出现了两次,问题真是存疑
#include<cstdio>

const int maxn = 100000;
int spouse[maxn] = {0};
bool isDog[maxn] = {false};
int arr[maxn];

int main(){
    int n,m;
    scanf("%d",&n);
    
    int g,b;
    for(int i = 0; i <  n; i++){
        scanf("%d %d",&g,&b);
        spouse[g] = b;
        spouse[b] = g;
    }
    scanf("%d",&m);
    int cnt = 0;
    for(int i = 0; i < m; i++){
        scanf("%d",&b);
        if(!spouse[b]){ // 没有对象
            isDog[b] = true;
            cnt++;
        }else{ // 有对象 
            if(isDog[spouse[b]]){ //对象在 
               isDog[spouse[b]] = false;
               //isDog[b] = false;
               cnt--;
            }else{ //对象不在 
                isDog[b] = true;
                cnt++;
            }
        }
    } //printf("%d",isDog[22156]);
    printf("%d
",cnt);
    for(int i = 0; i < maxn; i++){
        
        if(isDog[i]) {
            printf("%05d%c",i,--cnt ? ' ' : '');
            //if(--cnt) printf(" ");    
        } 
    }
//    printf("%d",cnt);

    return 0;
} 
//第二个测试点过不去 
#include<cstdio>
const int maxn = 100000;
int spouse[maxn] = {0};
int main(){
    int n;
    int u,v;
    scanf("%d",&n);
    for(int i = 0; i < n; i++){
        scanf("%d%d",&u,&v);
        spouse[u] = v;
        spouse[v] = u;
    }
    scanf("%d",&n);
    int cnt = 0;
    for(int i = 0; i < n; i++){
        scanf("%d",&u);
        if(!spouse[u]) spouse[u] = -1,cnt++;
        else spouse[u] = -2;
    }
    for(int i = 0; i < maxn; i++){
        if(spouse[i] > 0 && spouse[spouse[i]] == -2){
            spouse[spouse[i]] = -1;
            cnt++;
        }
    }
    printf("%d
",cnt);
    for(int i = 0; i < maxn; i++){
        if(spouse[i] == -1) printf("%05d%c", i, --cnt ? ' ' : '');
    }
    return 0;
}
//网友的答案
#include<cstdio>
const int maxn = 100000;
int spouse[maxn] = {0};
#define BLANK  -1
#define SINGED  -2
#define SINGLE -3
int main(){
    int n;
    int u,v;
    for(int i = 0 ; i < maxn; i++) spouse[i] = BLANK;
    scanf("%d",&n);
    for(int i = 0; i < n; i++){
        scanf("%d%d",&u,&v);
        spouse[u] = v;
        spouse[v] = u;
    }
    scanf("%d",&n);
    int cnt = 0;
    for(int i = 0; i < n; i++){
        scanf("%d",&u);
        if(spouse[u] >= 0) spouse[u] = SINGED;
        else spouse[u] = SINGLE,cnt++;
    }
    for(int i = 0; i < maxn; i++){
        if(spouse[i] >= 0 && spouse[spouse[i]] == SINGED){
            spouse[spouse[i]] = SINGLE;
            cnt++;
        }
    }
    printf("%d
",cnt);
    for(int i = 0; i < maxn; i++){
        if(spouse[i] == SINGLE) printf("%05d%c", i, --cnt ? ' ' : '');
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wanghao-boke/p/10407714.html