結果
問題 | No.898 tri-βutree |
ユーザー | hipopo |
提出日時 | 2020-04-13 15:05:42 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 981 ms / 4,000 ms |
コード長 | 3,490 bytes |
コンパイル時間 | 1,824 ms |
コンパイル使用メモリ | 149,520 KB |
実行使用メモリ | 28,280 KB |
最終ジャッジ日時 | 2024-11-08 23:35:00 |
合計ジャッジ時間 | 18,641 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 346 ms
28,280 KB |
testcase_01 | AC | 11 ms
15,740 KB |
testcase_02 | AC | 12 ms
15,736 KB |
testcase_03 | AC | 12 ms
15,748 KB |
testcase_04 | AC | 12 ms
15,732 KB |
testcase_05 | AC | 12 ms
15,864 KB |
testcase_06 | AC | 12 ms
15,740 KB |
testcase_07 | AC | 915 ms
24,056 KB |
testcase_08 | AC | 935 ms
24,060 KB |
testcase_09 | AC | 931 ms
24,056 KB |
testcase_10 | AC | 936 ms
24,184 KB |
testcase_11 | AC | 915 ms
24,056 KB |
testcase_12 | AC | 968 ms
24,052 KB |
testcase_13 | AC | 921 ms
24,052 KB |
testcase_14 | AC | 934 ms
24,052 KB |
testcase_15 | AC | 981 ms
24,180 KB |
testcase_16 | AC | 961 ms
24,056 KB |
testcase_17 | AC | 919 ms
24,136 KB |
testcase_18 | AC | 932 ms
24,056 KB |
testcase_19 | AC | 929 ms
24,056 KB |
testcase_20 | AC | 885 ms
24,056 KB |
testcase_21 | AC | 914 ms
24,096 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; 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; 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; } }