結果

問題 No.1002 Twotone
ユーザー kimiyukikimiyuki
提出日時 2020-02-28 22:41:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,313 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 216,340 KB
実行使用メモリ 76,016 KB
最終ジャッジ日時 2024-04-21 19:52:05
合計ジャッジ時間 10,807 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
5,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 3 ms
5,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
5,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 147 ms
12,644 KB
testcase_28 AC 247 ms
15,904 KB
testcase_29 AC 240 ms
16,028 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 227 ms
15,748 KB
testcase_32 AC 238 ms
15,772 KB
testcase_33 AC 222 ms
15,772 KB
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

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<map<int, int> (int, int)> go = [&](int x, int parent) {
        int sum_one = 0;
        map<int, int> one;
        map<pair<int, int>, int> two;
        for (auto [y, c] : g[x]) if (y != parent) {
            auto sub = go(y, x);
            for (auto [d, k] : sub) if (d != c) {
                ans += (int64_t)k;
                ans += (int64_t)k * two[minmax({ c, d })];
                ans += (int64_t)k * (one.count(c) ? one[c] : 0);
                ans += (int64_t)k * (one.count(d) ? one[d] : 0);
                two[minmax({ c, d })] += k;
            }
            int k = 1 + (sub.count(c) ? sub[c] : 0);
            ans += (int64_t)k * (sum_one - (one.count(c) ? one[c] : 0));
            one[c] += k;
            sum_one += k;
        }
        return one;
    };
    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