Submission #1102348

# Submission time Handle Problem Language Result Execution time Memory
1102348 2024-10-18T03:00:56 Z InvMOD Valley (BOI19_valley) C++14
23 / 100
91 ms 42936 KB
#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];

vector<pair<int,ll>> 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;
}

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);
    }

    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{
            if(!in(v, R)){
                ll answer = get(R, 1);
                if(answer == inf){
                    cout << "oo\n";
                }
                else{
                    cout << dist[R] + answer <<"\n";
                }
            }
            else{
                ll answer = get(R, v);
                if(answer == inf){
                    cout << "oo\n";
                }
                else{
                    cout << dist[R] + answer <<"\n";
                }
            }
        }
    }
}

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".OUT", "w", stdout);
    }

    solve();
    return 0;
}

Compilation message

valley.cpp: In function 'int32_t main()':
valley.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 29264 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 29264 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 69 ms 36688 KB Output is correct
2 Correct 81 ms 36604 KB Output is correct
3 Correct 77 ms 36680 KB Output is correct
4 Correct 82 ms 38792 KB Output is correct
5 Correct 75 ms 38728 KB Output is correct
6 Correct 91 ms 42936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 29264 KB Output isn't correct
2 Halted 0 ms 0 KB -