Submission #886817

#TimeUsernameProblemLanguageResultExecution timeMemory
886817Zero_OPValley (BOI19_valley)C++14
23 / 100
131 ms37276 KiB
#include<bits/stdc++.h>

using namespace std;

#define range(v) begin(v), end(v)
#define compact(v) v.erase(unique(range(v)), end(v))

template<class T> bool minimize(T& a, T b){
  if(a > b) return a = b, true;
  return false;
}

template<class T> bool maximize(T& a, T b){
  if(a < b) return a = b, true;
  return false;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int N = 1e5 + 5;
const ll inf = 1e17;

int n, s, q, root, lift[20][N], h[N], tin[N], tout[N], timerDFS;
ll depth[N], dp[N], rmq[20][N];
array<int, 3> e[N];
vector<pair<int, int>> g[N];

void dfsLCA(int u, int e){
  tin[u] = ++timerDFS;
  for(auto [v, w] : g[u]) if(v != e){
    h[v] = h[u] + 1;
    depth[v] = depth[u] + w;
    lift[0][v] = u;
    for(int i = 1; i <= 17; ++i){
      lift[i][v] = lift[i - 1][lift[i - 1][v]];
    }
    dfsLCA(v, u);
    minimize(dp[u], dp[v] + w);
  }
  tout[u] = timerDFS;
}

void dfsLift(int u, int e){
  for(auto [v, w] : g[u]) if(v != e){
    rmq[0][v] = -depth[u] + dp[u];
    for(int i = 1; i <= 17; ++i){
      rmq[i][v] = min(rmq[i - 1][v], rmq[i - 1][lift[i - 1][v]]);
    }
    dfsLift(v, u);
  }
}

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

ll upMin(int u, int d){
  ll ans = inf;
  for(int i = 17; i >= 0; --i){
    if(d >= (1 << i)){
      d -= (1 << i);
      minimize(ans, rmq[i][u]);
      u = lift[i][u];
    }
  }
  return ans;
}

void Zero_OP(){
  cin >> n >> s >> q >> root;
  for(int i = 1; i < n; ++i){
    int u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
    e[i] = {u, v, w};
  }

  fill(dp + 1, dp + 1 + n, inf);
  for(int i = 1; i <= s; ++i){
    int x; cin >> x;
    dp[x] = 0;
  }

  dfsLCA(root, -1);
  memset(rmq, -0x3f, sizeof(rmq));
  dfsLift(root, -1);
  for(int i = 1; i < n; ++i){
    if(h[e[i][0]] > h[e[i][1]]) swap(e[i][0], e[i][1]);
  }

  while(q--){
    int i, u;
    cin >> i >> u;

    int cur = e[i][1];
    //cout << cur << ' ' << u << '\n';
    if(!inSubtree(cur, u)) cout << "escaped\n";
    else if(dp[cur] == inf) cout << "oo";
    else cout << min(dp[u], depth[u] + upMin(u, h[u] - h[cur])) << '\n';
  }
}

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

  #define task "antuvu"
  if(fopen(task".inp", "r")){
    freopen(task".inp", "r", stdin);
    freopen(task".out", "w", stdout);
  }

  Zero_OP();

  return 0;
}

Compilation message (stderr)

valley.cpp: In function 'void dfsLCA(int, int)':
valley.cpp:32:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   32 |   for(auto [v, w] : g[u]) if(v != e){
      |            ^
valley.cpp: In function 'void dfsLift(int, int)':
valley.cpp:46:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   46 |   for(auto [v, w] : g[u]) if(v != e){
      |            ^
valley.cpp: In function 'int main()':
valley.cpp:112:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |     freopen(task".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:113:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |     freopen(task".out", "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...