Submission #1116644

#TimeUsernameProblemLanguageResultExecution timeMemory
1116644gdragonValley (BOI19_valley)C++17
59 / 100
3036 ms19784 KiB
#include <bits/stdc++.h> using namespace std; #define TASK "long" #define fi first #define se second #define ll long long #define pb push_back #define ALL(x) (x).begin(), (x).end() #define GETBIT(mask, i) ((mask) >> (i) & 1) #define MASK(i) ((1LL) << (i)) #define SZ(x) ((int)(x).size()) #define mp make_pair #define CNTBIT(mask) __builtin_popcount(mask) template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;}; template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;}; typedef pair<int, int> ii; const int N = 1e5 + 5; const long long INF = (long long)1e18 + 7; const int mod = 1e9 + 7; struct Fenwick { int n; vector<long long> v; Fenwick(int _n = 0) { n = _n; v.assign(n + 2, 0); } void update(int x, int c) { for(; x <= n; x += x & -x) v[x] += c; } long long get(int x) { long long ans = 0; for(; x > 0; x -= x & -x) ans += v[x]; return ans; } long long get(int l, int r) { return (get(r) - get(l - 1)); } } fw, weight; int n, q, numShop, root; int tin[N], tout[N], high[N], head[N], sz[N], par[N]; pair<int, int> e[N]; vector<pair<int, int>> adj[N]; vector<int> shops; void read() { cin >> n >> numShop >> q >> root; for(int i = 1; i < n; i++) { int u, v, c; cin >> u >> v >> c; adj[u].push_back({v, c}); adj[v].push_back({u, c}); e[i] = {u, v}; } shops.resize(numShop); for(auto&i : shops) cin >> i; } int timer = 0; void dfs(int u, int p) { sz[u] = 1; par[u] = p; for(auto& i: adj[u]) { int v = i.fi; if (v == p) continue; high[v] = high[u] + 1; dfs(v, u); sz[u] += sz[v]; } } bool isChild(int u, int p) { return tin[u] >= tin[p] && tin[u] <= tout[p]; } void hld(int u, int p, int top, int w) { tin[u] = ++timer; head[u] = top; // cerr << u << ' ' << head[u] << endl; if (p != -1) weight.update(tin[u], w); int bigC = -1, bigW = -1; for(auto &i: adj[u]) { int v = i.fi; if (v == p) continue; if (bigC == -1 || sz[bigC] < sz[v]) { bigC = v; bigW = i.se; } } if (bigC == -1) { tout[u] = timer; return; } hld(bigC, u, top, bigW); for(auto &i: adj[u]) { int v = i.fi; if (v == p || v == bigC) continue; hld(v, u, v, i.se); } tout[u] = timer; } long long get(int u, int v) { // if (high[u] < high[v]) swap(u, v); long long ans = 0; // cerr << "DB: " << u << ' ' << v << endl; while(head[u] != head[v]) { if (high[head[u]] < high[head[v]]) swap(u, v); ans += weight.get(tin[head[u]], tin[u]); // cerr << head[u] << ' ' << u << endl; u = par[head[u]]; } if (high[u] > high[v]) swap(u, v); ans += weight.get(tin[u] + 1, tin[v]); return ans; } void sub1() { fw = Fenwick(n); weight = Fenwick(n); dfs(root, -1); hld(root, -1, root, 0); for(int shop: shops) fw.update(tin[shop], 1); while(q--) { int road, node; cin >> road >> node; int u = e[road].fi, v = e[road].se; if (high[u] > high[v]) swap(u, v); if (isChild(node, v)) { if (fw.get(tin[v], tout[v]) == 0) { cout << "oo\n"; continue; } if (numShop == n) { cout << 0 << endl; continue; } long long ans = INF; for(int shop: shops) if (isChild(shop, v)) { ans = min(ans, get(shop, node)); } cout << ans << endl; } else cout << "escaped\n"; } } void solve() { sub1(); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); } int test = 1; // cin >> test; while(test--) { read(); solve(); } return 0; }

Compilation message (stderr)

valley.cpp: In function 'int main()':
valley.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |         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...