結果

問題 No.1843 Tree ANDistance
ユーザー 杨娵隅杨娵隅
提出日時 2022-02-19 00:50:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 2,064 bytes
コンパイル時間 2,134 ms
コンパイル使用メモリ 205,532 KB
実行使用メモリ 9,464 KB
最終ジャッジ日時 2023-09-11 20:22:06
合計ジャッジ時間 5,525 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
9,464 KB
testcase_01 AC 85 ms
9,424 KB
testcase_02 AC 82 ms
9,344 KB
testcase_03 AC 81 ms
9,332 KB
testcase_04 AC 87 ms
9,264 KB
testcase_05 AC 75 ms
9,412 KB
testcase_06 AC 95 ms
9,368 KB
testcase_07 AC 81 ms
9,220 KB
testcase_08 AC 84 ms
9,136 KB
testcase_09 AC 77 ms
8,896 KB
testcase_10 AC 76 ms
8,860 KB
testcase_11 AC 85 ms
9,220 KB
testcase_12 AC 67 ms
9,248 KB
testcase_13 AC 84 ms
8,824 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 35 ms
7,972 KB
testcase_29 AC 24 ms
6,236 KB
testcase_30 AC 23 ms
6,496 KB
testcase_31 AC 37 ms
8,360 KB
testcase_32 AC 35 ms
8,252 KB
testcase_33 AC 12 ms
4,736 KB
testcase_34 AC 28 ms
7,012 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 #

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

struct dsu
{
  vector<int> fa, sz;

  dsu(int n) : fa(n), sz(n, 1){iota(fa.begin(), fa.end(), 0);}
  int find(int x){while (x != fa[x]) x = fa[x] = fa[fa[x]]; return x;}
  bool same(int x, int y){return find(x) ==find(y);}
  bool merge(int x, int y)
  {
    x = find(x); y = find(y);
    if (x == y) return false;
    sz[x] += sz[y];
    fa[y] = x;
    return true;
  }
  int size(int x){return sz[find(x)];}
};

constexpr int P = 1000000007;
// assume -P <= x < 2P
int norm(int x) 
{
  if (x < 0) x += P;
  if (x >= P) x -= P;
  return x;
}
template<class T>
T qpow(T a, int b) 
{
  T res = 1;
  for (; b; b /= 2, a *= a) 
  {
    if (b % 2) res *= a;
  }
  return res;
}
struct Z
{
  int x;
  Z(int x = 0) : x(norm(x)){}
  int val()const{return x;}
  Z operator - ()const{return Z(norm(P - x));}
  Z inv()const {assert(x != 0); return qpow(*this, P - 2);}
  Z &operator *= (const Z &rhs){x = x * rhs.x % P; return *this;}
  Z &operator += (const Z &rhs){x = norm(x + rhs.x); return *this;}
  Z &operator -= (const Z &rhs){x = norm(x - rhs.x); return *this;}
  Z &operator /= (const Z &rhs){return *this *= rhs.inv();}
  friend Z operator * (const Z &lhs, const Z &rhs){Z ret = lhs; ret *= rhs; return ret;}
  friend Z operator + (const Z &lhs, const Z &rhs){Z ret = lhs; ret += rhs; return ret;}
  friend Z operator - (const Z &lhs, const Z &rhs){Z ret = lhs; ret -= rhs; return ret;}
  friend Z operator / (const Z &lhs, const Z &rhs){Z ret = lhs; ret /= rhs; return ret;}
};

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

  int n; cin >> n;
  vector<array<int, 3>> g(n - 1);
  for (int i = 1; i < n; i ++)
  {
    int u, v, w; cin >> u >> v >> w;
    u --; v --;
    g.push_back(array<int, 3>{u, v, w});
  }
  Z ans = 0;
  for (int i = 30; i --; )
  {
    ans += ans;
    dsu d(n);
    for (auto [u, v, w] : g)
    {
      if (w >> i & 1)
      {
        ans += d.size(u) * d.size(v);
        d.merge(u, v);
      }
    }
  }
  cout << ans.val() << "\n";

  return 0;
}
0