24302023-01-13 09:25:28TuruTamasTom és Jerry 3cpp11Wrong answer 29/50760ms129740 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, stack<int> &path, int a, int &b, int dist, bool &end, vector<bool> &visited) {
//     path.push(a);
//     visited[a] = true;
//     for (int x : graph[a]) {
//         if (visited[x]) continue;
//         if (end) {
//             return;
//         }
//         if (x == b) {
//             path.push(b);
//             end = true;
//         }
//         else {
//             dfs_shortest_path(graph, path, x, b, dist + 1, end, visited);
//         }
//     }
//     if (!end) path.pop();
// }

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<vi> 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;
        // }
        // // stack<int> path;
        // {
        // // bool end = false;
        // // vector<bool> visited(N, false);
        // // dfs_shortest_path(graph, path, max_d2, max_d_tom, 0, end, visited);
        // }
        // cout << d2[max_d2] << ' ' << max_d2 << '\n';
        // cout << *max_element(d2.begin(), d2.end()) << '\n';
        int nagy_tav = *max_element(d2.begin(), d2.end());
        if (nagy_tav/2 >= K) cout << "NEM\n";
        else cout << "IGEN\n";
    } // q (question)
}
SubtaskSumTestVerdictTimeMemory
base29/50
1Accepted0/03ms1824 KiB
2Accepted0/02ms2212 KiB
3Wrong answer0/52ms2228 KiB
4Accepted1/13ms2464 KiB
5Wrong answer0/13ms2664 KiB
6Accepted1/13ms2744 KiB
7Wrong answer0/13ms2948 KiB
8Wrong answer0/13ms3036 KiB
9Wrong answer0/13ms3076 KiB
10Accepted1/13ms3272 KiB
11Accepted2/23ms3464 KiB
12Accepted2/23ms3672 KiB
13Accepted1/13ms3640 KiB
14Accepted2/2237ms18036 KiB
15Accepted2/2442ms15944 KiB
16Accepted2/2648ms21924 KiB
17Runtime error0/2321ms129740 KiB
18Accepted2/2432ms32084 KiB
19Accepted2/2532ms11924 KiB
20Runtime error0/268ms129096 KiB
21Accepted2/2456ms25968 KiB
22Accepted2/2167ms7996 KiB
23Time limit exceeded0/3760ms49904 KiB
24Accepted2/2367ms32076 KiB
25Accepted3/3104ms10344 KiB
26Runtime error0/2467ms129080 KiB
27Accepted2/2230ms16716 KiB
28Wrong answer0/383ms7116 KiB