23992023-01-12 16:13:05TuruTamasTom és Jerry 3cpp11Wrong answer 13/50769ms129800 KiB
#include <bits/stdc++.h>
using namespace std;

int T, N, Tom, Jerry, K;

#define vvig vector<vector<int>> &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 + 1) 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;
        
        vector<vi> graph(N);
        for (size_t i = 0; i < N - 1; i++)
        {
            cin >> a >> b;
            a--; b--;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        
        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);
        }
        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;
        }
        
        vi d2(N, -1);
        bfs(d2, graph, max_d_tom);
        int max_d2 = 0;
        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);
        }
        int middle = path[path.size()/2];

        vi d_middle(N, -1);

        bfs(d_middle, graph, middle);
        int counter = 0;
        vector<bool> visited(N, false);
        for (size_t i = 0; i < d_middle.size(); i++)
        {
            if (visited[i] || d_middle[i] <= K) continue;
            counter++;
            dfs_visit(visited, graph, i, d_middle);
        }
        if (counter >= 2) cout << "NEM\n";
        else cout << "IGEN\n";
    } // q (question)
}
SubtaskSumTestVerdictTimeMemory
base13/50
1Accepted0/03ms1832 KiB
2Accepted0/02ms2040 KiB
3Wrong answer0/52ms2360 KiB
4Accepted1/13ms2644 KiB
5Wrong answer0/13ms2732 KiB
6Wrong answer0/13ms3076 KiB
7Wrong answer0/13ms3024 KiB
8Wrong answer0/13ms3392 KiB
9Wrong answer0/13ms3436 KiB
10Wrong answer0/13ms3760 KiB
11Wrong answer0/23ms3632 KiB
12Wrong answer0/23ms3904 KiB
13Wrong answer0/13ms4144 KiB
14Wrong answer0/2254ms19648 KiB
15Wrong answer0/2449ms19956 KiB
16Accepted2/2660ms28444 KiB
17Runtime error0/2349ms129800 KiB
18Accepted2/2435ms32484 KiB
19Accepted2/2560ms15560 KiB
20Runtime error0/268ms129644 KiB
21Wrong answer0/2460ms27280 KiB
22Accepted2/2180ms11544 KiB
23Time limit exceeded0/3769ms54688 KiB
24Accepted2/2395ms39256 KiB
25Wrong answer0/3112ms18728 KiB
26Runtime error0/2472ms129388 KiB
27Accepted2/2234ms16540 KiB
28Wrong answer0/393ms7228 KiB