24732023-01-13 11:06:04TuruTamasTom és Jerry 3cpp11Wrong answer 29/5092ms7860 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] < 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
base29/50
1Accepted0/03ms1828 KiB
2Accepted0/02ms2104 KiB
3Accepted5/52ms2164 KiB
4Accepted1/13ms2308 KiB
5Wrong answer0/13ms2516 KiB
6Accepted1/13ms2712 KiB
7Wrong answer0/13ms2932 KiB
8Accepted1/13ms3012 KiB
9Wrong answer0/13ms3016 KiB
10Wrong answer0/13ms2996 KiB
11Wrong answer0/23ms3060 KiB
12Wrong answer0/23ms3148 KiB
13Wrong answer0/13ms3192 KiB
14Wrong answer0/292ms6108 KiB
15Wrong answer0/282ms4616 KiB
16Accepted2/290ms6240 KiB
17Accepted2/283ms7452 KiB
18Accepted2/292ms6312 KiB
19Accepted2/290ms6332 KiB
20Accepted2/257ms7420 KiB
21Wrong answer0/282ms4776 KiB
22Accepted2/282ms4976 KiB
23Accepted3/382ms7428 KiB
24Accepted2/290ms6276 KiB
25Wrong answer0/390ms6456 KiB
26Accepted2/285ms7860 KiB
27Accepted2/283ms5136 KiB
28Wrong answer0/383ms4996 KiB