Submission #1171664

#TimeUsernameProblemLanguageResultExecution timeMemory
1171664paulxaxaValley (BOI19_valley)C++20
23 / 100
257 ms46488 KiB
#include <bits/stdc++.h>

#define NMAX 100000
#define LOG 20

#define ll long long int
#define BASE 1024

#define MOD 1000000009


using namespace std;

ifstream fin("cod.in");
ofstream fout("cod.out");

ll minDist[NMAX+1],dist[NMAX+1];
int isShop[NMAX+1];

vector<pair<int,int>> adj[NMAX+1];
pair<int,int> edges[NMAX+1];

int d[NMAX+1];
pair<int,ll> p[NMAX+1][LOG+1];
int n,s,q,e;

void dfs(int x,int t)
{
    minDist[x] = isShop[x] ? 0 : 1e17;
    p[x][0].first = t;
    d[x] = d[t] + 1;
    for(auto [y,c] : adj[x])
    {
        if(y!=t)
        {
            dist[y] = dist[x] + c;
            dfs(y,x);
            minDist[x] = min(minDist[x], minDist[y] + c);
        }
    }


}


pair<int,ll> kth(int x,int k)
{
    ll mn = minDist[x] == 1e17 ? 1e17 : minDist[x] - dist[x];
    for(int i=LOG;i>=0;i--)
    {
        if(d[x]-(1<<i) >= k)
        {
            mn = min(mn,p[x][i].second);
            x = p[x][i].first;
        }
    }

    return {x,mn};
}
int main()
{

    cin >> n >> s >> q >> e;
    for(int i=1;i<n;i++)
    {
        int a,b,w;
        cin >> a >> b >> w;
        edges[i] = {a,b};
        adj[a].push_back({b,w});
        adj[b].push_back({a,w});
    }
    for(int i=1;i<=s;i++)
    {
        int x;
        cin >> x;
        isShop[x] = 1;
    }

    dfs(e,0);


    for(int i=1;i<=n;i++)
    {
        p[i][0].second = min(minDist[p[i][0].first] - dist[p[i][0].first] , minDist[i] == 1e17 ? (ll)1e17 : minDist[i] - dist[i]);
    }

    for(int j=1;j<=LOG;j++)
    {
        for(int i=1;i<=n;i++)
        {
            p[i][j].first = p[p[i][j-1].first][j-1].first;
            p[i][j].second = min(p[i][j-1].second,p[p[i][j-1].first][j-1].second);
        }
    }
    for(int i=1;i<n;i++)
    {
        if(d[edges[i].first] > d[edges[i].second])
        {
            swap(edges[i].first,edges[i].second);
        }
    }


    while(q--)
    {
        int i,x;
        cin >> i >> x;
        int t = edges[i].second;
        if(d[t]<=d[x] && kth(x,d[t]).first==t)
        {
            ll mn = kth(x,d[t]).second;
            if(mn==1e17)
            {
                cout << "oo\n";
            }
            else
            {
                cout << mn+dist[x] << '\n';
            }
        }
        else
        {
            cout << "escaped\n";
        }

    }
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...