13732022-08-28 12:35:22csandrasTom és Jerry 3cpp11Wrong answer 7/50200ms35612 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] == K && dist_T[i] < K)
        {
            return true;
        }
    }
    return false;
}

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
base7/50
1Accepted0/03ms1808 KiB
2Wrong answer0/03ms2068 KiB
3Accepted5/52ms2292 KiB
4Accepted1/14ms2632 KiB
5Wrong answer0/14ms2736 KiB
6Wrong answer0/14ms2964 KiB
7Wrong answer0/13ms3040 KiB
8Accepted1/13ms3068 KiB
9Wrong answer0/14ms3504 KiB
10Wrong answer0/14ms3268 KiB
11Wrong answer0/23ms3500 KiB
12Wrong answer0/23ms3736 KiB
13Wrong answer0/13ms3984 KiB
14Wrong answer0/2199ms8760 KiB
15Wrong answer0/2173ms9816 KiB
16Wrong answer0/2185ms13296 KiB
17Wrong answer0/2174ms16612 KiB
18Wrong answer0/2200ms17452 KiB
19Wrong answer0/2177ms19484 KiB
20Wrong answer0/2120ms24532 KiB
21Wrong answer0/2168ms21320 KiB
22Wrong answer0/2160ms23476 KiB
23Wrong answer0/3190ms28448 KiB
24Wrong answer0/2200ms29120 KiB
25Wrong answer0/3192ms31116 KiB
26Wrong answer0/2194ms34884 KiB
27Wrong answer0/2180ms33836 KiB
28Wrong answer0/3173ms35612 KiB