72532024-01-05 14:53:08anonTom és Jerry 3cpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1828 KiB
2Elfogadva0/03ms2168 KiB
3Elfogadva5/53ms2360 KiB
4Elfogadva1/14ms2728 KiB
5Elfogadva1/14ms2840 KiB
6Elfogadva1/14ms3044 KiB
7Elfogadva1/14ms3276 KiB
8Elfogadva1/13ms3100 KiB
9Elfogadva1/14ms3016 KiB
10Elfogadva1/14ms3260 KiB
11Elfogadva2/24ms3216 KiB
12Elfogadva2/24ms3464 KiB
13Elfogadva1/14ms3680 KiB
14Elfogadva2/2137ms12500 KiB
15Elfogadva2/2125ms8532 KiB
16Elfogadva2/2136ms12640 KiB
17Elfogadva2/2123ms17380 KiB
18Elfogadva2/2144ms12952 KiB
19Elfogadva2/2131ms12960 KiB
20Elfogadva2/286ms19932 KiB
21Elfogadva2/2119ms8924 KiB
22Elfogadva2/2119ms8848 KiB
23Elfogadva3/3114ms15204 KiB
24Elfogadva2/2136ms13232 KiB
25Elfogadva3/3126ms13216 KiB
26Elfogadva2/2127ms15612 KiB
27Elfogadva2/2136ms8956 KiB
28Elfogadva3/3136ms8944 KiB