13752022-08-28 13:03:04csandrasTom és Jerry 3cpp11Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1812 KiB
2Elfogadva0/02ms2060 KiB
3Elfogadva5/52ms2256 KiB
4Elfogadva1/13ms2512 KiB
5Elfogadva1/14ms2712 KiB
6Elfogadva1/14ms2920 KiB
7Elfogadva1/14ms3120 KiB
8Elfogadva1/13ms3276 KiB
9Elfogadva1/14ms3440 KiB
10Elfogadva1/13ms3376 KiB
11Elfogadva2/23ms3412 KiB
12Elfogadva2/24ms3404 KiB
13Elfogadva1/14ms3388 KiB
14Elfogadva2/2196ms6040 KiB
15Elfogadva2/2165ms4792 KiB
16Elfogadva2/2184ms6300 KiB
17Elfogadva2/2175ms7652 KiB
18Elfogadva2/2207ms6392 KiB
19Elfogadva2/2172ms6608 KiB
20Elfogadva2/2119ms10764 KiB
21Elfogadva2/2168ms5668 KiB
22Elfogadva2/2162ms5552 KiB
23Elfogadva3/3189ms8656 KiB
24Elfogadva2/2196ms7044 KiB
25Elfogadva3/3189ms7016 KiB
26Elfogadva2/2190ms8848 KiB
27Elfogadva2/2180ms5760 KiB
28Elfogadva3/3174ms5888 KiB