結果

問題 No.1843 Tree ANDistance
ユーザー anmichianmichi
提出日時 2022-02-18 21:35:24
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 450 ms / 2,000 ms
コード長 3,500 bytes
コンパイル時間 2,402 ms
コンパイル使用メモリ 211,188 KB
実行使用メモリ 38,540 KB
最終ジャッジ日時 2023-09-11 18:40:08
合計ジャッジ時間 11,104 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 434 ms
38,360 KB
testcase_01 AC 407 ms
38,364 KB
testcase_02 AC 444 ms
38,368 KB
testcase_03 AC 444 ms
38,540 KB
testcase_04 AC 435 ms
38,380 KB
testcase_05 AC 323 ms
38,540 KB
testcase_06 AC 353 ms
38,476 KB
testcase_07 AC 436 ms
36,540 KB
testcase_08 AC 440 ms
37,660 KB
testcase_09 AC 422 ms
36,604 KB
testcase_10 AC 413 ms
35,676 KB
testcase_11 AC 450 ms
37,796 KB
testcase_12 AC 300 ms
34,984 KB
testcase_13 AC 319 ms
35,508 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 7 ms
4,636 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 69 ms
30,384 KB
testcase_29 AC 47 ms
21,688 KB
testcase_30 AC 41 ms
22,056 KB
testcase_31 AC 73 ms
32,764 KB
testcase_32 AC 70 ms
30,924 KB
testcase_33 AC 21 ms
12,548 KB
testcase_34 AC 55 ms
25,444 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,384 KB
testcase_37 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
template <class T, class U>
inline bool chmax(T& a, U b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
inline bool chmin(T& a, U b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
constexpr int INF = 1000000000;
constexpr ll llINF = 3000000000000000000;
constexpr int mod = 1000000007;
const ll half = (mod + 1) / 2;
constexpr double eps = 1e-10;
vector<int> prime_list(int n) {
    vector<int> v(n + 1), res;
    for (int i = n; i >= 2; i--) {
        for (int j = i; j <= n; j += i) v[j] = i;
    }
    for (int i = 2; i <= n; i++) {
        if (v[i] == i) res.push_back(i);
    }
    return res;
}
vector<int> next_divisor(int n) {
    vector<int> v(n + 1);
    for (int i = n; i >= 2; i--) {
        for (int j = i; j <= n; j += i) v[j] = i;
    }
    return v;
}
ll modpow(ll a, ll b, ll m = mod) {
    ll res = 1;
    while (b) {
        if (b & 1) {
            res *= a;
            res %= m;
        }
        a *= a;
        a %= m;
        b >>= 1;
    }
    return res;
}
vector<ll> inv, fact, factinv;
void init_fact(int n) {
    inv.resize(n + 1);
    fact.resize(n + 1);
    factinv.resize(n + 1);
    inv[0] = 0;
    inv[1] = 1;
    fact[0] = 1;
    factinv[0] = 1;
    for (ll i = 1; i <= n; i++) {
        if (i >= 2) inv[i] = mod - ((mod / i) * inv[mod % i] % mod);
        fact[i] = (fact[i - 1] * i) % mod;
        factinv[i] = factinv[i - 1] * inv[i] % mod;
    }
}
ll modinv(ll a, ll m = mod) {
    // gcd(a,m) must be 1
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
ll comb(int a, int b) {
    if (a < b || a < 0 || b < 0) return 0;
    return fact[a] * factinv[a - b] % mod * factinv[b] % mod;
}
struct UnionFind {
    vector<int> par, siz, es;
    UnionFind(int x) {
        par.resize(x);
        siz.resize(x);
        es.resize(x);
        for (int i = 0; i < x; i++) {
            par[i] = i;
            siz[i] = 1;
            es[i] = 0;
        }
    }
    int find(int x) {
        if (par[x] == x) return x;
        return par[x] = find(par[x]);
    }
    void unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) {
            es[x]++;
            return;
        }
        if (siz[x] < siz[y]) swap(x, y);
        par[y] = x;
        siz[x] += siz[y];
        es[x] += es[y] + 1;
    }
    bool same(int x, int y) { return find(x) == find(y); }
    int size(int x) { return siz[find(x)]; }
    int edges(int x) { return es[find(x)]; }
};
void solve() {
    int n;
    cin >> n;
    vector<UnionFind> uf;
    rep(i, 30) uf.push_back(UnionFind(n));
    rep(i, n - 1) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        rep(j, 30) {
            if ((c >> j) & 1) {
                uf[j].unite(a, b);
            }
        }
    }
    ll ans = 0;
    rep(i, 30) {
        rep(j, n) {
            if (uf[i].find(j) == j) {
                ans += (1LL * uf[i].size(j) * (uf[i].size(j) - 1) / 2) * (1 << i) % mod;
                ans %= mod;
            }
        }
    }
    cout << ans << endl;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    /*int t;
    cin >> t;
    while (t--) */
    solve();
}
0