結果
| 問題 |
No.1843 Tree ANDistance
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-02-18 23:54:27 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 204 ms / 2,000 ms |
| コード長 | 2,348 bytes |
| コンパイル時間 | 1,700 ms |
| コンパイル使用メモリ | 175,132 KB |
| 実行使用メモリ | 7,296 KB |
| 最終ジャッジ日時 | 2024-06-29 09:58:33 |
| 合計ジャッジ時間 | 6,807 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 38 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;
struct UnionFind {
vector <ll> par; // 各元の親を表す配列
vector <ll> siz; // 素集合のサイズを表す配列(1 で初期化)
vector <ll> edge;
// Constructor
UnionFind(ll sz_): par(sz_), siz(sz_, 1LL), edge(sz_) {
for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y) {
edge[x]++;
return false;
}
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
edge[x] += edge[y] + 1;
par[y] = x;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
ll cnt_edge(ll x) {
return edge[root(x)];
}
};
int main() {
int N; cin >> N;
vector<pair<int,int>> edge(N-1);
vector<long long> C(N-1);
for(int i = 0; i < N-1; i++) {
int a,b; cin >> a >> b;
a--,b--;
edge[i] = {a,b};
cin >> C[i];
}
long long ans = 0;
for(int i = 0; i < 40; i++) {
UnionFind UT(N);
for(int j = 0; j < N-1; j++) {
if(C[j] & (1LL<<i)) {
UT.merge(edge[j].first,edge[j].second);
}
}
long long S = 0;
for(int v = 0; v < N; v++) {
if(v == UT.root(v)) {
int r = UT.root(v);
S += UT.size(r)*(UT.size(r)-1)/2;
S %= MOD;
}
}
//cout << S << endl;
ans += (1LL<<i)%MOD*S;
ans %= MOD;
}
cout << ans << endl;
}