結果

問題 No.1843 Tree ANDistance
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2022-02-19 00:57:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,516 bytes
コンパイル時間 2,203 ms
コンパイル使用メモリ 208,260 KB
実行使用メモリ 23,376 KB
最終ジャッジ日時 2023-09-11 20:22:32
合計ジャッジ時間 11,020 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 2 ms
4,380 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 254 ms
13,280 KB
testcase_29 AC 145 ms
9,968 KB
testcase_30 AC 129 ms
12,256 KB
testcase_31 AC 256 ms
18,708 KB
testcase_32 AC 266 ms
13,844 KB
testcase_33 AC 44 ms
6,740 KB
testcase_34 WA -
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
void tprsort(VVi& gr, Vi& tpr, Vi& par, int s) {
    for (auto v : gr[s]) {
        if (v == par[s]) {
            continue;
        }
        par[v] = s;
        tprsort(gr, tpr, par, v);
    }
    tpr.push_back(s);
}
int main () {
    ll m = 1e9 + 7;
    ll f2 = (m + 1) / 2;
    int N;
    cin >> N;
    VVi tree(N);
    VVl dist(N);
    for (int i = 1; i < N; i ++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        a --;
        b --;
        tree[a].push_back(b);
        tree[b].push_back(a);
        dist[a].push_back(c);
        dist[b].push_back(c);
    }
    Vi tpr, par(N, -1);
    tprsort(tree, tpr, par, 0);
    ll ans = 0;
    for (int i_ = 0; i_ < 30; i_ ++) {
        Vl dp(N);
        ll kj = 0;
        for (auto u : tpr) {
            dp[u] = 1;
            ll sqsum = 1;
            for (int i = 0; i < tree[u].size(); i ++) {
                int v = tree[u][i];
                ll c = dist[u][i];
                if (v == par[u]) {
                    continue;
                }
                if ((c >> i_) & 1) {
                    dp[u] += dp[v];
                    sqsum += (dp[v] * dp[v]) % m;
                }
            }
            kj += ((((dp[u] * dp[u]) % m - sqsum + m) % m) * f2) % m;
            kj %= m;
        }
        ans += (kj * ((1 << i_) % m)) % m;
    }
    cout << ans << endl;
}
0