結果
問題 | No.898 tri-βutree |
ユーザー | tnakao0123 |
提出日時 | 2019-10-05 01:25:01 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 147 ms / 4,000 ms |
コード長 | 2,034 bytes |
コンパイル時間 | 1,280 ms |
コンパイル使用メモリ | 92,480 KB |
実行使用メモリ | 17,664 KB |
最終ジャッジ日時 | 2024-11-08 22:38:58 |
合計ジャッジ時間 | 5,665 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 76 ms
16,768 KB |
testcase_01 | AC | 3 ms
6,016 KB |
testcase_02 | AC | 4 ms
6,144 KB |
testcase_03 | AC | 3 ms
6,144 KB |
testcase_04 | AC | 3 ms
6,016 KB |
testcase_05 | AC | 3 ms
5,888 KB |
testcase_06 | AC | 3 ms
6,016 KB |
testcase_07 | AC | 143 ms
17,536 KB |
testcase_08 | AC | 147 ms
17,536 KB |
testcase_09 | AC | 140 ms
17,536 KB |
testcase_10 | AC | 138 ms
17,664 KB |
testcase_11 | AC | 139 ms
17,536 KB |
testcase_12 | AC | 142 ms
17,664 KB |
testcase_13 | AC | 139 ms
17,664 KB |
testcase_14 | AC | 142 ms
17,536 KB |
testcase_15 | AC | 142 ms
17,408 KB |
testcase_16 | AC | 141 ms
17,664 KB |
testcase_17 | AC | 140 ms
17,536 KB |
testcase_18 | AC | 142 ms
17,536 KB |
testcase_19 | AC | 139 ms
17,408 KB |
testcase_20 | AC | 139 ms
17,408 KB |
testcase_21 | AC | 137 ms
17,536 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:71:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 71 | scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:75:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 75 | scanf("%d%d%d", &u, &v, &w); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~ main.cpp:109:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 109 | scanf("%d", &qn); | ~~~~~^~~~~~~~~~~ main.cpp:113:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 113 | scanf("%d%d%d", &x, &y, &z); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*- * * 898.cc: No.898 tri-硫utree - yukicoder */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_N = 100000; const int K = 17; /* typedef */ typedef long long ll; typedef queue<int> qi; typedef pair<int,int> pii; typedef vector<pii> vpii; /* global variables */ vpii nbrs[MAX_N]; int prts[MAX_N][K], ds[MAX_N]; ll pws[MAX_N]; /* subroutines */ int lca(int u, int v) { if (ds[u] > ds[v]) swap(u, v); for (int i = K - 1; i >= 0; i--) if (((ds[v] - ds[u]) >> i) & 1) v = prts[v][i]; if (u == v) return u; for(int i = K - 1; i >= 0; i--) if (prts[u][i] != prts[v][i]) { u = prts[u][i]; v = prts[v][i]; } return prts[u][0]; } ll pathw(int u, int v) { int a = lca(u, v); return pws[u] + pws[v] - pws[a] * 2; } /* main */ int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); nbrs[u].push_back(pii(v, w)); nbrs[v].push_back(pii(u, w)); } prts[0][0] = -1; ds[0] = 0; pws[0] = 0; qi q; q.push(0); while (! q.empty()) { int u = q.front(); q.pop(); for (int k = 0; k < K - 1; k++) prts[u][k + 1] = (prts[u][k] >= 0) ? prts[prts[u][k]][k] : -1; int &up = prts[u][0], vd = ds[u] + 1; vpii &nbru = nbrs[u]; for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int &v = vit->first, &w = vit->second; if (v != up) { prts[v][0] = u; ds[v] = vd; pws[v] = pws[u] + w; q.push(v); } } } int qn; scanf("%d", &qn); while (qn--) { int x, y, z; scanf("%d%d%d", &x, &y, &z); ll sum = (pathw(x, y) + pathw(y, z) + pathw(z, x)) / 2; printf("%lld\n", sum); } return 0; }