結果
| 問題 |
No.1094 木登り / Climbing tree
|
| ユーザー |
uw_yu1rabbit
|
| 提出日時 | 2021-01-22 21:34:15 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 3,202 bytes |
| コンパイル時間 | 6,543 ms |
| コンパイル使用メモリ | 120,264 KB |
| 最終ジャッジ日時 | 2025-01-18 03:50:43 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | TLE * 1 MLE * 25 |
ソースコード
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<cstdint>
#include<set>
#include<cstddef>
#include<vector>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = size_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using ld = long double;
template<typename T>
using vec = vector<T>;
#define rep(i, n) for (i64 i = 0; i < (i64)(n); ++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define len(hoge) (i64)((hoge).size())
using P = pair<i64, i64>;
template<typename T>
class LCA {
public:
vector<int_fast32_t> depth;
const int_fast32_t dig;
vector<vector<int_fast64_t>>g;
vector<vector<int_fast64_t>> cost;
vector<int_fast64_t> costs;
vector<vector<int_fast32_t>> doubling;
LCA(int_fast64_t n) : depth(n), costs(n), cost(n), g(n),dig(n) {
doubling.assign(dig, vector<int_fast32_t>(n, -1));
}
void addedge(int_fast64_t u, int_fast64_t v, int_fast64_t c = 0) {
g[u].emplace_back(v);
cost[u].emplace_back(c);
g[v].emplace_back(u);
cost[v].emplace_back(c);
}
void dfs(int_fast32_t now, int_fast32_t par = -1, int_fast32_t d = 0, int_fast64_t c = 0) {
doubling[0][now] = par;
depth[now] = d;
costs[now] = c;
rep(i,len(g[now])) {
if (g[now][i] != par)dfs(g[now][i], now, d + 1, c + cost[now][i]);
}
}
void construct() {
dfs(0);
for (int_fast32_t i = 0; i < dig - 1; i++) {
for (int_fast32_t j = 0; j < doubling[i].size(); j++) {
if (doubling[i][j] == -1)doubling[i + 1][j] = -1;
else doubling[i + 1][j] = doubling[i][doubling[i][j]];
}
}
}
int_fast32_t query_answer(int_fast32_t u, int_fast32_t v) {
if (depth[u] > depth[v])swap(u, v);
for (int_fast32_t i = dig - 1; 0 <= i; i--) {
if (((depth[v] - depth[u]) >> i) & 1) v = doubling[i][v];
}
if (u == v) return u;
for (int_fast32_t i = dig - 1; 0 <= i; --i) {
if (doubling[i][u] != doubling[i][v]) {
u = doubling[i][u];
v = doubling[i][v];
}
}
return doubling[0][u];
}
int_fast64_t length(int_fast64_t u,int_fast64_t v){
return depth[u] + depth[v] - 2 * depth[query_answer(u,v)];
}
int_fast64_t dist(int_fast64_t u,int_fast64_t v){
return costs[u] + costs[v] - 2 * costs[query_answer(u,v)];
}
bool isOnPath(int_fast64_t u, int_fast64_t v,int_fast64_t x){
return length(u,x) + length(v,x) == length(u,v);
}
};
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
i64 n, q;
cin >> n;
LCA<i64> lca(n);
rep(i, n - 1) {
i64 a, b, c;
cin >> a >> b >> c;
a--, b--;
lca.addedge(a,b,c);
}
lca.construct();
cin >> q;
rep(i, q) {
i64 s, t;
cin >> s >> t;
cout << lca.dist(s - 1,t - 1) << endl;
}
}
uw_yu1rabbit