結果
| 問題 |
No.1002 Twotone
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-02-28 23:19:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,174 bytes |
| コンパイル時間 | 3,426 ms |
| コンパイル使用メモリ | 219,092 KB |
| 最終ジャッジ日時 | 2025-01-09 03:18:25 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 32 TLE * 1 |
ソースコード
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
using namespace std;
int64_t solve(int n, const vector<vector<pair<int, int> > > & g) {
int64_t ans = 0;
function<pair<map<int, int>, map<pair<int, int>, int> > (int, int)> go = [&](int x, int parent) {
int sum_one = 0;
map<int, int> one;
map<pair<int, int>, int> two;
map<pair<int, int>, int> two_lazy;
map<int, int> two_contains;
auto add_two = [&](int c, int d, int k) {
if (c > d) swap(c, d);
ans += (int64_t)k;
ans += (int64_t)k * two[make_pair(c, d)];
ans += (int64_t)k * (one.count(c) ? one[c] : 0);
ans += (int64_t)k * (one.count(d) ? one[d] : 0);
two_lazy[make_pair(c, d)] += k;
};
auto add_one = [&](int c, int k) {
ans += (int64_t)k * (sum_one - one[c]);
ans += (int64_t)k * (two_contains.count(c) ? two_contains[c] : 0);
one[c] += k;
sum_one += k;
};
for (auto [y, c] : g[x]) if (y != parent) {
auto [sub_one, sub_two] = go(y, x);
for (auto [de, k] : sub_two) {
auto [d, e] = de;
if (d == c or e == c) {
add_two(d, e, k);
}
}
for (auto [d, k] : sub_one) {
if (d != c) {
add_two(c, d, k);
}
}
add_one(c, 1 + (sub_one.count(c) ? sub_one[c] : 0));
for (auto [de, k] : two_lazy) {
auto [d, e] = de;
two[de] += k;
two_contains[d] += k;
two_contains[e] += k;
}
two_lazy = map<pair<int, int>, int>();
}
return make_pair(one, two);
};
go(0, -1);
return ans;
}
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);
}
cout << solve(n, g) << endl;
return 0;
}