Submission #1102368

#TimeUsernameProblemLanguageResultExecution timeMemory
1102368InvMODValley (BOI19_valley)C++14
36 / 100
3067 ms45616 KiB
#include<bits/stdc++.h>

#define fi first
#define se second
#define pb push_back

using namespace std;

using ll = long long;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a <= b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a >= b) return a = b, true; return false;}

const int N = 1e5+5;
const ll inf = 1e18+1;
const int lg = 21;



int n, q, S, Exit, timerDFS;
int h[N], par[lg][N], tin[N], tout[N];

ll val[lg][N], dist[N], near[N];
bool isShop[N], del[N];

vector<pair<int,ll>> adj[N];

vector<pair<int,ll>> id_adj[N];
pair<int,int> Edge[N];

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

void dfs(int x, int p){
    tin[x] = ++timerDFS;
    near[x] = (isShop[x] ? dist[x] : inf);

    for(pair<int,ll> e : adj[x])if(e.fi != p){
        int v = e.fi; ll w = e.se;

        h[v] = h[x] + 1;
        dist[v] = dist[x] + w;

        par[0][v] = x;
        for(int i = 1; i < lg; i++){
            par[i][v] = par[i-1][par[i-1][v]];
        }

        dfs(v, x);
        ckmn(near[x], near[v]);
    }

    tout[x] = ++timerDFS;
    return;
}

ll get(int x, int asc){
    int k = h[x] - h[asc]+1; ll res = inf;
    for(int i = 0; i < lg; i++){
        if(k>>i&1){
            ckmn(res, val[i][x]);
            x = par[i][x];
        }
    }
    return res;
}

ll BFS(int source){
    queue<int> q;
    vector<ll> d(n+1, inf);
    vector<bool> vis(n+1, false);
    d[source] = 0;
    vis[source] = true;
    q.push(source);

    ll answer = inf;

    while(!q.empty()){
        int x = q.front(); q.pop();


        if(isShop[x]){
            answer = min(answer, d[x]);
        }

        for(pair<int,int> e: id_adj[x]){
            if(del[e.fi]) continue;
            int v = Edge[e.fi].fi ^ Edge[e.fi].se ^ x;
            if(vis[v]) continue;

            d[v] = d[x] + e.se;
            vis[v] = true;
            q.push(v);
        }
    }
    return answer;
}

void solve()
{
    cin >> n >> S >> q >> Exit;
    for(int i = 1; i < n; i++){
        int u,v,w; cin >> u >> v >> w;
        adj[u].pb(make_pair(v, w));
        adj[v].pb(make_pair(u, w));
        Edge[i] = make_pair(u, v);

        id_adj[u].pb(make_pair(i, w));
        id_adj[v].pb(make_pair(i, w));
    }

    for(int i = 1; i <= S; i++){
        int v; cin >> v;
        isShop[v] = true;
    }

    dfs(1, -1);

    for(int i = 1; i <= n; i++){
        if(near[i] == inf){
            val[0][i] = inf;
        }
        else{
            val[0][i] = -2*dist[i] + near[i];
        }
    }

    for(int i = 1; i < lg; i++){
        for(int j = 1; j <= n; j++){
            val[i][j] = min(val[i-1][j], val[i-1][par[i-1][j]]);
        }
    }

    while(q--){
        int i,R; cin >> i >> R;
        if(h[Edge[i].fi] > h[Edge[i].se]) swap(Edge[i].fi, Edge[i].se);

        int v = Edge[i].se;
        if((in(v, R) && in(v, Exit)) || (!in(v, R) && !in(v, Exit))){
            cout << "escaped\n";
        }
        else{
            del[i] = true;
            ll val = BFS(R);
            if(val == inf){
                cout << "oo\n";
            }
            else cout << val <<"\n";
            del[i] = false;
        }
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "task"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".ANS", "w", stdout);
    }

    solve();
    return 0;
}

Compilation message (stderr)

valley.cpp: In function 'int32_t main()':
valley.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  163 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:164:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |         freopen(name".ANS", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...