結果
問題 | No.1002 Twotone |
ユーザー | TAISA_ |
提出日時 | 2020-03-01 21:20:45 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,174 ms / 5,000 ms |
コード長 | 2,769 bytes |
コンパイル時間 | 2,112 ms |
コンパイル使用メモリ | 185,252 KB |
実行使用メモリ | 65,816 KB |
最終ジャッジ日時 | 2024-04-21 22:27:12 |
合計ジャッジ時間 | 12,671 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 268 ms
16,000 KB |
testcase_04 | AC | 368 ms
19,840 KB |
testcase_05 | AC | 373 ms
19,840 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 144 ms
13,312 KB |
testcase_08 | AC | 264 ms
19,712 KB |
testcase_09 | AC | 259 ms
19,784 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 263 ms
17,276 KB |
testcase_12 | AC | 375 ms
21,344 KB |
testcase_13 | AC | 373 ms
21,632 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 169 ms
13,440 KB |
testcase_16 | AC | 357 ms
20,972 KB |
testcase_17 | AC | 399 ms
20,992 KB |
testcase_18 | AC | 1 ms
5,376 KB |
testcase_19 | AC | 414 ms
25,216 KB |
testcase_20 | AC | 559 ms
35,048 KB |
testcase_21 | AC | 519 ms
34,432 KB |
testcase_22 | AC | 3 ms
5,376 KB |
testcase_23 | AC | 350 ms
19,968 KB |
testcase_24 | AC | 690 ms
31,784 KB |
testcase_25 | AC | 690 ms
29,220 KB |
testcase_26 | AC | 2 ms
5,376 KB |
testcase_27 | AC | 73 ms
15,636 KB |
testcase_28 | AC | 134 ms
18,956 KB |
testcase_29 | AC | 118 ms
19,152 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 110 ms
18,740 KB |
testcase_32 | AC | 131 ms
18,812 KB |
testcase_33 | AC | 118 ms
18,900 KB |
testcase_34 | AC | 1,174 ms
65,816 KB |
ソースコード
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() #define eb emplace_back using namespace std; using ll = long long; using P = pair<ll, ll>; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll LINF = (1LL << 60) - 1LL; constexpr ll MOD = 1e9 + 7; template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chmax(T &a, T b) { a = max(a, b); } int n, k; vector<vector<P>> G; vector<int> vis, sz; ll res; void szd(int i, int p) { sz[i] = 1; for (auto &e : G[i]) { if (e.first == p || vis[e.first]) { continue; } szd(e.first, i); sz[i] += sz[e.first]; } } int findc(int i, int p, int lim) { for (auto &e : G[i]) { if (e.first == p || vis[e.first]) { continue; } if (sz[e.first] >= lim) { return findc(e.first, i, lim); } } return i; } void dfs(int i, int p, int a, int b, map<P, ll> &mp) { if (a < b) { swap(a, b); } mp[P(a, b)]++; for (auto &e : G[i]) { if (e.first == p || vis[e.first]) { continue; } if (a == e.second || b == e.second) { dfs(e.first, i, a, b, mp); } else if (b == -1) { dfs(e.first, i, a, e.second, mp); } } } void calc(int v) { szd(v, -1); int c = findc(v, -1, sz[v] / 2); map<P, ll> mp; map<ll, ll> cnt, m1; ll sum = 0; for (auto &e : G[c]) { if (vis[e.first]) { continue; } map<P, ll> mpn; dfs(e.first, c, e.second, -1, mpn); for (auto p : mpn) { if (p.first.second == -1) { res += (cnt[p.first.first] + sum - m1[p.first.first]) * p.second; } else { res += (mp[p.first] + m1[p.first.first] + m1[p.first.second]) * p.second; } } for (auto p : mpn) { if (p.first.second == -1) { m1[p.first.first] += p.second; sum += p.second; } else { res += p.second; cnt[p.first.first] += p.second; cnt[p.first.second] += p.second; mp[p.first] += p.second; } } } vis[c] = 1; for (auto &e : G[c]) { if (vis[e.first]) { continue; } calc(e.first); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; G.resize(n); vis.resize(n); sz.resize(n); for (int i = 0; i < n - 1; i++) { int a, b, c; cin >> a >> b >> c; --a; --b; --c; G[a].eb(b, c); G[b].eb(a, c); } calc(0); cout << res << '\n'; }