24312023-01-13 09:31:35TuruTamasTom és Jerry 3cpp11Hibás válasz 30/50755ms130176 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)) {
                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 >= K*2 + 1) cout << "NEM\n";
        else cout << "IGEN\n";
    } // q (question)
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base30/50
1Elfogadva0/03ms1832 KiB
2Elfogadva0/02ms2040 KiB
3Hibás válasz0/52ms2228 KiB
4Elfogadva1/13ms2580 KiB
5Hibás válasz0/13ms2704 KiB
6Elfogadva1/13ms2736 KiB
7Hibás válasz0/13ms2928 KiB
8Elfogadva1/13ms3020 KiB
9Hibás válasz0/13ms3004 KiB
10Elfogadva1/13ms3192 KiB
11Elfogadva2/23ms3400 KiB
12Elfogadva2/23ms3608 KiB
13Elfogadva1/13ms3608 KiB
14Elfogadva2/2232ms17824 KiB
15Elfogadva2/2444ms15652 KiB
16Elfogadva2/2654ms21560 KiB
17Futási hiba0/2344ms130176 KiB
18Elfogadva2/2421ms31600 KiB
19Elfogadva2/2532ms11176 KiB
20Futási hiba0/268ms129732 KiB
21Elfogadva2/2444ms25324 KiB
22Elfogadva2/2166ms7364 KiB
23Időlimit túllépés0/3755ms49472 KiB
24Elfogadva2/2363ms31612 KiB
25Elfogadva3/3104ms9876 KiB
26Futási hiba0/2467ms129524 KiB
27Elfogadva2/2232ms16248 KiB
28Hibás válasz0/383ms6664 KiB