Submission #536417

# Submission time Handle Problem Language Result Execution time Memory
536417 2022-03-13T09:16:42 Z cig32 Valley (BOI19_valley) C++17
0 / 100
3000 ms 20912 KB
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 2e5 + 10;
const int MOD = 120717661;
#define int long long
#define ll __int128
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
ll read() { // read int128
  int x; cin >> x; return (ll)x;
}
long long bm(long long b, long long p) {
  if(p==0) return 1 % MOD;
  long long r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
long long inv(long long b) { 
  return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) { 
  long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
  ans *= inv(f[n-r]); ans %= MOD; return ans;
}
void precomp() { 
  for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD);
}

vector<pair<int, int> > adj[MAXN];

int oh[MAXN];
int shop[MAXN];
int x, y; // edge removed
vector<pair<int, int> > edges;
int dep[MAXN];
int tin[MAXN], tout[MAXN];
int par[MAXN];
int wow = 0;
void dfs(int node, int prv, int cur) {
  oh[node] = 1e18;
  dep[node] = cur;
  tin[node] = ++wow;
  par[node] = prv;
  for(auto x: adj[node]) {
    if(x.first != prv) {
      dfs(x.first, node, cur + x.second);
      oh[node] = min(oh[node], oh[x.first]);
    }
  }
  tout[node] = ++wow;
  if(shop[node]) oh[node] = cur;
}
void solve(int tc) {
  int n, s, q, e;
  cin >> n >> s >> q >> e;
  for(int i=1; i<n; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    edges.push_back({a, b});
    adj[a].push_back({b, w});
    adj[b].push_back({a, w});
  }
  for(int i=0; i<s; i++) {
    int c; cin >> c;
    shop[c] = 1;
  }
  dfs(e, -1, 0);
  while(q--) {
    int i, r;
    cin >> i >> r;
    i--;
    x = edges[i].first, y = edges[i].second;
    if(tin[x] > tin[y]) swap(x, y);
    if(!(tin[y] <= tin[r] && tout[r] <= tout[y])) {
      cout << "escaped\n";
    }
    else {
      int og = r;
      int ans = 1e18;
      while(1) {
        ans = min(ans, dep[og] + oh[r] - 2 * dep[r]);
        if(r != y) r = par[r];
        else break;
      }
      cout << (ans == 1e18 ? "oo" : to_string(ans)) << "\n";
    }
  }
}


int32_t main(){
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
/*
10 10 5 4
7 2 3
4 8 3
9 10 1
6 7 3
9 2 3
10 1 2
8 2 2
5 2 1
3 8 2
1 2 3 4 5 6 7 8 9 10
2 1
1 5
8 4
6 2
7 7
*/
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 5140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 5140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 103 ms 17024 KB Output is correct
2 Correct 156 ms 16840 KB Output is correct
3 Correct 133 ms 17080 KB Output is correct
4 Correct 1439 ms 18996 KB Output is correct
5 Correct 2885 ms 19136 KB Output is correct
6 Execution timed out 3078 ms 20912 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 5140 KB Output isn't correct
2 Halted 0 ms 0 KB -