Submission #911086

#TimeUsernameProblemLanguageResultExecution timeMemory
911086VMaksimoski008Valley (BOI19_valley)C++14
100 / 100
368 ms36548 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int LOG = 18, maxn = 1e5 + 5;
 
int n, s, q, e, timer = 0;
vector<vector<pii> > graph;
int store[maxn+1], in[maxn+1], out[maxn+1], depth[maxn+1], up[maxn+1][LOG];
ll dp[maxn+1], dist[maxn+1], mn[maxn+1][LOG];
 
void dfs(int u, int p) {
    in[u] = timer++;
    dp[u] = (store[u] ? dist[u] : 1e17);
 
    for(auto &[v, w] : graph[u]) {
        if(v == p) continue;
        dist[v] = dist[u] + w;
        dfs(v, u);
        dp[u] = min(dp[u], dp[v]);
    }
 
    out[u] = timer;
}

void dfs2(int u, int p) {
    for(int i=1; i<LOG; i++) {
        up[u][i] = up[ up[u][i-1] ][i-1];
        mn[u][i] = min(mn[u][i-1], mn[ up[u][i-1] ][i-1]);
    }

    for(auto &[v, _] : graph[u]) {
        if(v == p) continue;
        up[v][0] = u;
        mn[v][0] = dp[u];
        dfs2(v, u);
    }
}
 
bool anc(int u, int v) { return (in[u] <= in[v] && out[u] >= out[v]); }
 
int32_t main() {
    cin >> n >> s >> q >> e;
    vector<pii> edges;
    graph.resize(n+1);
 
    for(int i=0; i<n-1; i++) {
        int a, b, w;
        cin >> a >> b >> w;
        edges.push_back({ a, b });
        graph[a].push_back({ b, w });
        graph[b].push_back({ a, w });
    }
 
    for(int i=0; i<s; i++) {
        int x;
        cin >> x;
        store[x] = 1;
    }
 
    dfs(e, 0);
    for(int u=1; u<=n; u++) dp[u] -= 2 * dist[u];
    dfs2(e, 0);
 
    while(q--) {
        int I, R;
        cin >> I >> R;
        pii edge = edges[I-1];
 
        if(in[edge.first] < in[edge.second]) swap(edge.first, edge.second);
        if(anc(edge.first, R) == anc(edge.first, e)) {
            cout << "escaped\n";
            continue;
        }

        ll ans = dp[R] + dist[R];
        int u = R;
        
        for(int j=LOG-1; j>=0; j--) {
            if(up[u][j] == 0) continue;
            if(!anc(edge.first, up[u][j])) continue;
            ans = min(ans, dist[R] + mn[u][j]);
            u = up[u][j];
        }

        if(ans >= 1e15) cout << "oo\n";
        else cout << ans << '\n';
    }

    return 0;
}

Compilation message (stderr)

valley.cpp: In function 'void dfs(int, int)':
valley.cpp:17:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   17 |     for(auto &[v, w] : graph[u]) {
      |               ^
valley.cpp: In function 'void dfs2(int, int)':
valley.cpp:33:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   33 |     for(auto &[v, _] : graph[u]) {
      |               ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...