結果
問題 |
No.898 tri-βutree
|
ユーザー |
|
提出日時 | 2020-01-22 11:20:01 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 296 ms / 4,000 ms |
コード長 | 2,954 bytes |
コンパイル時間 | 1,669 ms |
コンパイル使用メモリ | 134,544 KB |
実行使用メモリ | 28,800 KB |
最終ジャッジ日時 | 2024-11-08 23:20:18 |
合計ジャッジ時間 | 8,284 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 21 |
コンパイルメッセージ
main.cpp: In function 'll clz(ll)': main.cpp:48:1: warning: control reaches end of non-void function [-Wreturn-type] 48 | } | ^
ソースコード
#include <cstdio> #include <iostream> #include <string> #include <sstream> #include <stack> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <set> #include <cstdlib> #include <bitset> #include <tuple> #include <assert.h> #include <deque> #include <bitset> #include <iomanip> #include <limits> #include <chrono> #include <random> #include <array> #include <unordered_map> #include <functional> #include <complex> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long MAX = 5100000; const long long INF = 1LL << 60; const long long mod = 1000000007LL; //const long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; ll N; vector<vector<ll>> ug; vector<vector<pair<ll, ll>>> wg; vector<ll> sum; ll clz(ll x) { for (ll i = 31; i >= 0; i--) { if ((x >> i) & 1) { return 31 - i; } } } template< typename G > struct DoublingLowestCommonAncestor { const int LOG; vector< int > dep; const G& g; vector< vector< int > > table; DoublingLowestCommonAncestor(const G& g) : g(g), dep(g.size()), LOG(32 - clz(g.size())) { table.assign(LOG, vector< int >(g.size(), -1)); } void dfs(int idx, int par, int d) { table[0][idx] = par; dep[idx] = d; for (auto& to : g[idx]) { if (to != par) dfs(to, idx, d + 1); } } void build() { dfs(0, -1, 0); for (int k = 0; k + 1 < LOG; k++) { for (int i = 0; i < table[k].size(); i++) { if (table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int i = LOG - 1; i >= 0; i--) { if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v]; } if (u == v) return u; for (int i = LOG - 1; i >= 0; i--) { if (table[i][u] != table[i][v]) { u = table[i][u]; v = table[i][v]; } } return table[0][u]; } }; void dfs(ll cur, ll pre) { for (auto next : wg[cur]) { if (next.first == pre) continue; sum[next.first] = sum[cur] + next.second; dfs(next.first, cur); } } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ scanf("%lld", &N); ug.resize(N), wg.resize(N); for (ll i = 0; i < N - 1; i++) { ll x, y, w; scanf("%lld %lld %lld", &x, &y, &w); ug[x].push_back(y); ug[y].push_back(x); wg[x].emplace_back(y, w); wg[y].emplace_back(x, w); } sum = vector<ll>(N); dfs(0, -1); DoublingLowestCommonAncestor<vector<vector<ll>>> lca(ug); lca.build(); ll Q; scanf("%lld", &Q); while (Q--) { ll x, y, z; scanf("%lld %lld %lld", &x, &y, &z); ll xy = lca.query(x, y); ll yz = lca.query(y, z); ll xz = lca.query(x, z); ll xyz = lca.query(xy, z); ll res = sum[x] + sum[y] + sum[z] - sum[xyz] * 3; res -= sum[xy] + sum[yz] + sum[xz] - sum[xyz] * 3; printf("%lld\n", res); } return 0; }