結果
| 問題 |
No.898 tri-βutree
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-10-25 10:46:17 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 240 ms / 4,000 ms |
| コード長 | 2,385 bytes |
| コンパイル時間 | 1,323 ms |
| コンパイル使用メモリ | 109,268 KB |
| 実行使用メモリ | 34,560 KB |
| 最終ジャッジ日時 | 2024-11-08 23:09:17 |
| 合計ジャッジ時間 | 6,723 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 21 |
コンパイルメッセージ
In member function 'int LCA<T>::get_lca(int, int) [with T = long long int]',
inlined from 'T LCA<T>::get_length(int, int) [with T = long long int]' at main.cpp:73:20,
inlined from 'int main()' at main.cpp:103:52:
main.cpp:58:17: warning: 'v3' may be used uninitialized [-Wmaybe-uninitialized]
58 | if(depth[x] > depth[y]) swap(x, y);
main.cpp: In function 'int main()':
main.cpp:94:27: note: 'v3' was declared here
94 | int dep = -1, v1, v2, v3, a;
| ^~
In member function 'int LCA<T>::get_lca(int, int) [with T = long long int]',
inlined from 'T LCA<T>::get_length(int, int) [with T = long long int]' at main.cpp:73:20,
inlined from 'int main()' at main.cpp:103:27:
main.cpp:58:17: warning: 'v2' may be used uninitialized [-Wmaybe-uninitialized]
58 | if(depth[x] > depth[y]) swap(x, y);
main.cpp: In function 'int main()':
main.cpp:94:23: note: 'v2' was declared here
94 | int dep = -1, v1, v2, v3, a;
| ^~
In member function 'int LCA<T>::get_lca(int, int) [with T = long long int]',
inlined from 'T LCA<T>::get_length(int, int) [with T = long long int]' at main.cpp:73:20,
inlined from 'int main()' at main.cpp:103:27:
main.cpp:58:17: warning: 'v1' may be used uninitialized [-Wmaybe-uninitialized]
58 | if(depth[x] > depth[y]) swap(x, y);
main.cpp: In function 'int main()':
main.cpp:94:19: note: 'v1' was declared here
94 | int dep = -1, v1, v2, v3, a;
| ^~
ソースコード
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <iomanip>
#include <cmath>
#include <map>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);++i)
#define rep(i,a,b) for(int i=a;i<(b);++i)
using ll = long long;
constexpr ll INF = 1LL << 60;
constexpr int MOD = 998244353;
struct Edge {
int src, dst, cost;
Edge(int s, int d, int c = 1) : src(s), dst(d), cost(c) {}
bool operator<(const Edge &e){ return cost < e.cost;}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
// build O(NlogN), query O(logN)
template<typename T>
struct LCA {
vector<T> cost;
vector<int> depth;
vector<vector<int>> par;
Graph g;
int logn;
LCA(int n, Graph g) : cost(n), depth(n), g(g) {
logn = 0;
while((1<<logn) < n) ++logn;
par.assign(n+1, vector<int>(logn, n));
dfs(0, -1, 0, 0);
for(int i = 0; i < logn-1; ++i) {
for(int v = 0; v < n; ++v) {
par[v][i+1] = par[par[v][i]][i];
}
}
}
void dfs(int v, int p, int d, T c) {
if(p != -1) par[v][0] = p;
depth[v] = d;
cost[v] = c;
for(Edge e : g[v]) {
if(e.dst == p) continue;
dfs(e.dst, v, d+1, cost[v] + e.cost);
}
}
int get_lca(int x, int y) {
if(depth[x] > depth[y]) swap(x, y);
for(int i = logn-1; i >= 0; --i) {
if(((depth[y] - depth[x])>>i) & 1) y = par[y][i];
}
if(x == y) return y;
for(int i = logn-1; i >= 0; --i) {
int nx = par[x][i], ny = par[y][i];
if(nx != ny) x = nx, y = ny;
}
return par[y][0];
}
T get_length(int x, int y) {
int z = get_lca(x, y);
return cost[x] + cost[y] - cost[z] * 2;
}
};
ll q, n, k, u, v, w;
int main() {
cin.tie(0); ios_base::sync_with_stdio(false);
cin >> n;
Graph g(n);
REP(i, n-1) {
cin >> u >> v >> w;
g[u].emplace_back(u, v, w);
g[v].emplace_back(v, u, w);
}
LCA<ll> lca(n, g);
cin >> q;
REP(i, q) {
cin >> u >> v >> w;
int dep = -1, v1, v2, v3, a;
a = lca.get_lca(u, v);
if(lca.depth[a] > dep) v1 = u, v2 = v, v3 = w, dep = lca.depth[a];
a = lca.get_lca(v, w);
if(lca.depth[a] > dep) v1 = v, v2 = w, v3 = u, dep = lca.depth[a];
a = lca.get_lca(w, u);
if(lca.depth[a] > dep) v1 = w, v2 = u, v3 = v, dep = lca.depth[a];
cout << lca.get_length(v1, v2) + lca.get_length(lca.get_lca(v1, v2), v3) << '\n';
}
return 0;
}