Submission #639806

# Submission time Handle Problem Language Result Execution time Memory
639806 2022-09-11T18:14:55 Z milisav Valley (BOI19_valley) C++14
100 / 100
300 ms 88688 KB
        #include <bits/stdc++.h>
        using namespace std;
        #define endl '\n'
        #define int long long
         
        const int M = 3e5+5;
        int d[M], up[M][40], dp[M], mn[M][40], pr[M], ok[M], dist[M];
        vector<array<int, 3>> node[M];
        pair<int, int> v[M];
         
        void dfs(int s, int p, int dd = 0, int D = 0) {
            d[s] = D;
            dist[s] = dd;
            up[s][0] = p;
            dp[s] = (!ok[s])*LLONG_MAX;
            for (auto [i, w, x]:node[s]) {
                if (i != p) {
                    if (v[x].first != s) swap(v[x].first, v[x].second);
         
                    dfs(i, s, dd+w, D+1);
                    dp[s] = min(dp[s], dp[i]+w*(dp[i]!=LLONG_MAX));
                }
            }
         
            pr[s] = dp[s]-dd*(dp[s]!=LLONG_MAX);
        }
         
        int lca(int a, int b) {
            if (d[a] < d[b]) swap(a, b);
            for (int i = 0; i <= 20; i++) if ((d[a]-d[b])&(1<<i)) a = up[a][i];
         
            int l = 20;
            while (a != b) {
                while (l >= 0 && up[a][l] == up[b][l]) l--;
                if (l < 0) return up[a][0];
                a = up[a][l];
                b = up[b][l];
            }
         
            return a;
        }
         
        int mnn(int a, int b) {
            int ans = LLONG_MAX;
          	if(a==b) return pr[a];
            for (int i = 0; i < 30; i++) {
                if ((d[a]-d[b])&(1<<i)) {
                    ans = min(ans, mn[a][i]);
                    a = up[a][i];
                }
            } return ans;
        }
         
        signed main() {
            cin.tie(0)->sync_with_stdio(0);
         
            int n, s, q, e;
            cin >> n >> s >> q >> e;
            for (int i = 1; i < n; i++) {
                int a, b, c;
                cin >> a >> b >> c;
         
                node[a].push_back({b, c, i});
                node[b].push_back({a, c, i});
                v[i] = {a, b};
            }
         
            for (int i = 1; i <= s; i++) {
                int x; cin >> x; ok[x] = 1;
            }
         
            dfs(e, e);
            for (int i = 1; i <= n; i++) mn[i][0] = min(pr[i], pr[up[i][0]]);
            for (int j = 1; j <= 30; j++) {
                for (int i = 1; i <= n; i++) up[i][j] = up[up[i][j-1]][j-1];
                for (int i = 1; i <= n; i++) mn[i][j] = min(mn[i][j-1], mn[up[i][j-1]][j-1]);
            }
         
            while (q--) {
                int i, a;
                cin >> i >> a;
                int b = v[i].second;
         
                if (lca(a, b) != b) cout << "escaped" << endl;
                else {
                    int m = mnn(a, b);
                    if (m == LLONG_MAX)  cout << "oo" << endl;
                    else cout << m+dist[a] << endl;
                }
            }
         
            return 0;
        }

Compilation message

valley.cpp: In function 'void dfs(long long int, long long int, long long int, long long int)':
valley.cpp:16:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   16 |             for (auto [i, w, x]:node[s]) {
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7508 KB Output is correct
2 Correct 7 ms 7540 KB Output is correct
3 Correct 6 ms 7508 KB Output is correct
4 Correct 8 ms 7544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7508 KB Output is correct
2 Correct 7 ms 7540 KB Output is correct
3 Correct 6 ms 7508 KB Output is correct
4 Correct 8 ms 7544 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 5 ms 8148 KB Output is correct
7 Correct 5 ms 8148 KB Output is correct
8 Correct 5 ms 8148 KB Output is correct
9 Correct 5 ms 8148 KB Output is correct
10 Correct 5 ms 8148 KB Output is correct
11 Correct 5 ms 8148 KB Output is correct
12 Correct 5 ms 8064 KB Output is correct
13 Correct 5 ms 8148 KB Output is correct
14 Correct 6 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 222 ms 82488 KB Output is correct
2 Correct 222 ms 82424 KB Output is correct
3 Correct 244 ms 82704 KB Output is correct
4 Correct 283 ms 84992 KB Output is correct
5 Correct 251 ms 85144 KB Output is correct
6 Correct 291 ms 87756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7508 KB Output is correct
2 Correct 7 ms 7540 KB Output is correct
3 Correct 6 ms 7508 KB Output is correct
4 Correct 8 ms 7544 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 5 ms 8148 KB Output is correct
7 Correct 5 ms 8148 KB Output is correct
8 Correct 5 ms 8148 KB Output is correct
9 Correct 5 ms 8148 KB Output is correct
10 Correct 5 ms 8148 KB Output is correct
11 Correct 5 ms 8148 KB Output is correct
12 Correct 5 ms 8064 KB Output is correct
13 Correct 5 ms 8148 KB Output is correct
14 Correct 6 ms 8148 KB Output is correct
15 Correct 222 ms 82488 KB Output is correct
16 Correct 222 ms 82424 KB Output is correct
17 Correct 244 ms 82704 KB Output is correct
18 Correct 283 ms 84992 KB Output is correct
19 Correct 251 ms 85144 KB Output is correct
20 Correct 291 ms 87756 KB Output is correct
21 Correct 199 ms 81784 KB Output is correct
22 Correct 218 ms 81688 KB Output is correct
23 Correct 233 ms 82024 KB Output is correct
24 Correct 263 ms 84512 KB Output is correct
25 Correct 274 ms 88052 KB Output is correct
26 Correct 222 ms 82092 KB Output is correct
27 Correct 219 ms 81988 KB Output is correct
28 Correct 239 ms 82372 KB Output is correct
29 Correct 265 ms 84108 KB Output is correct
30 Correct 298 ms 86008 KB Output is correct
31 Correct 207 ms 82588 KB Output is correct
32 Correct 237 ms 82596 KB Output is correct
33 Correct 276 ms 82980 KB Output is correct
34 Correct 258 ms 85188 KB Output is correct
35 Correct 300 ms 88688 KB Output is correct
36 Correct 242 ms 85836 KB Output is correct