結果
| 問題 |
No.898 tri-βutree
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2019-10-04 21:59:08 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,526 bytes |
| コンパイル時間 | 1,789 ms |
| コンパイル使用メモリ | 133,048 KB |
| 最終ジャッジ日時 | 2025-01-07 20:31:31 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 1 WA * 20 |
ソースコード
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
using CostType = long long;
struct Edge {
int src, dst; CostType cost;
Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
inline bool operator<(const Edge &rhs) const {
return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
}
inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
inline bool operator>(const Edge &rhs) const {
return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src;
}
inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};
struct LCA {
vector<int> depth;
vector<CostType> dist;
LCA(const vector<vector<Edge> > &graph) : graph(graph) {
n = graph.size();
depth.resize(n);
dist.resize(n);
while ((1 << table_h) <= n) ++table_h;
parent.resize(table_h, vector<int>(n));
}
void build(int root = 0) {
dfs(-1, root, 0, 0);
for (int i = 0; i + 1 < table_h; ++i) REP(ver, n) {
parent[i + 1][ver] = (parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]]);
}
}
int query(int u, int v) {
if (depth[u] > depth[v]) swap(u, v);
REP(i, table_h) {
if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v];
}
if (u == v) return u;
for (int i = table_h - 1; i >= 0; --i) {
if (parent[i][u] != parent[i][v]) {
u = parent[i][u];
v = parent[i][v];
}
}
return parent[0][u];
}
CostType distance(int u, int v) { return dist[u] + dist[v] - dist[query(u, v)] * 2; }
private:
int n, table_h = 1;
vector<vector<Edge> > graph;
vector<vector<int> > parent;
void dfs(int par, int ver, int now_depth, CostType now_dist) {
depth[ver] = now_depth;
dist[ver] = now_dist;
parent[0][ver] = par;
for (const Edge &e : graph[ver]) {
if (e.dst != par) dfs(ver, e.dst, now_depth + 1, now_dist + e.cost);
}
}
};
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
// freopen("input.txt", "r", stdin);
int n; cin >> n;
vector<vector<Edge> > graph(n);
REP(_, n - 1) {
int u, v, w; cin >> u >> v >> w;
graph[u].emplace_back(u, v, w);
graph[v].emplace_back(v, u, w);
}
LCA lca(graph);
lca.build();
int q; cin >> q;
while (q--) {
vector<int> xyz(3); REP(i, 3) cin >> xyz[i];
long long ans = LINF;
do {
int l = lca.query(xyz[0], xyz[1]);
long long tmp = 0;
REP(i, 3) tmp += lca.distance(xyz[i], l);
ans = min(ans, tmp);
} while (next_permutation(ALL(xyz)));
cout << ans << '\n';
}
return 0;
}
emthrm