Submission #886823

#TimeUsernameProblemLanguageResultExecution timeMemory
886823Zero_OPValley (BOI19_valley)C++14
23 / 100
146 ms37464 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, q, s, root, h[N],  lift[20][N], a[N], b[N], tin[N], tout[N], sz[N], timerDFS;
vector<pair<int, int>> g[N];
ll d[N], dp[N], rmq[20][N];

void dfsLCA(int u, int e){
  tin[u] = ++timerDFS;
  for(auto [v, w] : g[u]) if(v != e){
    h[v] = h[u] + 1;
    d[v] = d[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);
    sz[u] += sz[v];
  }
  tout[u] = timerDFS;
}

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

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

ll getLift(int u, int d){
  ll ans = inf;
  for(int i = 17; i >= 0; --i){
    if(d >> i & 1){
      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, c;
    cin >> u >> v >> c;
    g[u].push_back({v, c});
    g[v].push_back({u, c});
    a[i] = u, b[i] = v;
  }

  memset(dp, 0x3f, sizeof(dp));
  memset(rmq, 0x3f, sizeof(rmq));

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

  dfsLCA(root, -1);
  dfsRMQ(root, -1);

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

    int u = (h[a[i]] < h[b[i]] ? b[i] : a[i]);
    if(!inSubtree(u, v)) cout << "escaped\n";
    else if(sz[u] == 0) cout << "oo";
    else cout << min(dp[v], d[v] + getLift(v, h[v] - h[u])) << '\n';
  }
}

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

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

  Zero_OP();

  return 0;
}

Compilation message (stderr)

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