結果

問題 No.1002 Twotone
ユーザー kyort0nkyort0n
提出日時 2020-02-28 22:58:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,723 ms / 5,000 ms
コード長 4,139 bytes
コンパイル時間 2,292 ms
コンパイル使用メモリ 196,264 KB
実行使用メモリ 143,240 KB
最終ジャッジ日時 2023-08-03 21:41:04
合計ジャッジ時間 24,389 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,184 KB
testcase_01 AC 3 ms
8,156 KB
testcase_02 AC 3 ms
8,000 KB
testcase_03 AC 494 ms
17,472 KB
testcase_04 AC 659 ms
20,320 KB
testcase_05 AC 660 ms
20,296 KB
testcase_06 AC 4 ms
8,220 KB
testcase_07 AC 305 ms
15,580 KB
testcase_08 AC 553 ms
19,868 KB
testcase_09 AC 551 ms
20,032 KB
testcase_10 AC 3 ms
8,004 KB
testcase_11 AC 661 ms
19,544 KB
testcase_12 AC 911 ms
22,312 KB
testcase_13 AC 935 ms
22,496 KB
testcase_14 AC 3 ms
8,232 KB
testcase_15 AC 502 ms
15,592 KB
testcase_16 AC 1,019 ms
21,332 KB
testcase_17 AC 1,010 ms
21,068 KB
testcase_18 AC 3 ms
8,248 KB
testcase_19 AC 728 ms
26,272 KB
testcase_20 AC 897 ms
35,600 KB
testcase_21 AC 900 ms
34,748 KB
testcase_22 AC 4 ms
8,200 KB
testcase_23 AC 918 ms
39,908 KB
testcase_24 AC 1,601 ms
58,872 KB
testcase_25 AC 1,575 ms
58,956 KB
testcase_26 AC 4 ms
8,136 KB
testcase_27 AC 133 ms
48,368 KB
testcase_28 AC 209 ms
57,032 KB
testcase_29 AC 198 ms
57,856 KB
testcase_30 AC 3 ms
8,180 KB
testcase_31 AC 190 ms
56,176 KB
testcase_32 AC 210 ms
56,652 KB
testcase_33 AC 195 ms
56,656 KB
testcase_34 AC 3,723 ms
143,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
//const ll mod = 1000000007;
ll N, K;
vector<l_l> paths[200100];
vector<bool> Del;
ll SIZE[200100];
void print(set<ll> st) {
    cerr << "{";
    for(auto val : st) {
        cerr << val << " ";
    }
    cerr << "}";
}

void dfs(int from, int now) {
    SIZE[now] = 1;
    for(auto tmp : paths[now]) {
        int to = tmp.first;
        if(to == from) continue;
        if(Del[to]) continue;
        dfs(now, to);
        SIZE[now] += SIZE[to];
    }
}

int centroid(int now, int sz) {
    for(auto tmp : paths[now]) {
        int to = tmp.first;
        if(Del[to]) continue;
        if(SIZE[to] > SIZE[now]) continue;
        if(SIZE[to] * 2 >= sz) return centroid(to, sz);
    }
    return now;
}

ll ans = 0;

void g(int now, int from, map<set<ll>, ll> &mp, set<ll> st) {
    /*
    cerr << from << " -> " << now << " ";
    print(st);
    cerr << endl;
    */
    mp[st]++;
    for(auto tmp : paths[now]) {
        int to = tmp.first;
        if(Del[to]) continue;
        if(to == from) continue;
        set<ll> st2 = st;
        st2.insert(tmp.second);
        if(st2.size() >= 3) continue;
        g(to, now, mp, st2);
    }
}


void f(int now) {
    dfs(-1, now);
    if(SIZE[now] == 1) return;
    int c = centroid(now, SIZE[now]);
    vector<map<set<ll>, ll>> V;
    map<set<ll>, ll> Total;
    for(auto tmp : paths[c]) {
        int to = tmp.first;
        if(Del[to]) continue;
        V.push_back({});
        g(to, c, V.back(), {tmp.second});
        for(auto tmp : V.back()) {
            Total[tmp.first] += tmp.second;
        }
    }
    ll OneNum = 0;
    ll OneAns = 0;
    map<set<ll>, ll> TwoSum;
    for(auto tmp : Total) {
        /*
        print(tmp.first);
        cerr << " " << tmp.second << endl;
        */
        if(tmp.first.size() == 1) {
            OneNum += tmp.second;
            OneAns -= tmp.second * tmp.second;
        } else {
            //TwoSquare += tmp.second * tmp.second;
            TwoSum[tmp.first] += tmp.second * tmp.second;
            //ans += tmp.second * (tmp.second + 1);
            ans += tmp.second;
            for(auto tmp2 : tmp.first) {
                set<ll> st = {tmp2};
                auto itr = Total.lower_bound(st);
                if((*itr).first == st) {
                    ans += (*itr).second * tmp.second;
                }
            }
        }
        //cerr << ans << endl;
    }
    //cerr << "end: Total" << endl;
    for(auto now : V) {
        for(auto tmp : now) {
            if(tmp.first.size() == 1) {
            } else {
                //ans -= tmp.second * tmp.second;
                TwoSum[tmp.first] -= tmp.second * tmp.second;
                for(auto tmp2 : tmp.first) {
                    set<ll> st = {tmp2};
                    auto itr = now.lower_bound(st);
                    if((*itr).first == st) {
                        ans -= (*itr).second * tmp.second;
                   }
                }
            }
        }
        //cerr << ans << endl;
    }
    OneAns += OneNum * OneNum;
    ans += OneAns / 2;
    for(auto tmp : TwoSum) {
        ans += tmp.second / 2;
    }
    Del[c] = true;
    //cerr << "-------------" << c << " " << ans << "--------" << endl;
    for(auto tmp : paths[c]) {
        int to = tmp.first;
        if(Del[to]) continue;
        f(to);
    }
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> K;
    Del.resize(N);
    for(int i = 0; i < N - 1; i++) {
        ll u, v, c;
        cin >> u >> v >> c;
        u--;
        v--;
        paths[u].push_back({v, c});
        paths[v].push_back({u, c});
    }
    f(0);
    cout << ans << endl;
    return 0;
}
0