Submission #362600

# Submission time Handle Problem Language Result Execution time Memory
362600 2021-02-03T17:38:25 Z wind_reaper Roadside Advertisements (NOI17_roadsideadverts) C++17
100 / 100
79 ms 12712 KB
#include <bits/stdc++.h>

using namespace std;

vector<vector<pair<int, int64_t>>> adj;
struct LCA{
    vector<vector<int>> up; 
    int l;
    vector<int> tin, tout; 
    vector<int64_t> depth;
    LCA(int n){
        l = 32 - __builtin_clz(n);
        up.resize(l+1, vector<int>(n));
        tin.resize(n); 
        tout.resize(n);
        depth.resize(n);
        dfs(0, 0, 0);
    }
    int timer = 0; 
    void dfs(int node, int par, int64_t W){
        tin[node] = timer++; 
        depth[node] = depth[par] + W;
        up[0][node] = par;
        for(int i = 1; i <= l; i++)
            up[i][node] = up[i-1][up[i-1][node]];

        for(auto&[v, w] : adj[node]){
            if(v == par)
                continue;
            dfs(v, node, w);
        }

        tout[node] = timer++;
    }

    bool isAncestor(int u, int v){
        return tin[u] <= tin[v] && tout[u] >= tout[v];
    }

    int lca(int u, int v){
        if(isAncestor(u, v))
            return u;
        if(isAncestor(v, u))
            return v;

        for(int i = l; i >= 0; --i)
            if(!isAncestor(up[i][u], v))
                u = up[i][u];

        return up[0][u];
    }

    int64_t dist(int u, int v){
        return depth[u] + depth[v] - 2*depth[lca(u, v)];
    }
}; 
int32_t main(){
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
        
    int n;
    cin >> n; 

    adj.resize(n); 

    for(int i = 0; i < n-1; i++){
        int u, v; 
        int64_t w;
        cin >> u >> v >> w;
        adj[u].emplace_back(v, w);
        adj[v].emplace_back(u, w); 
    }

    LCA d(n);
    int Q;
    cin >> Q; 
    for(int _ = 0; _ < Q; _++){
        vector<int> q(5);
        
        for(int i = 0; i < 5; i++)
            cin >> q[i];
        
        sort(q.begin(), q.end(), [&](int a, int b){
            if(d.tin[a] < d.tin[b])
                return true;
            return false;
        });
        int64_t ans = 0;

        for(int i = 0; i < 5; i++){
            int64_t w = d.dist(d.lca(q[(i+4)%5], q[i]), q[i]);
            ans += w;
        }

        cout << ans << '\n';
    }
    return 0; 
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 9768 KB Output is correct
2 Correct 62 ms 12584 KB Output is correct
3 Correct 54 ms 12456 KB Output is correct
4 Correct 56 ms 12456 KB Output is correct
5 Correct 53 ms 12456 KB Output is correct
6 Correct 56 ms 12456 KB Output is correct
7 Correct 53 ms 12712 KB Output is correct
8 Correct 52 ms 12456 KB Output is correct
9 Correct 55 ms 12456 KB Output is correct
10 Correct 55 ms 12584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 8232 KB Output is correct
2 Correct 48 ms 11688 KB Output is correct
3 Correct 49 ms 11688 KB Output is correct
4 Correct 50 ms 11688 KB Output is correct
5 Correct 50 ms 11688 KB Output is correct
6 Correct 49 ms 11688 KB Output is correct
7 Correct 49 ms 11688 KB Output is correct
8 Correct 45 ms 11688 KB Output is correct
9 Correct 47 ms 11688 KB Output is correct
10 Correct 48 ms 11688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 52 ms 9768 KB Output is correct
3 Correct 62 ms 12584 KB Output is correct
4 Correct 54 ms 12456 KB Output is correct
5 Correct 56 ms 12456 KB Output is correct
6 Correct 53 ms 12456 KB Output is correct
7 Correct 56 ms 12456 KB Output is correct
8 Correct 53 ms 12712 KB Output is correct
9 Correct 52 ms 12456 KB Output is correct
10 Correct 55 ms 12456 KB Output is correct
11 Correct 55 ms 12584 KB Output is correct
12 Correct 41 ms 8232 KB Output is correct
13 Correct 48 ms 11688 KB Output is correct
14 Correct 49 ms 11688 KB Output is correct
15 Correct 50 ms 11688 KB Output is correct
16 Correct 50 ms 11688 KB Output is correct
17 Correct 49 ms 11688 KB Output is correct
18 Correct 49 ms 11688 KB Output is correct
19 Correct 45 ms 11688 KB Output is correct
20 Correct 47 ms 11688 KB Output is correct
21 Correct 48 ms 11688 KB Output is correct
22 Correct 55 ms 10792 KB Output is correct
23 Correct 65 ms 9272 KB Output is correct
24 Correct 79 ms 12072 KB Output is correct
25 Correct 67 ms 12072 KB Output is correct
26 Correct 70 ms 12072 KB Output is correct
27 Correct 69 ms 12072 KB Output is correct
28 Correct 70 ms 12200 KB Output is correct
29 Correct 62 ms 11944 KB Output is correct
30 Correct 61 ms 11944 KB Output is correct
31 Correct 64 ms 11944 KB Output is correct