Submission #1366309

#TimeUsernameProblemLanguageResultExecution timeMemory
1366309otariusRoadside Advertisements (NOI17_roadsideadverts)C++20
100 / 100
27 ms14680 KiB
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rngl(chrono::steady_clock::now().time_since_epoch().count());

#define int long long
// #define int unsigned long long

// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>

const ll mod = 1e9 + 7;
// const ll mod = 998244353;

const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 2 * 1e5 + 15;

vector<pii> g[maxN];
int n, par[maxN][20], pref[maxN], in[maxN], out[maxN], tim;
void dfs(int v, int p) {
    in[v] = ++tim; par[v][0] = p;
    for (pii u : g[v]) {
        if (u.ff == p) continue;
        pref[u.ff] = pref[v] + u.sc;
        dfs(u.ff, v);
    }

    out[v] = tim;
}
bool is_anc(int x, int y) {
    return (in[x] <= in[y] && out[x] >= out[y]);
}
int lca(int x, int y) {
    if (is_anc(x, y)) return x;
    if (is_anc(y, x)) return y;
    for (int j = 19; j >= 0; j--)
        if (!is_anc(par[x][j], y)) x = par[x][j];
    return par[x][0];
}
ll dist(int x, int y) {
    int lc = lca(x, y);
    return pref[x] + pref[y] - 2 * pref[lc];
}
void solve() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int a, b, w; cin >> a >> b >> w;
        g[a].pb({b, w}); g[b].pb({a, w});
    }

    dfs(0, 0);
    for (int j = 1; j < 20; j++) {
        for (int i = 0; i < n; i++) {
            par[i][j] = par[par[i][j - 1]][j - 1];
        }
    }

    int q; cin >> q;
    while (q--) {
        int x[5];
        for (int i = 0; i < 5; i++) cin >> x[i];

        sort(x, x + 5, [&](int a, int b) {
            return in[a] < in[b];
        });

        cout << (dist(x[0], x[1]) + dist(x[1], x[2]) + dist(x[2], x[3]) + dist(x[3], x[4]) + dist(x[4], x[0])) / 2 << '\n';
    }
}
int32_t main() { 
// #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
// #endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...