結果

問題 No.1002 Twotone
ユーザー kimiyukikimiyuki
提出日時 2020-02-28 23:19:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,174 bytes
コンパイル時間 2,894 ms
コンパイル使用メモリ 220,296 KB
実行使用メモリ 137,808 KB
最終ジャッジ日時 2024-04-21 20:47:08
合計ジャッジ時間 17,981 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 301 ms
12,544 KB
testcase_04 AC 397 ms
15,440 KB
testcase_05 AC 410 ms
15,592 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 155 ms
10,624 KB
testcase_08 AC 268 ms
15,488 KB
testcase_09 AC 272 ms
15,360 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 248 ms
13,440 KB
testcase_12 AC 325 ms
16,616 KB
testcase_13 AC 329 ms
16,512 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 169 ms
10,752 KB
testcase_16 AC 309 ms
16,072 KB
testcase_17 AC 309 ms
16,128 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 344 ms
74,592 KB
testcase_20 AC 436 ms
137,808 KB
testcase_21 AC 432 ms
131,828 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 253 ms
61,156 KB
testcase_24 AC 420 ms
110,720 KB
testcase_25 AC 405 ms
91,136 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 150 ms
12,520 KB
testcase_28 AC 251 ms
16,160 KB
testcase_29 AC 246 ms
16,152 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 237 ms
15,620 KB
testcase_32 AC 251 ms
15,772 KB
testcase_33 AC 232 ms
15,856 KB
testcase_34 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0