Submission #764783

# Submission time Handle Problem Language Result Execution time Memory
764783 2023-06-24T04:22:46 Z vjudge1 Roadside Advertisements (NOI17_roadsideadverts) C++17
100 / 100
49 ms 12796 KB
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <bitset>
#include <cmath>
#include <queue>
#include <map>
#include <set>

// Akhmet Issa

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"

const int maxn = 5e4 + 100;
const ll INF = (ll)2e18;
const int inf = (ll)2e9;
const int maxl = 20;
const int MOD = 1e9 + 7;

int n, t, q;
vector<pii> g[maxn];
int p[maxl][maxn];
int sum[maxn];
int dep[maxn];
int tin[maxn];
int tout[maxn];

void dfs(int v){
    tin[v] = ++t;
    for(int k = 1; k < maxl; k++){
        p[k][v] = p[k-1][p[k-1][v]];
    }
    for(auto [to, w]: g[v]){
        if(to == p[0][v]) continue;
        p[0][to] = v;
        sum[to] = sum[v] + w;
        dep[to] = dep[v] + 1;
        dfs(to);
    }
    tout[v] = t;
}

bool ok(int a, int b){
    return tin[a] <= tin[b] && tout[b] <= tout[a];
}

int get(int a, int b){
    if(ok(a, b)) return a;
    if(ok(b, a)) return b;
    for(int k = maxl - 1; k >= 0; k--){
        if(p[k][a] && !ok(p[k][a], b)) a = p[k][a];
    }
    return p[0][a];
}

void test(){
    cin >> n;
    for(int i = 1; i < n; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a++; b++;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }
    dfs(1);
    cin >> q;
    for(int t = 1; t <= q; t++){
        int a[5];
        int r = -1;
        for(int i = 0; i < 5; i++){
            cin >> a[i]; a[i]++;
            if(r == -1) r = a[i];
            else r = get(r, a[i]);
        }
        ll ans = 0;
        for(int i = 0; i < 5; i++){
            int r1 = r;
            for(int j = 0; j < i; j++){
                int r2 = get(a[i], a[j]);
                if(dep[r1] < dep[r2]) r1 = r2;
            }
            ans += sum[a[i]] - sum[r1];
        }
        cout << ans << ent;
    }
}

int main(){
    // freopen("cows.in", "r", stdin);
    // freopen("cows.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    while(t--) test();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 9816 KB Output is correct
2 Correct 29 ms 12792 KB Output is correct
3 Correct 29 ms 12748 KB Output is correct
4 Correct 29 ms 12748 KB Output is correct
5 Correct 29 ms 12748 KB Output is correct
6 Correct 29 ms 12716 KB Output is correct
7 Correct 32 ms 12776 KB Output is correct
8 Correct 31 ms 12796 KB Output is correct
9 Correct 30 ms 12748 KB Output is correct
10 Correct 29 ms 12748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 8080 KB Output is correct
2 Correct 28 ms 11736 KB Output is correct
3 Correct 34 ms 11908 KB Output is correct
4 Correct 24 ms 11756 KB Output is correct
5 Correct 24 ms 11744 KB Output is correct
6 Correct 24 ms 11724 KB Output is correct
7 Correct 24 ms 11732 KB Output is correct
8 Correct 27 ms 11696 KB Output is correct
9 Correct 24 ms 11680 KB Output is correct
10 Correct 24 ms 11760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 37 ms 9816 KB Output is correct
3 Correct 29 ms 12792 KB Output is correct
4 Correct 29 ms 12748 KB Output is correct
5 Correct 29 ms 12748 KB Output is correct
6 Correct 29 ms 12748 KB Output is correct
7 Correct 29 ms 12716 KB Output is correct
8 Correct 32 ms 12776 KB Output is correct
9 Correct 31 ms 12796 KB Output is correct
10 Correct 30 ms 12748 KB Output is correct
11 Correct 29 ms 12748 KB Output is correct
12 Correct 20 ms 8080 KB Output is correct
13 Correct 28 ms 11736 KB Output is correct
14 Correct 34 ms 11908 KB Output is correct
15 Correct 24 ms 11756 KB Output is correct
16 Correct 24 ms 11744 KB Output is correct
17 Correct 24 ms 11724 KB Output is correct
18 Correct 24 ms 11732 KB Output is correct
19 Correct 27 ms 11696 KB Output is correct
20 Correct 24 ms 11680 KB Output is correct
21 Correct 24 ms 11760 KB Output is correct
22 Correct 36 ms 10836 KB Output is correct
23 Correct 30 ms 9064 KB Output is correct
24 Correct 39 ms 12140 KB Output is correct
25 Correct 46 ms 12044 KB Output is correct
26 Correct 37 ms 12108 KB Output is correct
27 Correct 41 ms 12156 KB Output is correct
28 Correct 36 ms 12140 KB Output is correct
29 Correct 37 ms 12088 KB Output is correct
30 Correct 37 ms 12128 KB Output is correct
31 Correct 49 ms 12108 KB Output is correct