24172023-01-13 08:46:47TuruTamasTom és Jerry 3cpp11Wrong answer 30/50774ms130044 KiB
#include <bits/stdc++.h>
using namespace std;

int T, N, Tom, Jerry, K;

#define vvig vector<vector<short>> &graph
#define vi vector<int>

void bfs(vector<int> &d, vvig, int kezd) {
    queue<int> q;
    q.push(kezd);
    d[kezd] = 0;
    int k;
    while (!q.empty())
    {
        k = q.front();
        q.pop();
        for (int x : graph[k]) {
            if (d[x] != -1) continue;
            d[x] = d[k] + 1;
            q.push(x);
        }
    }
}

void dfs_shortest_path(vvig, vi &path, int a, int &b, int dist, bool &end, vector<bool> &visited) {
    path.push_back(a);
    visited[a] = true;
    for (int x : graph[a]) {
        if (visited[x]) continue;
        if (end) {
            return;
        }
        if (x == b) {
            path.push_back(b);
            end = true;
        }
        else {
            dfs_shortest_path(graph, path, x, b, dist + 1, end, visited);
        }
    }
    if (!end) path.pop_back();
}

void dfs_visit(vector<bool> &visited, vvig, int a, vi &d) {
    visited[a] = true;
    for (int x: graph[a]) {
        if (visited[x] || d[x] <= K) continue;
        dfs_visit(visited, graph, x, d);
    }
}

void dfs_Jerry(vvig, vi d_tom, int loc, vector<bool> &visited, int dist, bool &success, bool &end) {
    visited[loc] = true;
    for (int x : graph[loc]) {
        if (end) return;
        if (visited[x] || d_tom[x] <= dist + 2) continue;
        if (d_tom[x] > K) {
            success = true;
            end = true;
            return;
        }
        dfs_Jerry(graph, d_tom, x, visited, dist + 1, success, end);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    for (size_t q = 0; q < T; q++)
    {
        cin >> N >> Tom >> Jerry >> K;
        Tom--; Jerry--;
        int a, b;
        if (K > N) {
            cout << "IGEN\n";
            continue;
        }
        vector<vector<short>> graph(N);
        {
        bool f = false;
        for (size_t i = 0; i < N - 1; i++)
        {
            cin >> a >> b;
            a--; b--;
            if ((a == Tom && b == Jerry) || (b == Tom && a == Jerry) && K > 0) {
                f = true;
            }
            if (!f){
                graph[a].push_back(b);
                graph[b].push_back(a);
            }
        }
        if (f) {
            cout << "IGEN\n";
            continue;
        }
        }
        vi d_tom(N, -1);
        bfs(d_tom, graph, Tom);

        bool success = false;
        {
        vector<bool> visited(N, false);
        bool end = false;
        dfs_Jerry(graph, d_tom, Jerry, visited, 0, success, end);
        }
        // cout << success << '\n';
        if (!success) {
            cout << "IGEN\n";
            continue;
        }

        int max_d_tom = 0;
        for (size_t i = 0; i < d_tom.size(); i++)
        {
            max_d_tom = d_tom[max_d_tom] < d_tom[i] ? i : max_d_tom;
        }
        int max_d2 = 0;
        {
        vi d2(N, -1);
        bfs(d2, graph, max_d_tom);
        for (size_t i = 0; i < d_tom.size(); i++)
        {
            max_d2 = d2[max_d2] < d2[i] ? i : max_d2;
        }
        }
        vi path;
        {
        bool end = false;
        vector<bool> visited(N, false);
        dfs_shortest_path(graph, path, max_d2, max_d_tom, 0, end, visited);
        }
        if (path.size()/2 > K) cout << "NEM\n";
        else cout << "IGEN\n";
    } // q (question)
}
SubtaskSumTestVerdictTimeMemory
base30/50
1Accepted0/03ms1956 KiB
2Accepted0/02ms2240 KiB
3Wrong answer0/52ms2440 KiB
4Accepted1/13ms2396 KiB
5Wrong answer0/13ms2532 KiB
6Accepted1/13ms2884 KiB
7Wrong answer0/13ms3092 KiB
8Accepted1/13ms3064 KiB
9Wrong answer0/13ms3256 KiB
10Accepted1/13ms3328 KiB
11Accepted2/23ms3348 KiB
12Accepted2/23ms3320 KiB
13Accepted1/13ms3312 KiB
14Accepted2/2236ms17816 KiB
15Accepted2/2439ms15560 KiB
16Accepted2/2657ms21644 KiB
17Runtime error0/2342ms130044 KiB
18Accepted2/2432ms31964 KiB
19Accepted2/2533ms11220 KiB
20Runtime error0/268ms129800 KiB
21Accepted2/2449ms25272 KiB
22Accepted2/2167ms7520 KiB
23Time limit exceeded0/3774ms45312 KiB
24Accepted2/2381ms31508 KiB
25Accepted3/3107ms10016 KiB
26Runtime error0/2448ms129264 KiB
27Accepted2/2237ms16452 KiB
28Wrong answer0/386ms7160 KiB