結果

問題 No.1002 Twotone
ユーザー ferinferin
提出日時 2020-03-03 00:43:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,590 ms / 5,000 ms
コード長 5,292 bytes
コンパイル時間 2,741 ms
コンパイル使用メモリ 224,176 KB
実行使用メモリ 40,312 KB
最終ジャッジ日時 2023-08-04 00:24:34
合計ジャッジ時間 21,574 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 267 ms
17,020 KB
testcase_04 AC 369 ms
21,260 KB
testcase_05 AC 348 ms
21,396 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 369 ms
22,060 KB
testcase_08 AC 689 ms
33,340 KB
testcase_09 AC 703 ms
33,200 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 762 ms
28,624 KB
testcase_12 AC 1,034 ms
33,968 KB
testcase_13 AC 1,037 ms
34,016 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 569 ms
20,112 KB
testcase_16 AC 1,157 ms
34,116 KB
testcase_17 AC 1,135 ms
33,696 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 647 ms
22,956 KB
testcase_20 AC 787 ms
29,072 KB
testcase_21 AC 785 ms
28,984 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 822 ms
25,788 KB
testcase_24 AC 1,590 ms
40,312 KB
testcase_25 AC 1,491 ms
40,180 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 109 ms
30,184 KB
testcase_28 AC 187 ms
38,892 KB
testcase_29 AC 168 ms
38,620 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 166 ms
39,624 KB
testcase_32 AC 192 ms
39,152 KB
testcase_33 AC 173 ms
39,036 KB
testcase_34 AC 1,006 ms
38,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

int main(void) {
    ll n, k;
    cin >> n >> k;
    vector<vector<PII>> g(n);
    REP(i, n-1) {
        ll u, v, c;
        cin >> u >> v >> c;
        u--, v--, c--;
        g[u].push_back({v, c});
        g[v].push_back({u, c});
    }

    vector<ll> sz(n), dead(n);
    auto find_centroid = [&](ll root) {
        auto get_size = [&](auto &&self, ll v, ll p) -> void {
            sz[v] = 1;
            for(auto to: g[v]) if(to.first != p && !dead[to.first]) {
                self(self, to.first, v);
                sz[v] += sz[to.first];
            }
        };
        get_size(get_size, root, -1);
        auto dfs = [&](auto &&self, ll v, ll p) -> ll {
            for(auto to: g[v]) if(to.first != p && !dead[to.first]) {
                if(sz[to.first] > sz[root]/2) return self(self, to.first, v);
            }
            return v;
        };
        return dfs(dfs, root, -1);
    };

    auto get_edges = [&](ll v) -> vector<vector<PII>> {
        vector<vector<PII>> ret;

        auto dfs = [&](auto &&self, ll v, ll p, PII col) -> void {
            for(auto to: g[v]) if(to.first != p && !dead[to.first]) {                
                PII now = col;
                if(to.second == now.first || to.second == now.second) {
                    ret.back().push_back(now);
                    self(self, to.first, v, now);
                } else if(now.first == -1) {
                    now.first = to.second;
                    if(now.first > now.second) swap(now.first, now.second);
                    ret.back().push_back(now);
                    self(self, to.first, v, now);
                }
            }
        };

        for(auto to: g[v]) if(!dead[to.first]) {
            ret.emplace_back();
            ret.back().emplace_back(-1, to.second);
            dfs(dfs, to.first, -1, PII(-1, to.second));
            sort(ALL(ret.back()));
        }
        return ret;
    };

    ll ans = 0;
    auto centroid_decomposition = [&](auto &&self, ll root) -> void {
        ll c = find_centroid(root);
        dead[c] = true;
        for(auto to: g[c]) if(!dead[to.first]) self(self, to.first);

        // (色1,色2) を含むパスとマージさせられるパスは (色1,色2) or (-1,色1) or (-1,色2) のどれか
        // (色1,色2) のペアが何個あるのか数えるための用意
        auto colors = get_edges(c);
        vector<PII> flat;
        for(auto v: colors) for(auto p: v) flat.push_back(p);
        sort(ALL(flat));
        dump(c);
        dump(colors);
        dump(flat);

        for(auto v: colors) for(auto p: v) {
            dump(p, ans);
            // 2色存在するペアと合わせられるものを数える
            if(p.first != -1 && p.second != -1) {
                // 頂点cまでのパスを数える
                ans += 2;
                // 同じ色のペア
                {
                    auto [lb, ub] = equal_range(ALL(flat), p);
                    ans += ub - lb;
                    auto [lb2, ub2] = equal_range(ALL(v), p);
                    ans -= ub2 - lb2; // 同じ方向のパスから2回選ぶのはだめ
                }
                // (-1,色1)
                {
                    auto [lb, ub] = equal_range(ALL(flat), PII(-1, p.first));
                    ans += 2 * (ub - lb);   // p=(-1,色1) の分をここで足しとく
                    auto [lb2, ub2] = equal_range(ALL(v), PII(-1, p.first));
                    ans -= 2 * (ub2 - lb2);
                }
                // (-1,色2)
                {
                    auto [lb, ub] = equal_range(ALL(flat), PII(-1, p.second));
                    ans += 2 * (ub - lb);
                    auto [lb2, ub2] = equal_range(ALL(v), PII(-1, p.second));
                    ans -= 2 * (ub2 - lb2);
                }
            } 
            // (-1, 色1) と合わせられるやつを探す
            else {
                // (-1, 色i) (色i!=色1) 
                {
                    auto ub = upper_bound(ALL(flat), PII(-1, INF));
                    auto lb = lower_bound(ALL(flat), PII(-1, 0));
                    ans += ub - lb;
                    auto ub2 = upper_bound(ALL(v), PII(-1, INF));
                    auto lb2 = lower_bound(ALL(v), PII(-1, 0));
                    ans -= ub2 - lb2;
                    auto [lb3, ub3] = equal_range(ALL(flat), PII(-1, p.second));
                    ans -= ub3 - lb3;
                    auto [lb4, ub4] = equal_range(ALL(v), PII(-1, p.second));
                    ans += ub4 - lb4;
                }
            }
            dump(ans);
        }

        dead[c] = false;
    };

    centroid_decomposition(centroid_decomposition, 0);
    cout << ans/2 << endl;

    return 0;
}
0