13752022-08-28 13:03:04csandrasTom és Jerry 3cpp11Accepted 50/50207ms10764 KiB
#include <bits/stdc++.h>

using namespace std;

using Graph = vector<vector<int>>;


void dfs(int p, const Graph& G, vector<int>& dist, vector<bool>& visited)
{
    visited[p] = true;
    for (int q : G[p])
    {
        if (!visited[q])
        {
            dist[q] = dist[p] + 1;
            dfs(q, G, dist, visited);
        }
    }
}

vector<int> distances_from(int start, const Graph& G)
{
    vector<int> dist(G.size(), -1);
    dist[start] = 0;
    vector<bool> visited(G.size(), false);
    dfs(start, G, dist, visited);
    return dist;
}

int diameter(const Graph& G)
{
    vector<int> dist = distances_from(0, G);
    int start = max_element(dist.begin(), dist.end()) - dist.begin();
    vector<int> dist2 = distances_from(start, G);
    return *max_element(dist2.begin(), dist2.end());
}

bool can_caught_in_K_step(const Graph& G, int TS, int JS, int K)
{
    vector<int> dist_T = distances_from(TS, G);
    vector<int> dist_J = distances_from(JS, G);
    for (int i = 0; i < G.size(); ++i)
    {
        if (dist_J[i]+1 < dist_T[i] && dist_T[i] > K)
        {
            return false;
        }
    }
    return true;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int N, TS, JS, K;
        cin >> N >> TS >> JS >> K;
        --TS, --JS;
        vector<vector<int>> G(N);
        for (int i = 0; i < N-1; ++i)
        {
            int u, v;
            cin >> u >> v;
            --u, --v;
            G[u].push_back(v);
            G[v].push_back(u);
        }
        if (diameter(G) <= 2*K || can_caught_in_K_step(G, TS, JS, K))
        {
            cout << "IGEN\n";
        }
        else
        {
            cout << "NEM\n";
        }
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1812 KiB
2Accepted0/02ms2060 KiB
3Accepted5/52ms2256 KiB
4Accepted1/13ms2512 KiB
5Accepted1/14ms2712 KiB
6Accepted1/14ms2920 KiB
7Accepted1/14ms3120 KiB
8Accepted1/13ms3276 KiB
9Accepted1/14ms3440 KiB
10Accepted1/13ms3376 KiB
11Accepted2/23ms3412 KiB
12Accepted2/24ms3404 KiB
13Accepted1/14ms3388 KiB
14Accepted2/2196ms6040 KiB
15Accepted2/2165ms4792 KiB
16Accepted2/2184ms6300 KiB
17Accepted2/2175ms7652 KiB
18Accepted2/2207ms6392 KiB
19Accepted2/2172ms6608 KiB
20Accepted2/2119ms10764 KiB
21Accepted2/2168ms5668 KiB
22Accepted2/2162ms5552 KiB
23Accepted3/3189ms8656 KiB
24Accepted2/2196ms7044 KiB
25Accepted3/3189ms7016 KiB
26Accepted2/2190ms8848 KiB
27Accepted2/2180ms5760 KiB
28Accepted3/3174ms5888 KiB