1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<template<typename...> class T, typename t, typename... Args> void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int maxn = 1E5 + 100, INF = 1E9; struct E { int to, d; }; vector<E> G[maxn]; int w[maxn]; vector<int> c[maxn];
bool vis[maxn]; int sz[maxn]; int get_sz(int u, int fa) { int& s = sz[u] = 1; for (E& e: G[u]) { int v = e.to; if (vis[v] || v == fa) continue; s += get_sz(v, u); } return s; }
void get_rt(int u, int fa, int s, int& m, int& rt) { int t = s - sz[u]; for (E& e: G[u]) { int v = e.to; if (vis[v] || v == fa) continue; get_rt(v, u, s, m, rt); t = max(t, sz[v]); } if (t < m) { m = t; rt = u; } }
LL dep[maxn], md[maxn]; void get_dep(int u, int fa, LL d) { dep[u] = d; md[u] = 0; for (E& e: G[u]) { int v = e.to; if (vis[v] || v == fa) continue; get_dep(v, u, d + e.d); md[u] = max(md[u], md[v] + 1); } }
using D = int; struct R { D rt; int sgn; LL dep; }; D pit = 1; vector<R> tr[maxn];
void go(int u, int fa, D rt, D rt2) { tr[u].push_back({rt, 1, dep[u]}); tr[u].push_back({rt2, -1, dep[u]}); for (E& e: G[u]) { int v = e.to; if (v == fa || vis[v]) continue; go(v, u, rt, rt2); } }
void dfs(int u) { int tmp = INF; get_rt(u, -1, get_sz(u, -1), tmp, u); vis[u] = true; get_dep(u, -1, 0); D rt = pit++; tr[u].push_back({rt, 1, 0}); for (E& e: G[u]) { int v = e.to; if (vis[v]) continue; go(v, u, rt, pit++); dfs(v); } }
LL ans[maxn], *ans_p = ans; map<pair<int, int>, LL*> mp; struct Q { int c1, c2; LL* ans; }; Q query[maxn]; vector<Q*> qc[maxn];
void solve(int c2, vector<Q*>& ask) { static LL sum[maxn << 1]; static int cnt[maxn << 1]; for (int& u: c[c2]) for (R& x: tr[u]) { sum[x.rt] += x.dep; ++cnt[x.rt]; } for (Q*& q: ask) { LL& s = *q->ans; for (int& u: c[q->c1]) for (R& x: tr[u]) { s += x.sgn * (sum[x.rt] + cnt[x.rt] * x.dep); } } for (int& u: c[c2]) for (R& x: tr[u]) sum[x.rt] = cnt[x.rt] = 0; }
int main() { int n; cin >> n; FOR (i, 1, n + 1) { scanf("%d", &w[i]); c[w[i]].push_back(i); } FOR (_, 1, n) { int u, v, d; scanf("%d%d%d", &u, &v, &d); G[u].push_back({v, d}); G[v].push_back({u, d}); } dfs(1); int Qn; cin >> Qn; FOR (i, 0, Qn) { int c1, c2; scanf("%d%d", &c1, &c2); if (c[c1].size() > c[c2].size()) swap(c1, c2); query[i] = {c1, c2, nullptr}; auto it = mp.find({c1, c2}); if (it != mp.end()) query[i].ans = it->second; else { mp[{c1, c2}] = query[i].ans = ans_p++; qc[c2].push_back(&query[i]); } } FOR (i, 1, n + 1) if (!qc[i].empty()) solve(i, qc[i]); FOR (i, 0, Qn) printf("%lld\n", *query[i].ans / (query[i].c1 == query[i].c2 ? 2 : 1)); }
|