結果
| 問題 |
No.898 tri-βutree
|
| コンテスト | |
| ユーザー |
koyopro
|
| 提出日時 | 2020-01-02 16:05:30 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,487 bytes |
| コンパイル時間 | 1,852 ms |
| コンパイル使用メモリ | 166,148 KB |
| 実行使用メモリ | 43,488 KB |
| 最終ジャッジ日時 | 2024-11-22 18:10:17 |
| 合計ジャッジ時間 | 12,762 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 1 WA * 20 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) printf(__VA_ARGS__)
int dxy[] = {0, 1, 0, -1, 0};
/*================================*/
struct Leaf {
int parent, child;
int w = -1;
Leaf(int p, int c, int w=-1): parent(p), child(c), w(w) {};
};
class Tree {
int n;
vector< vector<Leaf> > E;
Tree(int n): n(n) {
E = vector< vector<Leaf> >(n);
}
void add_node(int u, int v, int w) {
E[u].push_back(*new Leaf(u, v, w));
E[v].push_back(*new Leaf(v, u, w));
}
};
struct Edge {
int src, to, w;
Edge(int u, int v, int w): src(u), to(v), w(w) {};
};
int N,Q;
vector< vector<Edge> > E(111111);
int W[111111], P[111111][20], D[111111];
void dfs(int parent, int i, int weight, int depth) {
P[i][0] = parent;
W[i] = weight;
D[i] = depth;
for (auto e : E[i]) {
if (e.to == parent) continue;
dfs(i, e.to, weight + e.w, depth+1);
}
}
int lca(int u, int v) {
// depth調整
if (u > v) swap(u, v);
RFOR(i, 0, 20) {
if ((D[v] - D[u])&(1<<i)) v = P[v][i];
}
if (u == v) return u;
// lca
RFOR(i, 0, 20) {
if (P[v][i] != P[u][i]) {
v = P[v][i];
u = P[u][i];
}
}
return P[u][0];
}
signed main()
{
#if DEBUG
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
#endif
cin>>N;
REP(i,N)REP(j,20) P[i][j] = -1;
int u,v,w;
REP(i, N-1) {
cin >> u >> v >> w;
E[u].push_back(*new Edge(u,v,w));
E[v].push_back(*new Edge(v,u,w));
}
// euler tour -> weight, depth, parent
dfs(-1, 0, 0, 0);
// doubling
FOR(j, 1, 20) {
REP(i, N) {
if (P[i][j-1] == -1) continue;
P[i][j] = P[P[i][j-1]][j-1];
}
}
cin >> Q;
int x,y,z;
REP(i, Q) {
cin>>x>>y>>z;
int ans = W[x] + W[y] + W[z] - lca(x, y) - lca(y, z) - lca(z, x);
out("%lld\n", ans);
}
return 0;
}
koyopro