結果

問題 No.1843 Tree ANDistance
ユーザー kai4096_donkai4096_don
提出日時 2022-02-18 21:44:29
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 605 ms / 2,000 ms
コード長 4,021 bytes
コンパイル時間 4,137 ms
コンパイル使用メモリ 238,460 KB
実行使用メモリ 30,604 KB
最終ジャッジ日時 2023-09-11 18:49:45
合計ジャッジ時間 17,034 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 518 ms
30,600 KB
testcase_01 AC 511 ms
30,604 KB
testcase_02 AC 509 ms
30,580 KB
testcase_03 AC 507 ms
30,552 KB
testcase_04 AC 514 ms
30,512 KB
testcase_05 AC 508 ms
30,600 KB
testcase_06 AC 518 ms
30,572 KB
testcase_07 AC 489 ms
29,232 KB
testcase_08 AC 499 ms
30,112 KB
testcase_09 AC 480 ms
29,492 KB
testcase_10 AC 472 ms
28,536 KB
testcase_11 AC 508 ms
30,240 KB
testcase_12 AC 458 ms
27,936 KB
testcase_13 AC 475 ms
28,420 KB
testcase_14 AC 18 ms
4,376 KB
testcase_15 AC 19 ms
4,376 KB
testcase_16 AC 9 ms
4,376 KB
testcase_17 AC 9 ms
4,376 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 22 ms
4,376 KB
testcase_20 AC 11 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 553 ms
24,472 KB
testcase_29 AC 373 ms
17,564 KB
testcase_30 AC 387 ms
17,812 KB
testcase_31 AC 605 ms
26,352 KB
testcase_32 AC 566 ms
25,016 KB
testcase_33 AC 195 ms
10,576 KB
testcase_34 AC 447 ms
20,520 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
const long nPrime = 1000000007;
//const long nPrime = 998244353;
typedef long long ll;
struct UnionFind {  
    vector<int> size, parents;
    UnionFind() {}
    UnionFind(int n) {
        size.resize(n, 1);
        parents.resize(n, 0);
        for (int i = 0; i < n; i++) {
            MakeTree(i);
        }
    }
    void MakeTree(int x) {
        parents[x] = x;
    }
    bool IsSame(int x, int y) { return FindRoot(x) == FindRoot(y); }
    void Unite(int x, int y) {
        x = FindRoot(x);
        y = FindRoot(y);
        if(x==y){
            return;
        }
        if (size[x] > size[y]) {
            parents[y] = x;
            size[x] += size[y];
            size[y] = -1;
        } else {
            parents[x] = y;
            size[y] += size[x];
            size[x] = -1;
        }
    }
    int FindRoot(int x) {
        if (x != parents[x]) parents[x] = FindRoot(parents[x]);
        return parents[x];
    }
    int GetSize(int x){
        return size[FindRoot(x)];
    }
};
const long long nMod = nPrime;
class ModInt {
    long long x;
public:
    ModInt(long long x=0) : x((x%nMod+nMod)%nMod) {}
    ModInt operator-() const { 
      return ModInt(-x);
    }
    ModInt& operator+=(const ModInt& a) {
        if ((x += a.x) >= nMod) x -= nMod;
        return *this;
    }
    ModInt& operator-=(const ModInt& a) {
        if ((x += nMod-a.x) >= nMod) x -= nMod;
        return *this;
    }
    ModInt& operator*=(const  ModInt& a) {
        (x *= a.x) %= nMod;
        return *this;
    }
    ModInt operator+(const ModInt& a) const {
        ModInt res(*this);
        return res+=a;
    }
    ModInt operator-(const ModInt& a) const {
        ModInt res(*this);
        return res-=a;
    }
    ModInt operator*(const ModInt& a) const {
        ModInt res(*this);
        return res*=a;
    }
    ModInt Pow(long long t) const {
        if (!t) return 1;
        ModInt a = Pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime nMod
    ModInt Inv() const {
        return Pow(nMod-2);
    }
    ModInt& operator/=(const ModInt& a) {
        return (*this) *= a.Inv();
    }
    ModInt operator/(const ModInt& a) const {
        ModInt res(*this);
        return res/=a;
    }

    friend ostream& operator<<(ostream& os, const ModInt& m){
        os << m.x;
        return os;
    }
};

void MakeTable(vector<ModInt> &viInv, vector<ModInt> &viFctr,
               vector<ModInt> &viFctrInv, bool bUseFctr = false,
               const long nTableSize = 1000000){
    
    viInv.resize(nTableSize+1, 1);
    for(long i = 1; i < nTableSize+1; i++){
        viInv[i] /= i;            
    }
    if(!bUseFctr){
        return;
    }
  
    viFctr.resize(nTableSize+1, 1);
    viFctrInv.resize(nTableSize+1, 1);
    for(long i = 1; i < nTableSize+1; i++){
        viFctr[i] = viFctr[i-1] * i;         
    }
    viFctrInv[nTableSize] /= viFctr[nTableSize];
    for(long i = nTableSize; i >= 1; i--){
        viFctrInv[i-1] = viFctrInv[i]*i;
    }
}
int main() {
    long n;
    cin >> n;
    vector<pair<pair<long,long>,long>> viEdge(n-1);
    for(long i = 0; i < n-1; i++){
        long a,b,c;
        cin >> a >> b >> c;
        a--,b--;
        viEdge[i] = make_pair(make_pair(a,b),c);
    }
    
    ModInt nAns = 0;
    vector<UnionFind> vzGroup(31,UnionFind(n));
    for(long i = 0; i <= 30; i++){
        for(long j = 0; j < n-1; j++){
            long a = viEdge[j].first.first;
            long b = viEdge[j].first.second;
            long c = viEdge[j].second;
            if((c&(1<<i))!=0){
                vzGroup[i].Unite(a,b);
            }
        }
        ModInt iThis = 0;
        for(long j = 0; j < n; j++){
            if(vzGroup[i].FindRoot(j)==j){
                ModInt m = vzGroup[i].GetSize(j);
                iThis += m*(m-1)/2;
            }
        }
        nAns += iThis*(1<<i);
    }
    cout << nAns << endl;
    return 0;
}
0