結果
| 問題 |
No.898 tri-βutree
|
| コンテスト | |
| ユーザー |
hipopo
|
| 提出日時 | 2020-04-13 15:05:42 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,398 ms / 4,000 ms |
| コード長 | 3,490 bytes |
| コンパイル時間 | 2,324 ms |
| コンパイル使用メモリ | 143,840 KB |
| 最終ジャッジ日時 | 2025-01-09 18:01:51 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 21 |
ソースコード
#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;
}
}
hipopo