結果
| 問題 |
No.898 tri-βutree
|
| コンテスト | |
| ユーザー |
nasadigital
|
| 提出日時 | 2019-10-09 10:59:07 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 161 ms / 4,000 ms |
| コード長 | 1,789 bytes |
| コンパイル時間 | 1,692 ms |
| コンパイル使用メモリ | 171,176 KB |
| 実行使用メモリ | 23,084 KB |
| 最終ジャッジ日時 | 2024-11-08 23:01:28 |
| 合計ジャッジ時間 | 6,217 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 21 |
ソースコード
#include <bits/stdc++.h>
#define MAX_SIZE 100000
#define LOG_SIZE 17
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
vector<ii> adjList[MAX_SIZE];
int lcs[MAX_SIZE][LOG_SIZE], lvl[MAX_SIZE];
ll tot[MAX_SIZE];
void dfs(int cur, ll sum, int p, int cd) {
tot[cur] = sum;
lcs[cur][0] = p;
lvl[cur] = cd;
for (auto nxt : adjList[cur])
if (nxt.first != p)
dfs(nxt.first, sum + nxt.second, cur, cd + 1);
}
int findParent(int a, int b) {
if (lvl[b] < lvl[a])
swap(a, b);
int diff = lvl[b] - lvl[a];
for (int ctr1 = 0; ctr1 < LOG_SIZE; ++ctr1)
if (diff & (1 << ctr1))
b = lcs[b][ctr1];
if (a == b)
return a;
for (int ctr1 = LOG_SIZE - 1; ctr1 >= 0; --ctr1)
if (lcs[a][ctr1] != lcs[b][ctr1])
a = lcs[a][ctr1], b = lcs[b][ctr1];
return lcs[a][0];
}
ll pathSum(int a, int b, int p) {
return tot[a] + tot[b] - 2 * tot[p];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n;
int a, b, c;
for (int ctr1 = 1; ctr1 < n; ++ctr1) {
cin >> a >> b >> c;
adjList[a].push_back({b, c});
adjList[b].push_back({a, c});
}
dfs(0, 0, 0, 0);
for (int ctr1 = 1; ctr1 < LOG_SIZE; ++ctr1)
for (int ctr2 = 0; ctr2 < n; ++ctr2)
lcs[ctr2][ctr1] = lcs[lcs[ctr2][ctr1 - 1]][ctr1 - 1];
findParent(1, 3);
cin >> q;
int ch[3];
for (int ctr1 = 0; ctr1 < q; ++ctr1) {
for (int ctr2 = 0; ctr2 < 3; ++ctr2)
cin >> ch[ctr2];
ll rez = 0;
for (int ctr2 = 0; ctr2 < 3; ++ctr2)
rez += pathSum(ch[ctr2], ch[(ctr2 + 1) % 3], findParent(ch[ctr2], ch[(ctr2 + 1) % 3]));
cout << rez / 2 << "\n";
}
return 0;
}
nasadigital