Submission #1157496

#TimeUsernameProblemLanguageResultExecution timeMemory
1157496DaciejMaciejValley (BOI19_valley)C++20
23 / 100
206 ms58128 KiB
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/rope> using namespace std; // using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; typedef long double ld; template <class T> using VV = vector<vector<T>>; using VI = vector<int>; using VVI = vector<vector<int>>; using VL = vector<long long>; using VVL = vector<vector<long long>>; using VC = vector<char>; using VVC = vector<vector<char>>; using VB = vector<bool>; using VVB = vector<vector<bool>>; using VD = vector<double>; using VVD = vector<vector<double>>; using PII = pair<int, int>; using PLL = pair<long long, long long>; using PIL = pair<int, long long>; using PLI = pair<long long, int>; using VPII = vector<pair<int, int>>; using VPLL = vector<pair<long long, long long>>; #define LINE '\n' #define SPACE ' ' #define PB push_back #define FOR(i, a, b) for (int i = (a); i < (int(b)); i++) #define FORE(i, a, b) for (int i = (a); i <= (int)((b)); i++) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() #define sq(a) ((a) * (a)) #define sz(x) ((int)x.size()) #define fastio() \ ios_base::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr) #define debug(x) cerr << #x << " = " << x << endl; const ll MOD = 1e9 + 7; template <class T> inline void maxi(T &a, T b) { a = max(a, b); } template <class T> inline void mini(T &a, T b) { a = min(a, b); } template <class T> inline void addi(T &a, T b) { a = (a + b) % MOD; } template <class T> inline void subi(T &a, T b) { a = (a - b + MOD) % MOD; } template <class T> inline T add(T a, T b) { return (a + b) % MOD; } template <class T> inline T sub(T a, T b) { return (a - b + MOD) % MOD; } template <class T> inline T mul(T a, T b) { return ((a % MOD) * (b % MOD)) % MOD; } constexpr ll binpow(ll a, ll b, ll mod) { ll res = 1; while (b > 0) { if (b & 1) { res = (res * a) % mod; } a = (a * a) % mod; b >>= 1; } return res; } const ll INF = 1e14; const int MAX_N = 1e5 + 3; vector<pair<int, ll>> g[MAX_N]; VB shop(MAX_N, false); VL dpdown(MAX_N, INF); VI in(MAX_N), out(MAX_N); const int d = 22; VVL dp(MAX_N, VL(d + 1, INF)); VVL up(MAX_N, VL(d + 1, INF)); VL sm(MAX_N, 0); int t = 0; void dfs(int c, int p) { if (shop[c]) dpdown[c] = 0; in[c] = ++t; for (auto [v, cost] : g[c]) { if (v == p) continue; sm[v] = sm[c] + cost; dfs(v, c); mini(dpdown[c], dpdown[v] + cost); } out[c] = ++t; } bool isanc(int a, int b) { return in[a] <= in[b] && out[a] >= out[b]; } void dlift(int c, int p) { dp[c][0] = min(dpdown[c], (sm[c] - sm[p]) + dpdown[p]); // edge c-p up[c][0] = p; FOR(i, 1, d + 1) { int u = up[c][i - 1]; up[c][i] = up[u][i - 1]; ll fr = dp[c][i - 1]; ll cost = sm[c] - sm[u]; // koszt na c-up[c][i-1] ll sc = dp[u][i - 1] + cost; dp[c][i] = min(fr, sc); } for (auto [v, _] : g[c]) { if (v == p) continue; dlift(v, c); } } void query(int road_bottom, int orig) { int c = orig; if (!isanc(road_bottom, c)) { cout << "escaped" << LINE; return; } if(c == road_bottom) { ll ans = dpdown[c]; cout << (ans >= INF ? "oo" : to_string(ans)) << LINE; return; } ll ans = dpdown[c]; for (int i = d; i >= 0; i--) { if (isanc(up[c][i], road_bottom)) continue; ll nw = dp[c][i] + sm[orig] - sm[up[c][i]]; mini(ans, nw); c = up[c][i]; } mini(ans, (sm[orig] - sm[road_bottom]) + dpdown[road_bottom]); if(ans >= INF) { cout << "oo" << LINE; } else { cout << ans << LINE; } } void solve() { int n, s, q, e; cin >> n >> s >> q >> e; VPII edges(n - 1); FOR(i, 0, n - 1) { int a, b; ll c; cin >> a >> b >> c; g[a].PB({b, c}); g[b].PB({a, c}); edges[i] = {a, b}; } FOR(i, 0, s) { int c; cin >> c; shop[c] = true; } dfs(e, e); dlift(e, e); for (auto &[a, b] : edges) { if (!isanc(a, b)) swap(a, b); // a must be anc of b } while (q--) { int id, c; cin >> id >> c; query(edges[id-1].second, c); } } int main() { fastio(); int t = 1; // cin >> t; while (t--) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...