結果
問題 | No.898 tri-βutree |
ユーザー | fine |
提出日時 | 2019-10-04 21:58:29 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,244 bytes |
コンパイル時間 | 2,216 ms |
コンパイル使用メモリ | 171,088 KB |
実行使用メモリ | 25,856 KB |
最終ジャッジ日時 | 2024-10-03 07:36:55 |
合計ジャッジ時間 | 7,323 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 86 ms
25,856 KB |
testcase_01 | AC | 4 ms
5,760 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, ll>; const int MAX_V = 100000; const int MAX_LOG_V = 17; vector<P> G[MAX_V]; int root = 0; int parent[MAX_LOG_V][MAX_V]; int depth[MAX_V]; ll weights[MAX_V]; void dfs(int v, int p, int d, ll w) { parent[0][v] = p; depth[v] = d; weights[v] = w; for (int i = 0; i < G[v].size(); i++) { if (G[v][i].first != p) dfs(G[v][i].first, v, d + 1, w + G[v][i].second); } } void init(int V) { dfs(root, -1, 0, 0); for (int k = 0; k + 1 < MAX_LOG_V; k++) { for (int v = 0; v < V; v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } int lca(int u, int v) { if (depth[u] > depth[v]) swap(u, v); for (int k = 0; k < MAX_LOG_V; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = MAX_LOG_V - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i < n; i++) { int u, v; ll w; cin >> u >> v >> w; G[u].emplace_back(v, w); G[v].emplace_back(u, w); } init(n); int q; cin >> q; for (int i = 0; i < q; i++) { int x, y, z; cin >> x >> y >> z; int xy = lca(x, y); int yz = lca(y, z); int zx = lca(z, x); int tar = xy; int hoge = lca(xy, z); ll ans = weights[x] + weights[y] - weights[xy] * 2; ans += weights[z] + weights[xy] - weights[hoge]; if (depth[tar] < depth[yz]) { tar = yz; ans = weights[y] + weights[z] - weights[yz] * 2; ans += weights[x] + weights[yz] - weights[hoge]; } if (depth[tar] < depth[zx]) { tar = zx; ans = weights[z] + weights[x] - weights[zx] * 2; ans += weights[y] + weights[zx] - weights[hoge]; } cout << ans << "\n"; } return 0; }