結果

問題 No.1843 Tree ANDistance
ユーザー chestnut_68chestnut_68
提出日時 2022-02-18 21:44:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 1,646 bytes
コンパイル時間 3,841 ms
コンパイル使用メモリ 230,160 KB
実行使用メモリ 7,196 KB
最終ジャッジ日時 2023-09-11 18:50:18
合計ジャッジ時間 8,595 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 170 ms
6,996 KB
testcase_01 AC 170 ms
6,996 KB
testcase_02 AC 165 ms
7,116 KB
testcase_03 AC 165 ms
7,112 KB
testcase_04 AC 167 ms
7,004 KB
testcase_05 AC 150 ms
7,004 KB
testcase_06 AC 170 ms
7,196 KB
testcase_07 AC 157 ms
6,764 KB
testcase_08 AC 170 ms
6,952 KB
testcase_09 AC 154 ms
6,796 KB
testcase_10 AC 151 ms
6,892 KB
testcase_11 AC 169 ms
7,100 KB
testcase_12 AC 138 ms
6,708 KB
testcase_13 AC 161 ms
6,964 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 4 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 7 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 83 ms
6,128 KB
testcase_29 AC 56 ms
5,272 KB
testcase_30 AC 53 ms
5,160 KB
testcase_31 AC 88 ms
6,444 KB
testcase_32 AC 85 ms
6,376 KB
testcase_33 AC 27 ms
4,380 KB
testcase_34 AC 67 ms
5,684 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep1(a)          for(int64_t i = 0; i < a; i++)
#define rep2(i, a)       for(int64_t i = 0; i < a; i++)
#define rep3(i, a, b)    for(int64_t i = a; i < b; i++)
#define rep4(i, a, b, c) for(int i = a; i < b; i += c)
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
const int64_t MOD=998244353;
const long long INF = 1LL<<60;

struct UnionFind {
  vector<int> par,siz; 
  UnionFind(int N):par(N),siz(N,1){
    for(int i=0;i<N;i++)par[i]=i;
  }
  void init(int N){
    par.resize(N);
    siz.assign(N,1);
    for(int i=0;i<N;++i)par[i]=i;
  }
  int root(int x){
    if(par[x]==x)return x;
    return par[x]=root(par[x]);
  }
  void unite(int x,int y){
    int rx=root(x);
    int ry=root(y);
    if(rx==ry)return;
    if(siz[rx]<siz[ry])swap(rx, ry);
    siz[rx]+=siz[ry];
    par[ry]=rx;
  }
  bool same(int x,int y){
    int rx=root(x);
    int ry=root(y);
    return rx==ry;
  }
  int size(int x){
    return siz[root(x)];
  }
};
int main(){
  int N;cin>>N;
  vector<int64_t> A(N),B(N),C(N);
  rep(i,N){
    cin>>A[i]>>B[i]>>C[i];
    A[i]--,B[i]--;
  }
  UnionFind T(N);
  mint ans=0,d=1;
  int64_t D=1;
  rep(i,31){
    //T.init(N);
    UnionFind T(N);
    rep(j,N){
      if(C[j]&D){
        T.unite(A[j],B[j]);
        //cout<<(1<<j)<<endl;
      }
    }
    rep(j,N){
      if(T.root(j)==j){
        int64_t s=T.size(j);
        ans+=d*(s*(s-1)/2);
        //cout<<ans.val()<<endl;
      }
    }
    d*=2;
    D*=2;
  }
  cout<<ans.val();
}
0