結果
問題 | No.901 K-ary εxtrεεmε |
ユーザー | catupper |
提出日時 | 2019-10-05 02:21:24 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 363 ms / 3,000 ms |
コード長 | 2,414 bytes |
コンパイル時間 | 939 ms |
コンパイル使用メモリ | 92,868 KB |
実行使用メモリ | 34,760 KB |
最終ジャッジ日時 | 2024-04-15 00:43:23 |
合計ジャッジ時間 | 9,067 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 170 ms
34,760 KB |
testcase_01 | AC | 4 ms
9,064 KB |
testcase_02 | AC | 7 ms
9,300 KB |
testcase_03 | AC | 6 ms
12,044 KB |
testcase_04 | AC | 6 ms
9,440 KB |
testcase_05 | AC | 6 ms
8,768 KB |
testcase_06 | AC | 6 ms
9,112 KB |
testcase_07 | AC | 242 ms
30,824 KB |
testcase_08 | AC | 243 ms
30,912 KB |
testcase_09 | AC | 238 ms
31,680 KB |
testcase_10 | AC | 248 ms
31,544 KB |
testcase_11 | AC | 249 ms
30,608 KB |
testcase_12 | AC | 232 ms
31,720 KB |
testcase_13 | AC | 232 ms
31,376 KB |
testcase_14 | AC | 233 ms
31,856 KB |
testcase_15 | AC | 232 ms
30,940 KB |
testcase_16 | AC | 229 ms
31,012 KB |
testcase_17 | AC | 305 ms
31,376 KB |
testcase_18 | AC | 303 ms
31,548 KB |
testcase_19 | AC | 302 ms
31,684 KB |
testcase_20 | AC | 301 ms
31,256 KB |
testcase_21 | AC | 300 ms
31,196 KB |
testcase_22 | AC | 222 ms
31,668 KB |
testcase_23 | AC | 226 ms
31,524 KB |
testcase_24 | AC | 220 ms
31,348 KB |
testcase_25 | AC | 224 ms
31,060 KB |
testcase_26 | AC | 228 ms
31,904 KB |
testcase_27 | AC | 356 ms
31,776 KB |
testcase_28 | AC | 363 ms
30,908 KB |
testcase_29 | AC | 355 ms
31,752 KB |
ソースコード
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <map> #include <set> #include <string> #include <queue> #include <stack> using namespace std; #define MOD 1000000007 #define INF (1 << 29) #define LINF (1LL << 60) #define EPS (1e-10) typedef long long Int; typedef pair<Int, Int> P; Int ppp; Int come[110000]; Int p[110000][20]; Int depth[110000]; Int node[110000]; Int weight[110000]; vector<P> edge[110000]; void dfs(Int x, Int last = -1, Int d = 0) { come[x] = ppp; node[ppp] = x; depth[x] = d; ppp++; if (last != -1) { for (Int i = 0; i < 20; i++) { if (i == 0) p[x][i] = last; else p[x][i] = p[p[x][i - 1]][i - 1]; } } for (auto p : edge[x]) { Int to = p.first, w = p.second; if (to == last) continue; weight[to] = weight[x] + w; dfs(to, x, d + 1); } } Int la(Int x, Int d, Int pp = 19) { if (d == 0) return x; while (d < (1 << pp)) pp--; x = p[x][pp]; return la(x, d - (1 << pp), pp); } Int lca(Int x, Int y) { if (depth[x] < depth[y]) swap(x, y); if (depth[x] > depth[y]) x = la(x, depth[x] - depth[y]); if (x == y) return x; if (depth[x] != depth[y]) exit(1); Int up = 19; while (p[x][0] != p[y][0]) { while (p[x][up] == p[y][up]) up--; x = p[x][up]; y = p[y][up]; } return p[x][0]; } void solve() { Int k, ans = 0; cin >> k; vector<Int> nodes; for (Int i = 0; i < k; i++) { Int x; cin >> x; nodes.push_back(come[x]); } sort(nodes.begin(), nodes.end()); for (Int i = 0; i < k; i++) { Int x = node[nodes[i]]; Int y = node[nodes[(i + 1) % k]]; Int l = lca(x, y); //cout << x << ":" << weight[x] << " " << y << ":" << weight[y] << " " << l << ":" << weight[l] << endl; ans += weight[x] - weight[l]; ans += weight[y] - weight[l]; } cout << ans / 2 << endl; } int main() { int n, u, v, w, q; cin >> n; for (Int i = 0; i < n - 1; i++) { cin >> u >> v >> w; edge[u].push_back(P(v, w)); edge[v].push_back(P(u, w)); } dfs(0); cin >> q; while (q--) solve(); return 0; }