L2-016 愿天下有情人都是失散多年的兄妹

要记得他父母也有性别

#include <bits/stdc++.h>

using namespace std;

constexpr int N = 1e5 + 10;

vector<int> g[N];

int main() {
    int n, m;
    cin >> n;
    map<int, char> sex;
    for (int i = 0; i < n; i++) {
        int a, b, c;
        char s;
        cin >> a >> s >> b >> c;
        sex[a] = s;
        if (b != -1) g[a].push_back(b), sex[b] = 'M';
        if (c != -1) g[a].push_back(c), sex[c] = 'F';
    }

    function<void(int, set<int>&)> bfs = [&] (int x, set<int> &st) {
        queue<pair<int, int>> q;
        st.insert(x);
        q.push({x, 1});
        
        while (q.size()) {
            auto u = q.front(); 
            q.pop();

            int var = u.first, d = u.second;
            for (auto &v : g[var]) {
                if (d + 1 >= 6 || v == -1) continue;
                st.insert(v);
                q.push({v, d + 1});
            }
        }
    };

    function<bool(int, int)> check = [&] (int x, int y) {
        set<int> xfive;
        set<int> yfive;

        bfs(x, xfive);
        bfs(y, yfive);

        for (auto &v : xfive) {
            if (yfive.count(v)) return false;
        }
        
        for (auto &v : yfive) {
            if (xfive.count(v)) return false;
        }

        return true;
    };

    cin >> m;
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        if (sex[x] == sex[y]) cout << "Never Mind" << "\n";
        else if (check(x, y)) cout << "Yes" << "\n"; //出五服饰
        else cout << "No" << "\n";
    }

    return 0;
}
posted @ 2022-03-18 19:40  Xxaj5  阅读(33)  评论(0编辑  收藏  举报