72532024-01-05 14:53:08anonTom és Jerry 3cpp17Accepted 50/50144ms19932 KiB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define FastIO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
using namespace std;
typedef long long ll;
typedef struct {
    ll md;
    ll depth;
    ll parent;
    vector<ll> children;
} Branch;
ll build_tree(ll vertex, ll parent, ll depth, vector<Branch> &tree, const vector<vector<ll>> &graph) {
    tree[vertex].md = depth;
    tree[vertex].depth = depth;
    tree[vertex].parent = parent;
    for(const auto &x : graph[vertex]) {
        if(x == parent)
            continue;
        tree[vertex].md = max(tree[vertex].md, build_tree(x, vertex, depth + 1, tree, graph));
        tree[vertex].children.push_back(x);
    }
    return tree[vertex].md;
}
bool does_tom_win(const ll J, const ll K, const vector<Branch> &tree) {
    ll rj;
    rj = J;
    while(rj != -1) {
        if(tree[J].depth - tree[rj].depth >= tree[rj].depth - 1)
            return true;
        if(tree[rj].depth + min(K - 1 - (tree[J].depth - tree[rj].depth), tree[rj].md - tree[rj].depth) > K)
            return false;
        rj = tree[rj].parent;
    }
    return true;
}
int main() {
    FastIO;
    bool ok;
    ll i, j, u, v, N, T, J, K, C;
    cin >> C;
    while(C--) {
        cin >> N >> T >> J >> K;
        vector<vector<ll>> graph(N + 1);
        for(i = 1; i < N; i++) {
            cin >> u >> v;
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
        vector<Branch> tree(N + 1);
        build_tree(T, -1, 0, tree, graph);
        ok = does_tom_win(J, K, tree);
        if(!ok) {
            u = 0;
            while(tree[++u].depth != tree[T].md);
            tree = vector<Branch>(N + 1);
            build_tree(u, -1, 0, tree, graph);
            v = 0;
            while(tree[++v].depth != tree[u].md);
            for(i = 0; i < tree[u].md / 2; i++)
                v = tree[v].parent;
            tree = vector<Branch>(N + 1);
            build_tree(v, -1, 0, tree, graph);
            ok = (tree[v].md <= K);
        }
        cout << (ok ? "IGEN" : "NEM") << '\n';
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1828 KiB
2Accepted0/03ms2168 KiB
3Accepted5/53ms2360 KiB
4Accepted1/14ms2728 KiB
5Accepted1/14ms2840 KiB
6Accepted1/14ms3044 KiB
7Accepted1/14ms3276 KiB
8Accepted1/13ms3100 KiB
9Accepted1/14ms3016 KiB
10Accepted1/14ms3260 KiB
11Accepted2/24ms3216 KiB
12Accepted2/24ms3464 KiB
13Accepted1/14ms3680 KiB
14Accepted2/2137ms12500 KiB
15Accepted2/2125ms8532 KiB
16Accepted2/2136ms12640 KiB
17Accepted2/2123ms17380 KiB
18Accepted2/2144ms12952 KiB
19Accepted2/2131ms12960 KiB
20Accepted2/286ms19932 KiB
21Accepted2/2119ms8924 KiB
22Accepted2/2119ms8848 KiB
23Accepted3/3114ms15204 KiB
24Accepted2/2136ms13232 KiB
25Accepted3/3126ms13216 KiB
26Accepted2/2127ms15612 KiB
27Accepted2/2136ms8956 KiB
28Accepted3/3136ms8944 KiB