24842023-01-13 12:47:50rennTom és Jerry2 (60)cpp11Time limit exceeded 4/60533ms11496 KiB
#include <bits/stdc++.h>
using namespace std;

#define InTheNameOfGod cin.tie(0); cout.tie(0); ios::sync_with_stdio(0);
typedef vector<vector<int>> graf;
typedef vector<int> vi;
typedef vector<bool> vb;

vb cuts;
int timer;
void dfs(int v, int p, graf &adj, vb &volt, vi &tin, vi &low) {
    volt[v] = true;
    tin[v] = low[v] = timer++;
    int children=0;
    for (int to : adj[v]) {
        if (to == p) continue;
        if (volt[to]) {
            low[v] = min(low[v], tin[to]);
        } else {
            dfs(to, v, adj, volt, tin, low);
            low[v] = min(low[v], low[to]);
            if (low[to] >= tin[v] && p!=-1)
                cuts[v] = true;
            ++children;
        }
    }
    if(p == -1 && children > 1)
        cuts[v] = true;
}

inline int bfs(graf &g, int &k, vi &tavok)
{
    queue<int> next;
    next.push(k);
    tavok[k] = 0;

    int i;
    while(!next.empty())
    {
        i = next.front();
        next.pop();

        for(auto &x : g[i])
        {
            if(tavok[x] > -1) continue;

            next.push(x);
            tavok[x] = tavok[i]+1;
        }
    }

    return 0;
}

inline int jerry_bfs(graf &g, int &k, vi &tavok, vi &tomtavok, int &e)
{
    queue<int> next;
    next.push(k);
    tavok[k] = 0;

    int i, j = -1;
    while(!next.empty())
    {
        i = next.front();
        next.pop();

        for(auto &x : g[i])
        {
            if(tavok[x] > -1)
                continue;
            
            if(cuts[x])
                if(tomtavok[x] <= tavok[i]+1)
                {
                    j = j == -1 ? x : j;
                    continue;
                }
            
            if(x == e)
                return 0;

            next.push(x);
            tavok[x] = tavok[i]+1;
        }
    }

    return j+1;
}

int main()
{

    //InTheNameOfGod

    int n, m, t, jdb, j, e;
    cin >> n >> m >> t >> jdb >> e;
    e--;
    t--;

    vi tomd(n, -1);
    graf tom(n);
    graf jerry(n);
    cuts.assign(n, false);

    int a, b, c;
    bool h;

    while(m--)
    {
        cin >> a >> b >> c;
        a--; b--;

        if(((c&1)^1) && a!=e && b!=e)
        {
            tom[a].push_back(b);
            tom[b].push_back(a);
        }

        jerry[a].push_back(b);
        jerry[b].push_back(a);
    }

    bfs(tom, t, tomd);

    timer = 0;
    vb volt(n, false);
    vi tin(n, -1);
    vi low(n, -1);
    dfs(e, -1, jerry, volt, tin, low);

    while(jdb--)
    {
        vi jerryd(n, -1);

        cin >> j;
        j--;

        cout << jerry_bfs(jerry, j, jerryd, tomd, e) << "\n";
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base4/60
1Accepted0/03ms1812 KiB
2Time limit exceeded0/0500ms2756 KiB
3Accepted2/22ms2308 KiB
4Wrong answer0/22ms2360 KiB
5Wrong answer0/22ms2436 KiB
6Wrong answer0/32ms2672 KiB
7Accepted2/23ms2672 KiB
8Wrong answer0/24ms3040 KiB
9Wrong answer0/24ms2992 KiB
10Wrong answer0/37ms3320 KiB
11Wrong answer0/325ms3300 KiB
12Wrong answer0/318ms3944 KiB
13Wrong answer0/326ms4912 KiB
14Wrong answer0/337ms5480 KiB
15Wrong answer0/356ms6024 KiB
16Wrong answer0/375ms6416 KiB
17Time limit exceeded0/3469ms5892 KiB
18Time limit exceeded0/3533ms8628 KiB
19Time limit exceeded0/4458ms7540 KiB
20Time limit exceeded0/4462ms9996 KiB
21Time limit exceeded0/5462ms9640 KiB
22Time limit exceeded0/5470ms11496 KiB