话说,这题能用sg解不?我用sg解wa了两个点
查看原帖
话说,这题能用sg解不?我用sg解wa了两个点
1183187
sxhy楼主2024/5/16 18:41
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 1000005;
vector<int> tree[MAXN];
int grundy[MAXN];
int n, T;

void dfs(int node) {
    if (tree[node].empty()) {
        grundy[node] = 0;
        return;
    }
    vector<int> childGrundy;
    for (int child : tree[node]) {
        dfs(child);
        childGrundy.push_back(grundy[child]);
    }
   
    sort(childGrundy.begin(), childGrundy.end());
    childGrundy.erase(unique(childGrundy.begin(), childGrundy.end()), childGrundy.end());
    int mex = 0;
    for (int g : childGrundy) {
        if (g == mex) {
            mex++;
        } else {
            break;
        }
    }
    grundy[node] = mex;
}

int main() {
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i = 1; i <= n; ++i) {
            tree[i].clear();
        }
        for (int i = 2; i <= n; ++i) {
            int parent;
            cin >> parent;
            tree[parent].push_back(i);
        }
        dfs(1);
        if (grundy[1] != 0) {
            cout << 1 << endl; 
        } else {
            cout << 0 << endl; 
        }
    }
    return 0;
}

2024/5/16 18:41
加载中...