Submission #1094783

#TimeUsernameProblemLanguageResultExecution timeMemory
1094783natus26Valley (BOI19_valley)C++17
100 / 100
108 ms37204 KiB
#include <bits/stdc++.h> #define task "1" #define ll long long #define double long double #define ii pair<int, int> #define li pair<ll, int> #define fi first #define se second #define all(v) (v).begin(), (v).end() #define FULL(i) ((1LL << i) - 1) #define MASK(i) (1LL << i) #define c_bit(i) __builtin_popcountll(i) #define Bit(mask, i) ((mask >> i) & 1) #define onbit(mask, i) ((mask) bitor (1LL << i)) #define offbit(mask, i) ((mask) &~ (1LL << i)) using namespace std; const int maxn = 1e5 + 5; const ll oo = 1e18; const int mod = 1e9 + 7; const int dx[] = {0, 1, 0, -1} , dy[] = {1, 0, -1, 0}; template <class X, class Y> bool maximize(X &a, const Y &b) { if(a < b) return a = b, true; return false; } template <class X, class Y> bool minimize(X &a, const Y &b) { if(a > b) return a = b, true; return false; } int n, s, q, h, head[maxn], pos[maxn], par[maxn], sz[maxn], in[maxn], out[maxn], depth[maxn]; vector<ii> adj[maxn]; bool mark[maxn]; ll d[maxn], dp[maxn], mi[maxn][20]; struct Edge{ int u, v, w; } edges[maxn]; void dfs(int u, int p) { static int timeDfs = 0; in[u] = ++ timeDfs; dp[u] = mark[u] ? 0 : oo; sz[u] = 1; int iMax = -1; for(int i = 0; i < adj[u].size(); ++ i){ auto [v, w] = adj[u][i]; if(v == p) continue; par[v] = u; d[v] = d[u] + w; depth[v] = depth[u] + 1; dfs(v, u); minimize(dp[u], dp[v] + w); sz[u] += sz[v]; if(!~iMax || sz[adj[u][iMax].fi] < sz[v]) iMax = i; } out[u] = timeDfs; if(~iMax) swap(adj[u][0], adj[u][iMax]); } void hld(int u) { static int id = 0; pos[u] = ++ id; for(auto [v, _] : adj[u]) if(par[v] == u){ if(sz[v] << 1 >= sz[u]) head[v] = head[u]; else head[v] = v; hld(v); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } cin >> n >> s >> q >> h; for(int i = 1; i < n; ++ i){ int u, v, w; cin >> u >> v >> w; edges[i] = {u, v, w}; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } for(int i = 1; i <= s; ++ i){ int u; cin >> u; mark[u] = true; } dfs(h, 0); hld(h); auto inSubTree = [&](int v, int u){ return in[u] <= in[v] && in[v] <= out[u]; }; for(int u = 1; u <= n; ++ u) mi[pos[u]][0] = dp[u] - d[u]; for(int k = 1; 1 << k <= n; ++ k) for(int i = 1; i + (1 << k) - 1 <= n; ++ i) mi[i][k] = min(mi[i][k - 1], mi[i + (1 << k - 1)][k - 1]); auto get = [&](int l, int r){ int h = __lg(r - l + 1); return min(mi[l][h], mi[r - (1 << h) + 1][h]); }; while(q --){ int e, x; cin >> e >> x; auto [u, v, _] = edges[e]; if(depth[u] < depth[v]) swap(u, v); if(!inSubTree(x, u)) { cout << "escaped\n"; continue; } ll res = oo; ll d_x = d[x]; while(head[x] != head[u]){ minimize(res, get(pos[head[x]], pos[x]) + d_x); x = par[head[x]]; } minimize(res, get(pos[u], pos[x]) + d_x); if(res == oo) cout << "oo\n"; else cout << res << '\n'; } cerr << LONG_LONG_MAX; return 0; }

Compilation message (stderr)

valley.cpp: In function 'void dfs(int, int)':
valley.cpp:48:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |     for(int i = 0; i < adj[u].size(); ++ i){
      |                    ~~^~~~~~~~~~~~~~~
valley.cpp: In function 'int main()':
valley.cpp:102:53: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  102 |         mi[i][k] = min(mi[i][k - 1], mi[i + (1 << k - 1)][k - 1]);
      |                                                   ~~^~~
valley.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         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...