結果

問題 No.1843 Tree ANDistance
ユーザー 蜜蜂
提出日時 2022-02-18 21:38:36
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 293 ms / 2,000 ms
コード長 1,393 bytes
コンパイル時間 4,235 ms
コンパイル使用メモリ 235,704 KB
実行使用メモリ 11,484 KB
最終ジャッジ日時 2024-06-29 08:29:30
合計ジャッジ時間 10,316 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 38
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:53:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   53 |       auto [a,b,c]=edge[i];
      |            ^

ソースコード

diff #

//g++ 1.cpp -std=c++14 -O2 -I .
#include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vst = vector<string>;
using vvst = vector<vst>;

#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define pq_big(T) priority_queue<T,vector<T>,less<T>>
#define pq_small(T) priority_queue<T,vector<T>,greater<T>>
#define all(a) a.begin(),a.end()
#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)
#define per(i,start,end) for(ll i=start;i>=(ll)(end);i--)
#define uniq(a) sort(all(a));a.erase(unique(all(a)),a.end())

constexpr ll mod = 1e9+7;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin>>n;

  vector<tuple<int,int,ll>> edge(n-1);
  rep(i,0,n-1){
    int a,b;
    ll c;
    cin>>a>>b>>c;
    a--;b--;
    edge[i]={a,b,c};
  }

  ll ans=0;
  rep(bt,0,32){
    dsu d(n);
    rep(i,0,n-1){
      auto [a,b,c]=edge[i];
      if(c&(1<<bt)){
        d.merge(a,b);
      }
    }

    ll pow2=(1<<bt);
    auto gr=d.groups();

    for(auto v:gr){
      ll sz=v.size();
      sz=(sz*(sz-1))/2;
      sz%=mod;
      sz*=pow2;
      sz%=mod;
      ans+=sz;
      ans%=mod;
    }
  }

  cout<<ans<<endl;
}
0