Submission #342963

#TimeUsernameProblemLanguageResultExecution timeMemory
342963spike1236Valley (BOI19_valley)C++14
100 / 100
295 ms50156 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>


const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;

const int MAXN = 1e5 + 10;
int n, s, q;
int root, tmr;
int tin[MAXN], tout[MAXN];
set <pii> g[MAXN];
pii EDGE[MAXN];
int is_shop[MAXN];
pii query[MAXN];
ll dp[MAXN][20];
ll rtsum[MAXN];
int up[MAXN][20];

void dfs_sz(int v = root, int p = 0, ll sum = 0) {
    tin[v] = ++tmr;
    up[v][0] = p;
    rtsum[v] = sum;
    if(is_shop[v]) dp[v][0] = sum;
    else dp[v][0] = 1e18;
    for(auto to : g[v]) {
        if(to.f != p) {
            dfs_sz(to.f, v, sum + to.s);
            dp[v][0] = min(dp[v][0], dp[to.f][0]);
        }
    }
    tout[v] = ++tmr;
}

bool upper(int v, int u) {
    return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}

ll get(int v, int u) {
    ll res = 1e18;
    ll d = rtsum[v];
    for(int i = 18; i >= 0; --i)
        if(up[v][i] && !upper(up[v][i], u))
            res = min(res, dp[v][i] + d), v = up[v][i];
    return min(res, dp[v][0] + d);
}

void solve() {
    cin >> n >> s >> q >> root;
    for(int i = 1; i < n; ++i) {
        int u, v, cost;
        cin >> u >> v >> cost;
        g[u].insert(mp(v, cost));
        g[v].insert(mp(u, cost));
        EDGE[i] = mp(u, v);
    }
    for(int i = 1; i <= s; ++i) {
        int x;
        cin >> x;
        is_shop[x] = 1;
    }
    for(int i = 1; i <= q; ++i)
        cin >> query[i].f >> query[i].s;
    dfs_sz();
    for(int i = 1; i <= n; ++i) dp[i][0] -= 2 * rtsum[i];
    for(int i = 1; i < 19; ++i)
        for(int v = 1; v <= n; ++v)
            if(up[v][i - 1])
                up[v][i] = up[up[v][i - 1]][i - 1], dp[v][i] = min(dp[v][i - 1], dp[up[v][i - 1]][i - 1]);
    for(int i = 1; i <= q; ++i) {
        int blv = EDGE[query[i].f].f, blu = EDGE[query[i].f].s, v = query[i].s;
        if(!upper(blv, blu)) swap(blv, blu);
        if(upper(blu, v)) {
            ll res = get(v, blv);
            if(res >= (ll)1e16) cout << "oo\n";
            else cout << res << '\n';
        } else cout << "escaped\n";
    }
}


int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    ///cin >> T;
    while(T--) solve(), 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...