Submission #765098

# Submission time Handle Problem Language Result Execution time Memory
765098 2023-06-24T08:23:13 Z vjudge1 Roadside Advertisements (NOI17_roadsideadverts) C++17
100 / 100
100 ms 23860 KB
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define ent '\n'
#define all(x) x.begin(), x.end()
#define s second
#define f first
#define pb push_back
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 250;
const int mod = 1e9 + 7;
const int K = 400;
const double Pi = acos(-1.0);
const long double eps = 1e-9;
#define int long long
int a[maxn], pref[maxn];
vector<pair<int,int>>g[maxn];
int tin[maxn], tout[maxn], lev[maxn];
int p[30][maxn];
int timer;
void dfs(int v, int pa = 1) {
    lev[v] = lev[pa]+1;
    tin[v] = timer++;
    p[0][v] = pa;
    for(int i = 1;i < 30; i++) {
        p[i][v] = p[i-1][p[i-1][v]];
    }
    for(auto to:g[v]) {
        if(to.f==pa)continue;
            pref[to.f] = pref[v]+to.s;
            dfs(to.f, v);
    }
    tout[v] = timer-1;
}

bool parent(long long u, long long v) {
    if(tin[u] <= tin[v] && tout[u] >= tout[v]) {
        return 1;
    }
    return 0;
}

int lca(long long u, long long v) {
    if(lev[u] < lev[v]) {
        swap(u, v);
    }
    for(long long i = 25; i >= 0; i--) {
        if(lev[u] - (1<<i) >= lev[v]) {
            u = p[i][u];
        }
    }
    if(u == v) return u;
    for(long long i = 25; i >= 0; i--) {
        if(p[i][u] != p[i][v]) {
            u = p[i][u];
            v = p[i][v];
        }
    }
    return p[0][v];
}
void solve() {
    int n, k;
    cin >> n;
    for(int i = 1;i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u++,v++;
        g[u].pb({v, w});
        g[v].pb({u, w});
    }
    dfs(1);
    cin >> k;
    for(int i = 1;i <= k; i++) {
        int sum = 0;
        for(int j = 1;j <= 5; j++) {
            cin >> a[j];
            a[j]++;
        }
        int x = a[1];
        for(int j = 2;j <= 5; j++) {
            x = lca(x, a[j]);
        }
        for(int j = 1;j <= 5; j++) {
            sum += pref[a[j]]-pref[x];
        }
        for(int j = 1;j <= 5; j++) {
            int mx = 0;
            for(int l = j+1;l <= 5; l++) {
                mx = max(mx, pref[lca(a[j], a[l])]-pref[x]);
            }
            sum -= mx;
        }
        cout << sum << ent;
    }
}
signed main () {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // freopen("position.in", "r" , stdin);
    // freopen("position.out", "w", stdout);
    int t = 1;
    // cin >> t;
    for(int i = 1;i <= t; i++) {
        // cout << "Case #" << i << ": ";
        solve();    
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 22192 KB Output is correct
2 Correct 72 ms 23540 KB Output is correct
3 Correct 74 ms 23500 KB Output is correct
4 Correct 90 ms 23520 KB Output is correct
5 Correct 87 ms 23480 KB Output is correct
6 Correct 76 ms 23560 KB Output is correct
7 Correct 78 ms 23528 KB Output is correct
8 Correct 76 ms 23456 KB Output is correct
9 Correct 82 ms 23520 KB Output is correct
10 Correct 76 ms 23560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 21076 KB Output is correct
2 Correct 44 ms 23120 KB Output is correct
3 Correct 42 ms 23756 KB Output is correct
4 Correct 43 ms 23712 KB Output is correct
5 Correct 41 ms 23792 KB Output is correct
6 Correct 45 ms 23716 KB Output is correct
7 Correct 44 ms 23724 KB Output is correct
8 Correct 43 ms 23756 KB Output is correct
9 Correct 44 ms 23780 KB Output is correct
10 Correct 43 ms 23756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5252 KB Output is correct
2 Correct 93 ms 22192 KB Output is correct
3 Correct 72 ms 23540 KB Output is correct
4 Correct 74 ms 23500 KB Output is correct
5 Correct 90 ms 23520 KB Output is correct
6 Correct 87 ms 23480 KB Output is correct
7 Correct 76 ms 23560 KB Output is correct
8 Correct 78 ms 23528 KB Output is correct
9 Correct 76 ms 23456 KB Output is correct
10 Correct 82 ms 23520 KB Output is correct
11 Correct 76 ms 23560 KB Output is correct
12 Correct 34 ms 21076 KB Output is correct
13 Correct 44 ms 23120 KB Output is correct
14 Correct 42 ms 23756 KB Output is correct
15 Correct 43 ms 23712 KB Output is correct
16 Correct 41 ms 23792 KB Output is correct
17 Correct 45 ms 23716 KB Output is correct
18 Correct 44 ms 23724 KB Output is correct
19 Correct 43 ms 23756 KB Output is correct
20 Correct 44 ms 23780 KB Output is correct
21 Correct 43 ms 23756 KB Output is correct
22 Correct 100 ms 22792 KB Output is correct
23 Correct 95 ms 21768 KB Output is correct
24 Correct 97 ms 23836 KB Output is correct
25 Correct 97 ms 23760 KB Output is correct
26 Correct 86 ms 23784 KB Output is correct
27 Correct 85 ms 23824 KB Output is correct
28 Correct 88 ms 23860 KB Output is correct
29 Correct 87 ms 23848 KB Output is correct
30 Correct 88 ms 23836 KB Output is correct
31 Correct 83 ms 23764 KB Output is correct