結果

問題 No.1843 Tree ANDistance
ユーザー tentententen
提出日時 2023-04-26 15:43:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 520 ms / 2,000 ms
コード長 3,409 bytes
コンパイル時間 2,795 ms
コンパイル使用メモリ 84,344 KB
実行使用メモリ 59,644 KB
最終ジャッジ日時 2024-04-27 18:23:38
合計ジャッジ時間 17,034 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 520 ms
58,784 KB
testcase_01 AC 496 ms
59,212 KB
testcase_02 AC 499 ms
58,856 KB
testcase_03 AC 504 ms
59,128 KB
testcase_04 AC 498 ms
58,940 KB
testcase_05 AC 479 ms
58,712 KB
testcase_06 AC 507 ms
59,644 KB
testcase_07 AC 512 ms
58,888 KB
testcase_08 AC 484 ms
59,000 KB
testcase_09 AC 482 ms
59,208 KB
testcase_10 AC 477 ms
59,120 KB
testcase_11 AC 501 ms
58,972 KB
testcase_12 AC 491 ms
58,764 KB
testcase_13 AC 493 ms
58,772 KB
testcase_14 AC 148 ms
52,964 KB
testcase_15 AC 151 ms
52,944 KB
testcase_16 AC 119 ms
52,196 KB
testcase_17 AC 128 ms
52,488 KB
testcase_18 AC 85 ms
51,320 KB
testcase_19 AC 155 ms
52,892 KB
testcase_20 AC 136 ms
52,928 KB
testcase_21 AC 53 ms
50,064 KB
testcase_22 AC 54 ms
50,004 KB
testcase_23 AC 54 ms
50,348 KB
testcase_24 AC 54 ms
49,996 KB
testcase_25 AC 55 ms
50,240 KB
testcase_26 AC 55 ms
50,300 KB
testcase_27 AC 54 ms
50,332 KB
testcase_28 AC 373 ms
58,720 KB
testcase_29 AC 364 ms
58,580 KB
testcase_30 AC 359 ms
58,716 KB
testcase_31 AC 394 ms
58,800 KB
testcase_32 AC 387 ms
58,776 KB
testcase_33 AC 235 ms
56,004 KB
testcase_34 AC 363 ms
58,732 KB
testcase_35 AC 54 ms
50,060 KB
testcase_36 AC 54 ms
50,388 KB
testcase_37 AC 54 ms
50,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static final int MOD = 1000000007;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] lefts = new int[n - 1];
        int[] rights = new int[n - 1];
        int[] values = new int[n - 1];
        for (int i = 0; i < n - 1; i++) {
            lefts[i] = sc.nextInt() - 1;
            rights[i] = sc.nextInt() - 1;
            values[i] = sc.nextInt();
        }
        UnionFindTree uft = new UnionFindTree(n);
        long ans = 0;
        for (int i = 0; i < 30; i++) {
            uft.clear();
            for (int j = 0; j < n - 1; j++) {
                if ((values[j] & (1 << i)) > 0) {
                    uft.unite(lefts[j], rights[j]);
                }
            }
            long tmp = 0;
            for (int j = 0; j < n; j++) {
                if (j == uft.find(j)) {
                    long count = uft.counts[j];
                    tmp += count * (count - 1) / 2;
                    tmp %= MOD;
                }
            }
            ans += tmp * (1 << i);
            ans %= MOD;
        }
        System.out.println(ans);
    }
    
    static class UnionFindTree {
        int[] parents;
        int[] counts;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            counts = new int[size];
        }
        
        public void clear() {
            for (int i = 0; i < parents.length; i++) {
                parents[i] = i;
                counts[i] = 1;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx != yy) {
                parents[xx] = yy;
                counts[yy] += counts[xx];
            }
         }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0