제출 #1327380

#제출 시각아이디문제언어결과실행 시간메모리
1327380lizaValley (BOI19_valley)C++20
59 / 100
3100 ms200764 KiB
#include <bits/stdc++.h>

using namespace std;

int n, s, q, e;
int const N = 100005;
vector<pair<int, int>> g[N];
vector <int> et;
pair<int, int> eti[N];
vector<int> shops;
map<pair<int, int>, long long> froms;
pair<int, int> roads[N];
void fet(int u, int p)
{
    et.push_back(u);
    eti[u]={et.size()-1, 0};
    for(auto i: g[u])
    {
        if(i.first == p)continue;
        fet(i.first, u);
    }
    et.push_back(u);
    eti[u]={eti[u].first, et.size()-1};
}

vector<pair<int, int>> qi;
map<int, int> isr;
map<int, int> iss;
void dis(int sak, int u, int p,  long long di=0)
{
    if(isr[u])
    {
        if(iss[u] && di!=0){}
        else
        froms[{sak, u}]=di;
    }

    for(auto i: g[u])
    {
        if(i.first == p)continue;
        dis(sak, i.first, u, di+i.second);
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> s >> q >> e;
    for(int i = 0; i < n-1; i++)
    {
        int a, b, w; cin >> a >> b >> w;
        g[a].push_back({b, w});
        g[b].push_back({a, w});
        roads[i+1]={a, b};
    }
    for(int i =0; i < s; i++)
    {
        int x; cin >> x;
        shops.push_back(x);
        iss[x]=1;

    }
    fet(e, 0);

    for(int i = 0; i < q; i++)
    {
        int a, b; cin >> a >> b;
        qi.push_back({a, b});
        isr[b]++;
    }

    for(int i =0; i < s; i++)
    {
        int x = shops[i];
        if(s!=n)dis(x, x, 0);
    }
    for(int jj = 0; jj < q; jj++)
    {
        int i, r; i=qi[jj].first;
        r =  qi[jj].second;
        if(eti[roads[i].first].first <= eti[r].first && eti[roads[i].second].first <= eti[r].first && eti[roads[i].first].second >= eti[r].second && eti[roads[i].second].second >= eti[r].second)
        {
            if(s==n)
            {
                cout << "0\n"; continue;
            }
            long long rez=-1;
            for(auto k: shops)
            {
                if(eti[roads[i].first].first <= eti[k].first && eti[roads[i].second].first <= eti[k].first && eti[roads[i].first].second >= eti[k].second && eti[roads[i].second].second >= eti[k].second)
                {
                    if(rez==-1) rez = froms[make_pair(k, r)];
                    else
                    rez=min(rez, froms[make_pair(k, r)]);
                }
            }
            if(rez==-1)
            {
                cout << "oo\n";
                continue;
            }
            cout << rez << "\n";
        }
        else
        {
            cout << "escaped\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...