24742023-01-13 11:07:24TuruTamasTom és Jerry 3cpp11Accepted 50/5090ms8712 KiB
#include <bits/stdc++.h>
using namespace std;

int T, N, Tom, Jerry, K;

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

inline int 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);
        }
    }
    return k;
}

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;
        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);
        int max_d_tom = bfs(d_tom, graph, Tom);

        if(d_tom[Jerry] == 1)
        {
            cout << "IGEN\n";
            continue;
        }
        vi d_jerry(N, -1);
        bfs(d_jerry, graph, Jerry);
        bool f = false;
        for (size_t i = 0; i < d_jerry.size(); i++)
        {
            if (d_tom[i] > K && d_jerry[i] + 1 < d_tom[i]) {
                f = true;
                break;
            }   
        }
        if (!f) {
            cout << "IGEN\n";
            continue;
        }
        // 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;
        // }
        
        vi d2(N, -1);
        
        int nagy_tav = d2[bfs(d2, graph, max_d_tom)];
        if (nagy_tav >= K*2 + 1) cout << "NEM\n";
        else cout << "IGEN\n";
    } // q (question)
    cout << '\n';
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1824 KiB
2Accepted0/02ms2068 KiB
3Accepted5/52ms2260 KiB
4Accepted1/13ms2440 KiB
5Accepted1/13ms2540 KiB
6Accepted1/13ms2644 KiB
7Accepted1/13ms2624 KiB
8Accepted1/13ms2748 KiB
9Accepted1/13ms2964 KiB
10Accepted1/13ms3304 KiB
11Accepted2/23ms3424 KiB
12Accepted2/23ms3512 KiB
13Accepted1/13ms3740 KiB
14Accepted2/290ms6504 KiB
15Accepted2/282ms5004 KiB
16Accepted2/290ms6632 KiB
17Accepted2/282ms7848 KiB
18Accepted2/290ms6916 KiB
19Accepted2/290ms7152 KiB
20Accepted2/257ms8316 KiB
21Accepted2/281ms5660 KiB
22Accepted2/282ms5716 KiB
23Accepted3/382ms8388 KiB
24Accepted2/290ms7172 KiB
25Accepted3/389ms7220 KiB
26Accepted2/285ms8712 KiB
27Accepted2/282ms5844 KiB
28Accepted3/383ms6072 KiB