結果

問題 No.1002 Twotone
ユーザー pockynypockyny
提出日時 2024-01-09 03:14:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,250 bytes
コンパイル時間 1,851 ms
コンパイル使用メモリ 110,908 KB
実行使用メモリ 69,232 KB
最終ジャッジ日時 2024-01-09 03:15:12
合計ジャッジ時間 28,829 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
6,676 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1 ms
6,676 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
6,676 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
6,676 KB
testcase_27 AC 204 ms
53,896 KB
testcase_28 AC 393 ms
69,232 KB
testcase_29 AC 328 ms
69,232 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 321 ms
68,712 KB
testcase_32 AC 372 ms
69,104 KB
testcase_33 AC 351 ms
69,104 KB
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://onlinejudge.u-aizu.ac.jp/beta/review.html#OUPC2023Day2/8737708

#include <iostream>
#include <vector>
#include <map>

using namespace std;
struct CentroidDecomposition{
    // 重心分解後の木の深さ
    vector<int> rank;
    // 部分木のサイズ
    vector<int> sz;
    // 元のグラフ
    vector<vector<int>> g;
    // 重心分解後の木におけるグラフの親
    vector<int> par;
    // 重心分解後の木におけるグラフの子 (あまり使わないらしいが一応持つ)
    vector<vector<int>> child;
    int n;
    CentroidDecomposition(vector<vector<int>> &g) : n(g.size()), g(g), sz(g.size()), rank(g.size(), -1), par(g.size(), -1), child(g.size()) {
        _build(0,-1);
    }
    // 部分木のサイズを計算
    void calc_size(int v, int p) {
        sz[v] = 1;
        for(int to : g[v]) {
            if(rank[to] == -1 && to != p) {
                calc_size(to, v);
                sz[v] += sz[to];
            }
        }
    }

    void _build(int v, int p) {
        calc_size(v, -1);
        int tot = sz[v];
        bool ok = false;
        int pp = -1;
        // いま見ている部分木での重心を見つける
        while(!ok) {
            ok = true;
            for(int to : g[v]) {
                if(rank[to] == -1 && to != pp && 2 * sz[to] > tot) {
                    pp = v, v = to, ok = false;
                    break;
                }
            }
        }
        par[v] = p;
        if(p != -1) {
            child[p].push_back(v);
            rank[v] = rank[p] + 1;
        } else {
            rank[v] = 0;
        }
        // 深さ優先でたどる
        for(int to : g[v]) {
            if(rank[to] == -1) {
                _build(to, v);
            }
        }
    }
};

typedef long long ll;
int main(){
    int i,n,k; cin >> n >> k;
    vector<vector<int>> g(n);
    vector<vector<pair<int,int>>> G(n);
    for(i=0;i<n - 1;i++){
        int u,v,c; cin >> u >> v >> c;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
        G[u].push_back({v,c});
        G[v].push_back({u,c});
    }
    CentroidDecomposition cd(g);
    // for(i=0;i<n;i++) cout << cd.rank[i] << " ";
    // cout << "\n";
    ll ans = 0;
    for(i=0;i<n;i++){
        int r = cd.rank[i];
        vector<map<ll,ll>> vec;
        // bool deb = i==0 && false;
        auto dfs = [&](auto &&self,int v,int p,map<int,int> &mp,int id) -> void {
            // if(deb){
            //     cout << "now = " << v << " " << mp.size() << " " << ans << "\n";
            //     for(auto p:mp) cout << p.first << " " << p.second << "\n";
            // }
            if(mp.size()>=3) return;
            // i->v
            if(mp.size()==2) ans++;
            if(mp.size()==1) {
                vec[id][(*mp.begin()).first]++;
            }
            for(auto e:G[v]){
                if(e.first==p || cd.rank[e.first]<r) continue;
                mp[e.second]++;
                self(self,e.first,v,mp,id);
                mp[e.second]--;
                if(!mp[e.second]) mp.erase(e.second);
            }
        };
        int id = 0;
        for(auto e:G[i]){
            if(cd.rank[e.first]<r) continue;
            map<int,int> mp1;
            mp1[e.second]++;
            vec.resize(id + 1);
            dfs(dfs,e.first,i,mp1,id);
            id++;
        }
        // cout << "i = " << i + 1 << " after_dfs " << ans << "\n";
        // u->i->v;
        ll val1 = 0,val2 = 0,val3 = 0,val4 = 0;
        map<ll,ll> mp2;
        int de = 0;
        for(auto mp:vec){
            ll sum3 = 0;
            de++;
            for(auto p:mp){
                // cout << "de = " << de << " " << p.first << " " << p.second << "\n";
                mp2[p.first] += p.second;
                val1 += p.second;
                sum3 += p.second;
                val4 += p.second*p.second;
            }
            val3 += sum3*sum3;
        }
        for(auto p:mp2) val2 += p.second*p.second;
        val1 *= val1;
        ans += (val1 - val2 - val3 + val4)/2;
        // cout << "i = " << i + 1 << " after_calc " << ans << "\n";
        // cout << val1 << " " << val2 << " " << val3 << " " << val4 << " " << vec.size() << "\n";
    }
    cout << ans << "\n";
}
0