1 条题解

  • 0
    @ 2025-10-14 9:19:47
    #include <bits/stdc++.h>
    using namespace std;
    bitset<40005> a[40005];
    vector<int> v[40005];
    bitset<40005> b;
    bitset<40005> f;
    int n, m;
    void build(int idx) {
        b[idx] = 0;
        a[idx] = f;
        f[idx] = 1;
        for (int i : v[idx]) {
            if (b[i]) {
                build(i);
            }
        }
        f[idx] = 0;
        return;
    }
    int main() {
        cin >> n;
        for (int i = 0; i <= 40000; i++) {
            b[i] = 1;
        }
        int root = -1;
        for (int i = 0; i < n; i++) {
            int x, y;
            cin >> x >> y;
            if (y == -1) {
                root = x;
            } else {
                v[x].push_back(y);
                v[y].push_back(x);
            }
        }
        build(root);
        cin >> m;
        while (m--) {
            int x, y;
            cin >> x >> y;
            if (a[x][y] == 1) {
                cout << 2;
            } else if (a[y][x] == 1) {
                cout << 1;
            } else {
                cout << 0;
            }
            cout << endl;
        }
        return 0;
    }
    
    • 1

    信息

    ID
    1280
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    (无)
    递交数
    0
    已通过
    0
    上传者