結果

問題 No.1843 Tree ANDistance
ユーザー umezoumezo
提出日時 2022-02-18 23:40:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,388 bytes
コンパイル時間 2,105 ms
コンパイル使用メモリ 205,532 KB
実行使用メモリ 7,912 KB
最終ジャッジ日時 2023-09-11 20:11:21
合計ジャッジ時間 6,666 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 145 ms
7,720 KB
testcase_01 AC 144 ms
7,812 KB
testcase_02 AC 136 ms
7,856 KB
testcase_03 AC 136 ms
7,912 KB
testcase_04 AC 144 ms
7,760 KB
testcase_05 AC 117 ms
7,764 KB
testcase_06 AC 136 ms
7,764 KB
testcase_07 AC 135 ms
7,656 KB
testcase_08 AC 138 ms
7,740 KB
testcase_09 AC 129 ms
7,604 KB
testcase_10 AC 126 ms
7,504 KB
testcase_11 AC 143 ms
7,896 KB
testcase_12 AC 106 ms
7,516 KB
testcase_13 AC 128 ms
7,516 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 55 ms
6,884 KB
testcase_29 AC 37 ms
5,380 KB
testcase_30 AC 36 ms
5,652 KB
testcase_31 AC 60 ms
7,148 KB
testcase_32 AC 58 ms
6,808 KB
testcase_33 AC 18 ms
4,448 KB
testcase_34 AC 44 ms
6,292 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

class Dis{
  public:
  vector<ll> rank,p,siz;
  
  Dis(int s){
    rank.resize(s,0);
    p.resize(s,0);
    siz.resize(s,1);
    rep(i,s) makeSet(i);
  }
  
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
  }
  
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
  
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
  
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      siz[x]+=siz[y];
    }
    else{
      p[x]=y;
      siz[y]+=siz[x];
      if(rank[x]==rank[y]) rank[y]++;
    }
  }
  
  int findSet(int x){
    if(x != p[x]) p[x]=findSet(p[x]);
    return p[x];
  }
  
  int size(int x){
    return siz[findSet(x)];
  }
};

const int MOD=1e9+7;
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n;
  cin>>n;
  vector<ll> A(n-1),B(n-1),C(n-1);
  rep(i,n-1){
    cin>>A[i]>>B[i]>>C[i];
    A[i]--,B[i]--;
  }
  
  ll ans=0;
  ll c=1;
  rep(i,31){
    Dis ds=Dis(n);
    rep(j,n-1){
      if(C[j]&(1<<i)) ds.unite(A[j],B[j]);
    }
    ll sum=0;
    rep(j,n){
      if(ds.findSet(j)==j){
        ll t=ds.size(j);
        sum+=t*(t-1)/2;
      }
    }
    ans=(ans+c*sum%MOD)%MOD;
    c=c*2%MOD;
  }
  cout<<ans<<endl;
  
  return 0;
}
0