Submission #717518

# Submission time Handle Problem Language Result Execution time Memory
717518 2023-04-02T04:12:13 Z boyliguanhan Valley (BOI19_valley) C++17
100 / 100
175 ms 39628 KB
#include <bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define ll __uint128_t
#define V(x) vector<x>
#define G(x) greater<x>
#define MS(x) multiset<x>
#define BG(x) begin(x)
#define All(x) BG(x), end(x)
#define Q(x) queue<x>
#define PQ(x) priority_queue<x, V(x), G(x)>
#define PQG(x) priority_queue<x, V(x), less<x>>
#define OST(x, y) tree<x, null_type, y, rb_tree_tag,tree_order_statistics_node_update>
#define Lb lower_bound
#define Ub upper_bound
#define F first
#define S second
#define T0 get<0>
#define T1 get<1>
#define T2 get<2>
#define P(x) pair<x,x>
#define IO cin.sync_with_stdio(false); cin.tie(nullptr);
#define For(i,a,b,c) for(int i = a; i != b; i+=c)
#define lsb(x) (x&(-x))
#define PB push_back
#define Rm1(a,b) (a.erase(a.find(b)))
#define ER erase
#define Add insert
#define AE(a,b,c) a[b].PB(c)
#define AWE(a,b,c,d) a[b].PB({c,d})
#define AUE(a,b,c) AE(a,b,c), AE(a,c,b)
#define AUWE(a,b,c,d) AWE(a,b,c,d), AWE(a,c,b,d)
#define MOD 1000000007LL
#define HMOD 9999999992999999999ULL
#define FIO(a) freopen((string(a) + ".in").c_str(), "r", stdin), freopen((string(a)+".out").c_str(), "w", stdout)
#define N 100100
V(P(int)) adj[N];
P(int) roads[N];
int hc[N], sz[N], dep[N], val[N], top[N], pos[N], arr[N][20], proc, p[N];
bool shop[N];
void dfs(int n) {
    sz[n] = 1;
    if(!shop[n])
        val[n] = 1e18;
    for(auto i: adj[n]) {
        if(i.F!=p[n]) {
            p[i.F] = n;
            dep[i.F] = dep[n]+i.S;
            dfs(i.F);
            if(sz[hc[n]] < sz[i.F])
                hc[n] = i.F;
            sz[n]+=sz[i.F];
            val[n] = min(val[n], val[i.F]+i.S);
        }
    }
}
void dfs2(int n) {
    if(n!=hc[p[n]])
        top[n] = n;
    else
        top[n] = top[p[n]];
    pos[n] = ++proc;
    val[n]-=dep[n];
    arr[pos[n]][0] = val[n];
    if(hc[n])
        dfs2(hc[n]);
    for(auto i: adj[n]) {
        if(i.F!=p[n]&&i.F!=hc[n]) {
            dfs2(i.F);
        }
    }
}
int query(int l, int r){
    if(l > r)
        swap(l, r);
    int x = 31-__builtin_clz(r-l+1);
    return min(arr[l][x], arr[r-(1<<x)+1][x]);
}
signed main() {
    IO;
    int n, s, q, e;
    cin >> n >> s >> q >> e;
    For(i, 1, n, 1){
        int a, b,c;
        cin >> a >> b >> c;
        AUWE(adj,a,b,c);
        roads[i] = {a, b};
    }
    For(i,1,s+1,1){
        int x;
        cin >> x;
        shop[x]=1;
    }
    dep[e] = 1;
    dfs(e);
    dfs2(e);
    for(int j = 1; j < 20; j++)
        for(int i = 1; i < n - (1 << j) + 2; i++)
            arr[i][j] = min(arr[i][j-1], arr[i+(1<<(j-1))][j-1]);
    for(int i = 0; i < q; i++){
        int x, y;
        cin >> y >> x;
        if(dep[roads[y].F] > dep[roads[y].S])
            y = roads[y].F;
        else
            y = roads[y].S;
        int temp = x;
        while(dep[p[top[temp]]] >= dep[y]) {
            temp = p[top[temp]];
        }
        if(top[temp]!=top[y]||dep[x] < dep[y]) {
            cout << "escaped\n";
        } else {
            int ans = 1e18, add = dep[x];
            while(x!=temp) {
                ans = min(ans, query(pos[top[x]], pos[x]));
                x = p[top[x]];
            }
            ans = min(ans, query(pos[x], pos[y]));
            if(ans > 1e15) {
                cout << "oo\n";
            } else {
                cout << ans + add << '\n';
            }
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2772 KB Output is correct
2 Correct 4 ms 2900 KB Output is correct
3 Correct 4 ms 2900 KB Output is correct
4 Correct 4 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2772 KB Output is correct
2 Correct 4 ms 2900 KB Output is correct
3 Correct 4 ms 2900 KB Output is correct
4 Correct 4 ms 2900 KB Output is correct
5 Correct 2 ms 3028 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 2 ms 3028 KB Output is correct
8 Correct 2 ms 3028 KB Output is correct
9 Correct 2 ms 3028 KB Output is correct
10 Correct 2 ms 2944 KB Output is correct
11 Correct 2 ms 2948 KB Output is correct
12 Correct 2 ms 3028 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 35020 KB Output is correct
2 Correct 149 ms 34948 KB Output is correct
3 Correct 150 ms 35164 KB Output is correct
4 Correct 158 ms 37000 KB Output is correct
5 Correct 131 ms 37136 KB Output is correct
6 Correct 136 ms 39188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2772 KB Output is correct
2 Correct 4 ms 2900 KB Output is correct
3 Correct 4 ms 2900 KB Output is correct
4 Correct 4 ms 2900 KB Output is correct
5 Correct 2 ms 3028 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 2 ms 3028 KB Output is correct
8 Correct 2 ms 3028 KB Output is correct
9 Correct 2 ms 3028 KB Output is correct
10 Correct 2 ms 2944 KB Output is correct
11 Correct 2 ms 2948 KB Output is correct
12 Correct 2 ms 3028 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
15 Correct 132 ms 35020 KB Output is correct
16 Correct 149 ms 34948 KB Output is correct
17 Correct 150 ms 35164 KB Output is correct
18 Correct 158 ms 37000 KB Output is correct
19 Correct 131 ms 37136 KB Output is correct
20 Correct 136 ms 39188 KB Output is correct
21 Correct 121 ms 34420 KB Output is correct
22 Correct 122 ms 34272 KB Output is correct
23 Correct 140 ms 34524 KB Output is correct
24 Correct 145 ms 36580 KB Output is correct
25 Correct 133 ms 39628 KB Output is correct
26 Correct 119 ms 34520 KB Output is correct
27 Correct 132 ms 34368 KB Output is correct
28 Correct 145 ms 34624 KB Output is correct
29 Correct 175 ms 36052 KB Output is correct
30 Correct 155 ms 37772 KB Output is correct
31 Correct 117 ms 34664 KB Output is correct
32 Correct 154 ms 34500 KB Output is correct
33 Correct 158 ms 34812 KB Output is correct
34 Correct 156 ms 36628 KB Output is correct
35 Correct 171 ms 39580 KB Output is correct
36 Correct 139 ms 36628 KB Output is correct