Submission #987657

#TimeUsernameProblemLanguageResultExecution timeMemory
987657NoLoveValley (BOI19_valley)C++14
23 / 100
203 ms69752 KiB
/** * author : Lăng Trọng Đạt * created: 23-05-2024 **/ #include <bits/stdc++.h> using namespace std; #ifndef LANG_DAT #define db(...) ; #endif // LANG_DAT #define int long long #define mp make_pair #define f first #define se second #define pb push_back #define all(v) (v).begin(), (v).end() using pii = pair<int, int>; using vi = vector<int>; #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++) void mx(int& a, int b) { if (b > a) a = b; } void mi(int& a, int b) { if (b < a) a = b; } #define si(x) (int)(x.size()) const int INF = 1e18; const int MOD = 1e9 + 7; const int MAXN = 1e5 + 5; int g[MAXN]; int n, s, q, root, r; vector<pii> adj[MAXN]; pii up[20][MAXN]; bool is_shop[MAXN]; int far[MAXN], lca[2*MAXN]; int get(int x) { return (far[x] == 0 ? x : far[x] = get(far[x])); } vector<pair<int, int>> query_lcas[MAXN]; int l[MAXN]; // shortest length from node u to shop which is in subtree of u int d[MAXN]; // length of path from root to node u int depth[MAXN]; void dfs(int v, int prv, int steps, int dist) { d[v] = dist; depth[v] = steps; if (is_shop[v]) l[v] = 0; for (auto[u, w] : adj[v]) { if (u == prv) continue; dfs(u, v, steps + 1, dist + w); mi(l[v], l[u] + w); far[u] = v; up[0][u].f = v; if (is_shop[u]) up[0][u].se = u; } for (auto[u, id] : query_lcas[v]) { if (depth[u]) { lca[id] = get(u); } } } int32_t main() { cin.tie(0)->sync_with_stdio(0); if (fopen("hi.inp", "r")) { freopen("hi.inp", "r", stdin); // freopen("hi.out", "w", stdout); } // input cin >> n >> s >> q >> root; int a, b, w; vector<pii> edges; FOR(i, 1, n - 1) { cin >> a >> b >> w; adj[a].pb({b, w}); adj[b].pb({a, w}); edges.pb({a, b}); } FOR(i, 1, s) { cin >> a; is_shop[a] = 1; } vector<pii> Q(q + 1); FOR(i, 1, q) { cin >> Q[i].f >> Q[i].se; int r = Q[i].se; auto[a, b] = edges[Q[i].f - 1]; query_lcas[r].pb({a, i}); query_lcas[a].pb({r, i}); query_lcas[r].pb({b, q + i}); query_lcas[b].pb({r, q + i}); } // preprocess memset(l, 0x3f, sizeof(l)); dfs(root, -1, 1, 0); FOR(i, 1, 19) { FOR(v, 1, n) { up[i][v].f = up[i - 1][up[i - 1][v].f].f; if (up[i - 1][v].se) up[i][v].se = up[i - 1][v].se; else if (up[i - 1][up[i - 1][v].f].se) up[i][v].se = up[i - 1][up[i - 1][v].f].se; } } // answer query FOR(i, 1, q) { auto[a, b] = edges[Q[i].f - 1]; if (depth[a] < depth[b] && lca[i + q] != b) cout << "escaped\n"; else if (depth[a] > depth[b] && lca[i] != a) cout << "escaped\n"; else { int tren = a, r = Q[i].se; if (depth[a] < depth[b]) tren = b; if (l[tren] > INF) cout << "oo\n"; else { db(tren, d[tren], d[r]) int ans = min(l[tren] + d[r] - d[tren], l[r]); int jump = depth[r] - depth[tren]; int shop_in_path = -1; int tmp = r; FOR(i, 0, 19) { if ((1 << i) && jump) { if (up[i][tmp].se != 0) { shop_in_path = up[i][tmp].se; } tmp = up[i][tmp].f; } if (shop_in_path != -1) { mi(ans, d[r] - d[shop_in_path]); break; } } cout << ans << "\n"; } } } }

Compilation message (stderr)

valley.cpp: In function 'void dfs(long long int, long long int, long long int, long long int)':
valley.cpp:43:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   43 |     for (auto[u, w] : adj[v]) {
      |              ^
valley.cpp:52:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   52 |     for (auto[u, id] : query_lcas[v]) {
      |              ^
valley.cpp: In function 'int32_t main()':
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:71:5: note: in expansion of macro 'FOR'
   71 |     FOR(i, 1, n - 1) {
      |     ^~~
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:77:5: note: in expansion of macro 'FOR'
   77 |     FOR(i, 1, s) {
      |     ^~~
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:82:5: note: in expansion of macro 'FOR'
   82 |     FOR(i, 1, q) {
      |     ^~~
valley.cpp:85:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   85 |         auto[a, b] = edges[Q[i].f - 1];
      |             ^
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:96:5: note: in expansion of macro 'FOR'
   96 |     FOR(i, 1, 19) {
      |     ^~~
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:97:9: note: in expansion of macro 'FOR'
   97 |         FOR(v, 1, n) {
      |         ^~~
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:106:5: note: in expansion of macro 'FOR'
  106 |     FOR(i, 1, q) {
      |     ^~~
valley.cpp:107:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  107 |         auto[a, b] = edges[Q[i].f - 1];
      |             ^
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:120:17: note: in expansion of macro 'FOR'
  120 |                 FOR(i, 0, 19) {
      |                 ^~~
valley.cpp:121:28: warning: '<<' in boolean context, did you mean '<'? [-Wint-in-bool-context]
  121 |                     if ((1 << i) && jump) {
      |                         ~~~^~~~~
valley.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         freopen("hi.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...