結果

問題 No.1002 Twotone
ユーザー kyort0nkyort0n
提出日時 2020-02-28 23:55:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,899 ms / 5,000 ms
コード長 4,139 bytes
コンパイル時間 2,535 ms
コンパイル使用メモリ 194,724 KB
実行使用メモリ 142,804 KB
最終ジャッジ日時 2024-04-21 21:06:03
合計ジャッジ時間 25,087 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,320 KB
testcase_01 AC 5 ms
8,320 KB
testcase_02 AC 4 ms
8,236 KB
testcase_03 AC 520 ms
17,572 KB
testcase_04 AC 678 ms
20,480 KB
testcase_05 AC 678 ms
20,352 KB
testcase_06 AC 4 ms
8,064 KB
testcase_07 AC 325 ms
15,488 KB
testcase_08 AC 579 ms
19,968 KB
testcase_09 AC 579 ms
20,164 KB
testcase_10 AC 4 ms
8,192 KB
testcase_11 AC 685 ms
19,456 KB
testcase_12 AC 956 ms
22,528 KB
testcase_13 AC 966 ms
22,400 KB
testcase_14 AC 4 ms
8,192 KB
testcase_15 AC 519 ms
15,744 KB
testcase_16 AC 1,047 ms
21,120 KB
testcase_17 AC 1,046 ms
21,120 KB
testcase_18 AC 5 ms
8,192 KB
testcase_19 AC 732 ms
26,240 KB
testcase_20 AC 894 ms
35,424 KB
testcase_21 AC 907 ms
34,944 KB
testcase_22 AC 5 ms
8,064 KB
testcase_23 AC 919 ms
39,168 KB
testcase_24 AC 1,720 ms
57,600 KB
testcase_25 AC 1,755 ms
57,344 KB
testcase_26 AC 4 ms
8,192 KB
testcase_27 AC 143 ms
46,180 KB
testcase_28 AC 241 ms
56,512 KB
testcase_29 AC 220 ms
56,524 KB
testcase_30 AC 4 ms
8,064 KB
testcase_31 AC 210 ms
56,152 KB
testcase_32 AC 244 ms
56,328 KB
testcase_33 AC 216 ms
56,208 KB
testcase_34 AC 3,899 ms
142,804 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