제출 #753472

#제출 시각아이디문제언어결과실행 시간메모리
753472hafoValley (BOI19_valley)C++14
100 / 100
243 ms43588 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e18 + 69;

int n, s, q, e, u, v, w;
ll dp[maxn], f[maxn];
vector<pa> g[maxn];
bool is_shop[maxn];
pa edge[maxn];

void dfs(int u, int par) {
    dp[u] = oo;
    if(is_shop[u]) dp[u] = f[u];
    for(auto e:g[u]) {
        int v = e.fi, w = e.se;
        if(v == par) continue;
        f[v] = f[u] + w;
        dfs(v, u);
        mini(dp[u], dp[v]);
    }
}

struct LCA {
    int p[maxn][LOG], f[maxn];
    ll mn[maxn][LOG];

    void dfs(int u, int par) {
        p[u][0] = par;
        for(auto e:g[u]) {
            int v = e.fi;
            if(v == par) continue;
            f[v] = f[u] + 1;
            mn[v][0] = dp[v];
            dfs(v, u);
        }
    }

    ll get(int u, int v) {
        if(f[u] < f[v]) swap(u, v);
        int k = f[u] - f[v];
        ll res = dp[u];
        for(int i = 0; i < LOG; i++)
        if((k >> i) & 1) {
            mini(res, mn[u][i]);
            u = p[u][i];
        }
        mini(res, dp[v]);
        if(u == v) return res;
        for(int i = LOG - 1; i >= 0; i--)
        if(p[u][i] != p[v][i]) {
            mini(res, mn[u][i]);
            u = p[u][i];
            mini(res, mn[v][i]);
            v = p[v][i];
        }
        mini(res, mn[u][0]);
        mini(res, mn[v][0]);
        mini(res, mn[p[u][0]][0]);
        return res;
    }

    void init() {
        f[e] = 0;
        mn[e][0] = dp[e];
        dfs(e, 0);

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

    bool check(int u, int v) {
        int k = f[u] - f[v];
        for(int i = 0; i < LOG; i++)
            if((k >> i) & 1) u = p[u][i];
        return (u == v);
    }

} lca;

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

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n>>s>>q>>e;
    for(int i = 1; i < n; i++) {
        cin>>u>>v>>w;
        g[u].pb({v, w});
        g[v].pb({u, w});
        edge[i] = {u, v};
    }
    for(int i = 1; i <= s; i++) {
        int x;
        cin>>x;
        is_shop[x] = 1;
    }

    f[e] = 0;
    dfs(e, 0);
    for(int i = 1; i <= n; i++)
        if(dp[i] != oo) dp[i] -= 2LL * f[i];
    lca.init();
    while(q--) {
        int x, id;
        cin>>id>>x;

        u = edge[id].fi, v = edge[id].se;
        if(lca.f[u] < lca.f[v]) swap(u, v);
        if(!lca.check(x, u)) {
            cout<<"escaped\n";
            continue;
        }

        ll ans = lca.get(x, u);
        if(ans == oo) cout<<"oo";
        else cout<<ans + f[x];
        cout<<"\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...