23902023-01-12 13:34:13sztomiTom és Jerry2 (60)cpp11Time limit exceeded 2/60500ms11640 KiB
#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9+7;

vector<int> tavok(int start, vector<vector<int>>& graf, int n){
    vector<int> ki(n+1, INF);
    vector<bool> volt(n+1, false);
    queue<int> q;
    q.push(start);
    volt[start] = true;
    int akt, akt_meret = 1, kov_meret = 0, tav = 0;
    while(!q.empty()){
        akt = q.front();
        akt_meret--;
        ki[akt] = tav;
        q.pop();

        for(int kov : graf[akt]){
            if(volt[kov]) continue;
            q.push(kov);
            kov_meret++;
            volt[kov] = true;
        }

        if(akt_meret == 0){
            swap(akt_meret, kov_meret);
            tav++;
        }
    }

    return ki;
}

void dfs(int akt, int szulo, int& ido, vector<vector<int>>& graf, vector<int>& be_ido, vector<int>& kis, vector<bool>& volt, vector<bool>& vago){
    be_ido[akt] = kis[akt] = ido++;
    volt[akt] = true;

    int gyerek_db = 0;
    for(int kov : graf[akt]){
        if(kov == szulo) continue;
        if(volt[kov]){
            kis[akt] = min(kis[akt], be_ido[kov]);
        }
        else{
            dfs(kov, akt, ido, graf, be_ido, kis, volt, vago);
            gyerek_db++;
            kis[akt] = min(kis[akt], kis[kov]);

            if(be_ido[akt] <= kis[kov]){
                vago[akt] = true;
            }
        }
    }
    if(szulo == -1 && gyerek_db == 1){
        vago[akt] = true;
    }
}


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m, t, j_db, lyuk;
    cin >> n >> m >> t >> j_db >> lyuk;
    vector<vector<int>> tom_graf(n+1, vector<int>());
    vector<vector<int>> jerry_graf(n+1, vector<int>());

    int a, b, c;
    for(int i = 0; i < m; i++){
        cin >> a >> b >> c;
        jerry_graf[a].push_back(b);
        jerry_graf[b].push_back(a);
        if(c == 2){
            tom_graf[a].push_back(b);
            tom_graf[b].push_back(a);
        }
    }

    int ido = 0;
    vector<int> be_ido(n+1, INF);
    vector<int> kis(n+1, INF);
    vector<bool> volt(n+1, false);
    vector<bool> vago(n+1, false);
    dfs(lyuk, -1, ido, jerry_graf, be_ido, kis, volt, vago);

    vector<int> tom_tav = tavok(t, tom_graf, n);

    int x;
    for(int i = 0; i < j_db; i++){
        cin >> x;
        int elkapja = 0;

        // mely vagopontok vannak jerry es a lyuk kozott - itt tudja Tom biztosan elkapni
        vector<bool> volt(n+1, false);
        queue<int> q;
        q.push(x);
        volt[x] = true;
        int akt, akt_meret = 1, kov_meret = 0, tav = 0;
        while(!q.empty()){
            akt = q.front();
            akt_meret--;
            if(tav >= tom_tav[akt] && vago[akt]){
                elkapja = akt;
            }
            q.pop();

            for(int kov : jerry_graf[akt]){
                if(volt[kov]) continue;
                q.push(kov);
                kov_meret++;
                volt[kov] = true;
            }

            if(akt_meret == 0){
                swap(akt_meret, kov_meret);
                tav++;
            }
        }

        cout << elkapja << "\n";
    }


}
SubtaskSumTestVerdictTimeMemory
base2/60
1Accepted0/03ms1828 KiB
2Time limit exceeded0/0500ms2988 KiB
3Accepted2/22ms2492 KiB
4Wrong answer0/22ms2560 KiB
5Wrong answer0/22ms2416 KiB
6Wrong answer0/32ms2548 KiB
7Wrong answer0/23ms2904 KiB
8Wrong answer0/214ms3060 KiB
9Wrong answer0/28ms2904 KiB
10Wrong answer0/320ms3096 KiB
11Wrong answer0/326ms3124 KiB
12Wrong answer0/3100ms3556 KiB
13Wrong answer0/3275ms4460 KiB
14Time limit exceeded0/3458ms3108 KiB
15Time limit exceeded0/3469ms3460 KiB
16Time limit exceeded0/3465ms3696 KiB
17Time limit exceeded0/3465ms5508 KiB
18Time limit exceeded0/3462ms8416 KiB
19Time limit exceeded0/4483ms7220 KiB
20Time limit exceeded0/4469ms9792 KiB
21Time limit exceeded0/5451ms9892 KiB
22Time limit exceeded0/5462ms11640 KiB