L2-016 愿天下有情人都是失散多年的兄妹
Statement
Metadata
- 作者: 陈越
 - 单位: 浙江大学
 - 代码长度限制: 16 KB
 - 时间限制: 200 ms
 - 内存限制: 64 MB
 
呵呵。大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人、父母、祖父母、曾祖父母、高祖父母)则不可通婚。本题就请你帮助一对有情人判断一下,他们究竟是否可以成婚?
输入格式
输入第一行给出一个正整数N(2 N N行,每行按以下格式给出一个人的信息:
其中ID是5位数字,每人不同;性别M代表男性、F代表女性。如果某人的父亲或母亲已经不可考,则相应的ID位置上标记为-1。
接下来给出一个正整数K,随后K行,每行给出一对有情人的ID,其间以空格分隔。
注意:题目保证两个人是同辈,每人只有一个性别,并且血缘关系网中没有乱伦或隔辈成婚的情况。
输出格式
对每一对有情人,判断他们的关系是否可以通婚:如果两人是同性,输出Never Mind;如果是异性并且关系出了五服,输出Yes;如果异性关系未出五服,输出No。
输入样例
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
输出样例
Solution
#include <ctype.h>
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 5;
const int MOD = 1e9 + 7;
map<int, pii> m;
map<int, int> judge[2];
queue<int> q[2];
int ans;
void bfs(int cur) {
    if (cur == 7)
        return;
    for (int i = 0; i < 2; i++) {
        judge[i].clear();
        while (!q[i].empty()) {
            int num = q[i].front();
            q[i].pop();
            if (num != -1)
                judge[i][num] = 1;
        }
    }
    map<int, int>::iterator it;
    //	for (int i = 0; i < 2; i++)
    //	{
    //		for (it = judge[i].begin(); it != judge[i].end(); it++)
    //		{
    //			if (it != judge[i].begin())
    //				printf(" ");
    //			cout << it->first;
    //		}
    //		cout << endl;
    //	}
    for (it = judge[0].begin(); it != judge[0].end(); it++) {
        if (judge[1][it->first]) {
            ans = cur;
            return;
        } else
            judge[1].erase(it->first);
    }
    for (int i = 0; i < 2; i++) {
        for (it = judge[i].begin(); it != judge[i].end(); it++) {
            if (m[it->first].first != -1)
                q[i].push(m[it->first].first);
            if (m[it->first].second != -1)
                q[i].push(m[it->first].second);
        }
    }
    bfs(cur + 1);
}
int main() {
    map<int, int> vis, opt;
    int n;
    scanf("%d", &n);
    int a, b, c, d;
    char code;
    for (int i = 0; i < n; i++) {
        scanf("%d %c %d %d", &a, &code, &b, &c);
        opt[a] = 1;
        if (code == 'M')
            vis[a] = 0;
        else
            vis[a] = 1;
        m[a].first = b;
        if (b != -1) {
            if (opt[b] == 0) {
                m[b].first = -1;
                m[b].second = -1;
            }
            vis[b] = 0;
        }
        m[a].second = c;
        if (c != -1) {
            if (opt[c] == 0) {
                m[c].first = -1;
                m[c].second = -1;
            }
            vis[c] = 1;
        }
    }
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &a, &b);
        if (vis[a] == vis[b])
            printf("Never Mind\n");
        else {
            while (!q[0].empty()) q[0].pop();
            while (!q[1].empty()) q[1].pop();
            q[0].push(a);
            q[1].push(b);
            ans = INT_MAX;
            bfs(1);
            if (ans > 5)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
}
  Last update: May 4, 2022