PTA group programming TIANTI competition - exercise set L2-016 wish all lovers in the world are brothers and sisters who have been separated for many years (25 points)

Keywords: network

Ha-ha. We all know that intermarriage is not allowed within five uniforms, that is, two people cannot intermarry if their nearest common ancestor is within five generations (i.e. themselves, parents, grandparents, great grandparents, and great grandparents). Please help a couple of lovers to judge whether they can get married or not?

Input format:

Enter the first line to give a positive integer N (2 ≤ N ≤ 10 4), then N lines, each line to give a person's information in the following format:

My ID gender father ID mother ID

Among them, ID is 5 digits, each person is different; gender M represents male and F represents female. If someone's father or mother is no longer available, the corresponding ID location is marked - 1.

Next, we give a positive integer k, followed by K lines, each line giving a pair of lovers' ID S, separated by spaces.

Note: the topic ensures that two people are of the same generation, each of them has only one gender, and there is no incest or marriage between different generations in the blood relationship network.

Output format:

For each pair of lovers, judge whether their relationship can be intermarried: if they are of the same sex, output Never Mind; if they are of the opposite sex and the relationship has five services, output Yes; if the relationship has not five services, output No.

Input example:

24
00001 M 01111 -1
00002 F 02222 03333
00003 M 02222 03333
00004 F 04444 03333
00005 M 04444 05555
00006 F 04444 05555
00007 F 06666 07777
00008 M 06666 07777
00009 M 00001 00002
00010 M 00003 00006
00011 F 00005 00007
00012 F 00008 08888
00013 F 00009 00011
00014 M 00010 09999
00015 M 00010 09999
00016 M 10000 00012
00017 F -1 00012
00018 F 11000 00013
00019 F 11100 00018
00020 F 00015 11110
00021 M 11100 00020
00022 M 00016 -1
00023 M 10012 00017
00024 M 00022 10013
9
00021 00024
00019 00024
00011 00012
00022 00018
00001 00004
00013 00016
00017 00015
00019 00021
00010 00011

Output example:

Never Mind
Yes
Never Mind
No
Yes
No
Yes
No
No
1. Since every couple in the topic is of the same generation, if they can't get married, they must have the same status. Therefore, they can search with bfs
 2. Although this question says the positive integer N (2 ≤ N ≤ 10 ^ 4), but each person's parent's id only says that it is a five digit number, and its value range is between 0-99999,
The static hash is adopted, and the array should be opened to 1e5 level
 3. The most pitiful point of the test data is that the id of the query may be the id of some ancestors, that is to say, the gender of the parents should also be recorded when inputting
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct Person{
    int id;
    char sex;
    int f_id = -1;
    int m_id = -1;
}persons[100010];
queue<int> q;
int vis[100010];
int main(){
    int n,id,sex,m_id,f_id;
    cin>>n;
    for (int i = 0; i < n; ++i){
        scanf("%d %c %d %d",&id,&sex,&f_id,&m_id);
        persons[id] = {id,sex,f_id,m_id};
        persons[f_id].sex = 'M';
        persons[m_id].sex = 'F';
    }
    int m,id1,id2,depth,flag;
    cin>>m;
    while(m--){
        scanf("%d%d",&id1,&id2);
        if(persons[id1].sex == persons[id2].sex){
            puts("Never Mind");
            continue;
        }
        while(!q.empty()) q.pop();
        q.push(id1); q.push(id2);
        depth = 1; memset(vis,0,sizeof vis); flag = 0;
        vis[id1] = vis[id2] = 1;
        while(!q.empty()){
            int sz = q.size();
            depth++;
            for (int i = 0; i < sz; ++i){
                int t = q.front();
                q.pop();
                if (persons[t].f_id != -1 && !vis[persons[t].f_id]){
                    q.push(persons[t].f_id);
                    vis[persons[t].f_id] = 1;
                }
                else if(persons[t].f_id != -1  && vis[persons[t].f_id] == 1 && depth <= 5){
                    puts("No");
                    flag = 1; break;
                }
                if (persons[t].m_id != -1 && !vis[persons[t].m_id]){
                    q.push(persons[t].m_id);
                    vis[persons[t].m_id] = 1;
                }
                else if(persons[t].m_id != -1 && vis[persons[t].m_id] == 1 && depth <= 5){
                    puts("No");
                    flag = 1; break;
                }
            }
            if (flag) break;
        }
        if (flag == 0) puts("Yes");
    }
    return 0;
}

 

363 original articles published, 33 praised, 30000 visitors+
Private letter follow

Posted by fcaserio on Sat, 08 Feb 2020 22:54:50 -0800