結果
問題 | No.1002 Twotone |
ユーザー | pekempey |
提出日時 | 2020-02-28 22:42:47 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,157 ms / 5,000 ms |
コード長 | 3,022 bytes |
コンパイル時間 | 2,263 ms |
コンパイル使用メモリ | 194,648 KB |
実行使用メモリ | 47,432 KB |
最終ジャッジ日時 | 2023-08-03 21:26:56 |
合計ジャッジ時間 | 17,825 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge13 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
4,380 KB |
testcase_01 | AC | 2 ms
4,376 KB |
testcase_02 | AC | 1 ms
4,380 KB |
testcase_03 | AC | 370 ms
13,048 KB |
testcase_04 | AC | 503 ms
16,088 KB |
testcase_05 | AC | 503 ms
16,116 KB |
testcase_06 | AC | 1 ms
4,380 KB |
testcase_07 | AC | 233 ms
11,132 KB |
testcase_08 | AC | 438 ms
15,948 KB |
testcase_09 | AC | 441 ms
15,832 KB |
testcase_10 | AC | 2 ms
4,384 KB |
testcase_11 | AC | 451 ms
14,048 KB |
testcase_12 | AC | 632 ms
17,200 KB |
testcase_13 | AC | 653 ms
17,232 KB |
testcase_14 | AC | 1 ms
4,376 KB |
testcase_15 | AC | 307 ms
10,928 KB |
testcase_16 | AC | 632 ms
16,612 KB |
testcase_17 | AC | 643 ms
16,544 KB |
testcase_18 | AC | 2 ms
4,380 KB |
testcase_19 | AC | 637 ms
18,284 KB |
testcase_20 | AC | 785 ms
24,896 KB |
testcase_21 | AC | 752 ms
24,344 KB |
testcase_22 | AC | 2 ms
4,376 KB |
testcase_23 | AC | 539 ms
18,084 KB |
testcase_24 | AC | 1,016 ms
27,124 KB |
testcase_25 | AC | 1,008 ms
27,348 KB |
testcase_26 | AC | 2 ms
4,384 KB |
testcase_27 | AC | 151 ms
12,192 KB |
testcase_28 | AC | 263 ms
16,600 KB |
testcase_29 | AC | 251 ms
16,572 KB |
testcase_30 | AC | 2 ms
4,384 KB |
testcase_31 | AC | 237 ms
16,440 KB |
testcase_32 | AC | 267 ms
16,460 KB |
testcase_33 | AC | 235 ms
16,476 KB |
testcase_34 | AC | 1,157 ms
47,432 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define range(a) a.begin(), a.end() int main() { int N, K; cin >> N >> K; vector<vector<pair<int, int>>> G(N); rep(i, N - 1) { int u, v, c; cin >> u >> v >> c; u--; v--; G[u].emplace_back(v, c); G[v].emplace_back(u, c); } vector<bool> used(N); vector<int> sz(N); auto dfs2 = [&](auto dfs2, int u, int p) -> void { sz[u] = 1; for (auto e : G[u]) if (e.first != p && not used[e.first]) { dfs2(dfs2, e.first, u); sz[u] += sz[e.first]; } }; auto dfs3 = [&](auto dfs3, int u, int p, int n) -> int { for (auto e : G[u]) if (e.first != p && not used[e.first]) { int v = e.first; if (sz[v] > n / 2) return dfs3(dfs3, v, u, n); } return u; }; auto dfs4 = [&](auto dfs4, int u, int p, int now1, int now2, map<pair<int, int>, ll> &res) -> void { res[minmax(now1, now2)]++; for (auto e : G[u]) if (e.first != p && not used[e.first]) { int v = e.first; int next1 = now1; int next2 = now2; if (next1 == -1) { next1 = e.second; } else if (next1 == e.second) { } else if (next2 == -1) { next2 = e.second; } else if (next2 == e.second) { } else { continue; } dfs4(dfs4, v, u, next1, next2, res); } }; ll ans = 0; auto dfs = [&](auto dfs, int u) -> void { dfs2(dfs2, u, -1); u = dfs3(dfs3, u, -1, sz[u]); map<pair<int, int>, ll> mp; map<int, ll> two; map<int, ll> one; ll single = 0; used[u] = true; for (auto e : G[u]) if (not used[e.first]) { int v = e.first; map<pair<int, int>, ll> np; dfs4(dfs4, v, u, e.second, -1, np); for (auto kv : np) { if (kv.first.first != -1) { // nisyoku ans += kv.second; if (mp.count(kv.first)) { ans += kv.second * mp[kv.first]; } if (one.count(kv.first.first)) { ans += kv.second * one[kv.first.first]; } if (one.count(kv.first.second)) { ans += kv.second * one[kv.first.second]; } } else if (kv.first.second != -1) { // issyoku ans += single * kv.second; if (mp.count(kv.first)) { ans -= mp[kv.first] * kv.second; } if (two.count(kv.first.second)) { ans += kv.second * two[kv.first.second]; } } } for (auto kv : np) { mp[kv.first] += kv.second; if (kv.first.first != -1) { two[kv.first.first] += kv.second; two[kv.first.second] += kv.second; } if (kv.first.first == -1 && kv.first.second != -1) { single += kv.second; one[kv.first.second] += kv.second; } } } for (auto e : G[u]) if (not used[e.first]) { dfs(dfs, e.first); } }; dfs(dfs, 0); cout << ans << endl; }