結果
問題 | No.898 tri-βutree |
ユーザー | hipopo |
提出日時 | 2020-04-13 15:08:35 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 811 ms / 4,000 ms |
コード長 | 3,572 bytes |
コンパイル時間 | 1,917 ms |
コンパイル使用メモリ | 149,484 KB |
実行使用メモリ | 28,364 KB |
最終ジャッジ日時 | 2024-11-08 23:35:49 |
合計ジャッジ時間 | 16,196 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 258 ms
28,364 KB |
testcase_01 | AC | 13 ms
15,992 KB |
testcase_02 | AC | 12 ms
15,868 KB |
testcase_03 | AC | 12 ms
15,860 KB |
testcase_04 | AC | 13 ms
15,864 KB |
testcase_05 | AC | 12 ms
15,732 KB |
testcase_06 | AC | 12 ms
15,992 KB |
testcase_07 | AC | 779 ms
24,184 KB |
testcase_08 | AC | 750 ms
24,184 KB |
testcase_09 | AC | 768 ms
24,312 KB |
testcase_10 | AC | 780 ms
24,312 KB |
testcase_11 | AC | 811 ms
24,052 KB |
testcase_12 | AC | 783 ms
24,180 KB |
testcase_13 | AC | 794 ms
24,312 KB |
testcase_14 | AC | 754 ms
24,316 KB |
testcase_15 | AC | 776 ms
24,308 KB |
testcase_16 | AC | 770 ms
24,180 KB |
testcase_17 | AC | 776 ms
24,184 KB |
testcase_18 | AC | 768 ms
24,184 KB |
testcase_19 | AC | 778 ms
24,184 KB |
testcase_20 | AC | 795 ms
24,316 KB |
testcase_21 | AC | 782 ms
24,208 KB |
ソースコード
#include <algorithm> #include <cmath> #include <complex> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #include <functional> #include <cstring> #include <regex> #include <random> #include <cassert> #include <stack> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, s, n) for (int i = (s); i < (int)(n); i++) #define revrep(i, n) for (int i = (n) - 1; i >= 0; i--) #define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--) #define debug(x) cerr << #x << ": " << x << "\n" #define popcnt(x) __builtin_popcount(x) using ll = long long; using P = pair<int, int>; 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; } template<class T> istream& operator >>(istream &is, vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i); return is; } template<class T, class U> ostream& operator <<(ostream &os, pair<T, U> p) { cout << '(' << p.first << ", " << p.second << ')'; return os; } template<class T> void print(const vector<T> &v, const string &delimiter) { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; } template<class T> void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); } const int MAX_V = 100000; const int MAX_LOG_V = log2(MAX_V) + 1; vector<vector<int>> graph(MAX_V); int root; vector<vector<int>> parent(MAX_LOG_V, vector<int>(MAX_V + 1)); vector<int> depth(MAX_V + 2); void dfs(int v, int p, int d) { parent.at(0).at(v) = p; depth.at(v) = d; for(int c: graph.at(v)) if (c != p) dfs(c, v, d + 1); } //O(N log N) void init(int V) { dfs(root, -1, 0); for (int k = 0; k + 1 < MAX_LOG_V; k++) { for (int v = 0; v < V; v++) { if (parent.at(k).at(v) < 0) parent.at(k + 1).at(v) = -1; else parent.at(k + 1).at(v) = parent.at(k).at(parent.at(k).at(v)); } } } //O(log N) int lca(int u, int v) { if (depth.at(u) > depth.at(v)) swap(u, v); for (int k = 0; k < MAX_LOG_V; k++) { if ((depth.at(v) - depth.at(u)) >> k & 1) v = parent.at(k).at(v); if (u == v) return u; } for (int k = MAX_LOG_V - 1; k >= 0; k--) { if (parent.at(k).at(u) != parent.at(k).at(v)) { u = parent.at(k).at(u); v = parent.at(k).at(v); } } return parent.at(0).at(u); } struct Edge { int to; ll weight; }; vector<vector<Edge>> edges(MAX_V); vector<ll> d(MAX_V, -1); void calc_dist(int v, ll dist, int pre = -1) { d[v] = dist; for (auto &&e: edges[v]) if (e.to != pre) calc_dist(e.to, dist + e.weight, v); } ll get_dist(int u, int v) { return d[u] + d[v] - d[lca(u, v)] * 2; } int main() { int n; cin >> n; rep(i, n - 1) { int u, v, w; //cin >> u >> v >> w; scanf("%d %d %d", &u, &v, &w); graph[u].push_back(v); graph[v].push_back(u); edges[u].push_back(Edge{v, w}); edges[v].push_back(Edge{u, w}); } root = 0; init(n); calc_dist(0, 0); int q; cin >> q; rep(_q, q) { int x, y, z; //cin >> x >> y >> z; scanf("%d %d %d", &x, &y, &z); int a = lca(lca(x, y), z); ll ans = get_dist(a, x) + get_dist(a, y) + get_dist(a, z) - get_dist(a, lca(x, y)) - get_dist(a, lca(x, z)) - get_dist(a, lca(y, z)); cout << ans << endl; } }