結果
問題 |
No.898 tri-βutree
|
ユーザー |
|
提出日時 | 2023-12-16 09:43:18 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 376 ms / 4,000 ms |
コード長 | 2,051 bytes |
コンパイル時間 | 903 ms |
コンパイル使用メモリ | 77,136 KB |
実行使用メモリ | 41,704 KB |
最終ジャッジ日時 | 2024-09-27 07:36:20 |
合計ジャッジ時間 | 8,924 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 21 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<pair<int, int>> no[100000]; vector<int> o; int in[100000], st[32][300000]; long long d[100000]; void tour(int n, long long e) { d[n] = e; in[n] = o.size(); o.push_back(n); for (auto i : no[n]) { if (d[i.first] == 0) { tour(i.first, e + i.second); o.push_back(n); } } } int lca(int x, int y) { if (in[y] < in[x]) swap(x, y); int u = in[x], v = in[y]; int i = 0; while (1 << (i + 1) <= v - u + 1) i++; if (d[st[i][u]] < d[st[i][v-(1<<i)+1]]) return st[i][u]; return st[i][v-(1<<i)+1]; } int main() { int n, u, v, w, q, x, y, z; cin >> n; for (int i = 0; i < n - 1; i++) { cin >> u >> v >> w; no[u].push_back({v, w}); no[v].push_back({u, w}); } tour(0, 1); for (int i = 0; i < o.size(); i++) { st[0][i] = o[i]; } for (int i = 1; (1 << i) <= o.size(); i++) { for (int j = 0; j < o.size(); j++) { if (d[st[i -1][j]] < d[st[i - 1][min(j + (1 << (i - 1)), (int)o.size() - 1)]]) { st[i][j] = st[i - 1][j]; } else { st[i][j] = st[i - 1][min(j + (1 << (i - 1)), (int)o.size() - 1)]; } } } cin >> q; for (int i = 0; i < q; i++) { cin >> x >> y >> z; u = lca(x, y); v = lca(y, z); w = lca(z, x); if (u == v) cout << d[x] + d[y] + d[z] - d[w] - 2 * d[u] << endl; else if (v == w) cout << d[x] + d[y] + d[z] - d[u] - 2 * d[v] << endl; else cout << d[x] + d[y] + d[z] - d[v] - 2 * d[u] << endl; } // for (int i = 0; i < o.size(); i++) cout << o[i] << " "; // cout << endl; // for (int i = 0; i < n; i++) cout << d[i] << " "; // cout << endl; // for (int i = 0; i < 4; i++) { // for (int j = 0; j < o.size(); j++) { // cout << st[i][j] << " "; // } // cout << endl; // } // cout << u << " " << v << " " << w << endl; }