結果
問題 | No.1843 Tree ANDistance |
ユーザー | tenten |
提出日時 | 2023-04-26 15:43:31 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 556 ms / 2,000 ms |
コード長 | 3,409 bytes |
コンパイル時間 | 3,095 ms |
コンパイル使用メモリ | 91,316 KB |
実行使用メモリ | 59,396 KB |
最終ジャッジ日時 | 2024-11-15 22:56:17 |
合計ジャッジ時間 | 18,295 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 540 ms
58,984 KB |
testcase_01 | AC | 544 ms
58,696 KB |
testcase_02 | AC | 541 ms
59,132 KB |
testcase_03 | AC | 529 ms
58,796 KB |
testcase_04 | AC | 555 ms
59,080 KB |
testcase_05 | AC | 528 ms
58,656 KB |
testcase_06 | AC | 556 ms
59,396 KB |
testcase_07 | AC | 522 ms
59,040 KB |
testcase_08 | AC | 543 ms
59,072 KB |
testcase_09 | AC | 507 ms
58,952 KB |
testcase_10 | AC | 513 ms
58,480 KB |
testcase_11 | AC | 530 ms
59,132 KB |
testcase_12 | AC | 491 ms
59,060 KB |
testcase_13 | AC | 535 ms
58,808 KB |
testcase_14 | AC | 152 ms
52,860 KB |
testcase_15 | AC | 153 ms
52,636 KB |
testcase_16 | AC | 132 ms
52,148 KB |
testcase_17 | AC | 134 ms
52,204 KB |
testcase_18 | AC | 99 ms
51,204 KB |
testcase_19 | AC | 160 ms
52,692 KB |
testcase_20 | AC | 143 ms
52,940 KB |
testcase_21 | AC | 57 ms
50,292 KB |
testcase_22 | AC | 57 ms
50,356 KB |
testcase_23 | AC | 57 ms
50,156 KB |
testcase_24 | AC | 57 ms
50,124 KB |
testcase_25 | AC | 57 ms
50,096 KB |
testcase_26 | AC | 56 ms
50,188 KB |
testcase_27 | AC | 56 ms
50,032 KB |
testcase_28 | AC | 400 ms
58,356 KB |
testcase_29 | AC | 374 ms
58,484 KB |
testcase_30 | AC | 376 ms
58,472 KB |
testcase_31 | AC | 407 ms
58,544 KB |
testcase_32 | AC | 403 ms
58,596 KB |
testcase_33 | AC | 230 ms
55,992 KB |
testcase_34 | AC | 395 ms
58,596 KB |
testcase_35 | AC | 60 ms
50,164 KB |
testcase_36 | AC | 60 ms
50,028 KB |
testcase_37 | AC | 58 ms
50,184 KB |
ソースコード
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(); } }