結果
問題 | No.898 tri-βutree |
ユーザー |
![]() |
提出日時 | 2019-10-04 22:37:22 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,952 bytes |
コンパイル時間 | 1,841 ms |
コンパイル使用メモリ | 171,772 KB |
実行使用メモリ | 36,520 KB |
最終ジャッジ日時 | 2024-10-03 08:08:05 |
合計ジャッジ時間 | 12,674 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 1 |
other | TLE * 1 -- * 20 |
コンパイルメッセージ
In function 'll pathSum(int, int, int)', inlined from 'int main()' at main.cpp:73:51: main.cpp:38:26: warning: 't' may be used uninitialized [-Wmaybe-uninitialized] 38 | return tot[a] + tot[b] - 2 * tot[p]; | ~~~~~^ main.cpp: In function 'int main()': main.cpp:64:19: note: 't' was declared here 64 | int f, s, t; | ^ main.cpp:71:35: warning: 's' may be used uninitialized [-Wmaybe-uninitialized] 71 | int smallTree = findParent(f, s); | ~~~~~~~~~~^~~~~~ main.cpp:64:16: note: 's' was declared here 64 | int f, s, t; | ^ main.cpp:71:35: warning: 'f' may be used uninitialized [-Wmaybe-uninitialized] 71 | int smallTree = findParent(f, s); | ~~~~~~~~~~^~~~~~ main.cpp:64:13: note: 'f' was declared here 64 | int f, s, t; | ^
ソースコード
#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[a] > lvl[b]); swap(a, b); for (int ctr1 = 0; ctr1 < LOG_SIZE; ++ctr1) if ((lvl[b] - lvl[a]) & (1 << ctr1)) b = lcs[b][ctr1]; if (a == b) return a; for (int ctr1 = lvl[a] - 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]; cin >> q; for (int ctr1 = 0; ctr1 < q; ++ctr1) { cin >> a >> b >> c; int ab = findParent(a, b); int bc = findParent(b, c); int ac = findParent(a, c); int f, s, t; if (ab == bc) f = a, s = c, t = b; if (bc == ac) f = a, s = b, t = c; if (ab == ac) f = b, s = c, t = a; int smallTree = findParent(f, s); int bigTree = findParent(smallTree, t); cout << pathSum(f, s, smallTree) + pathSum(smallTree, t, bigTree) << "\n"; } return 0; }