結果

問題 No.1843 Tree ANDistance
ユーザー ripityripity
提出日時 2022-02-18 22:29:38
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,355 bytes
コンパイル時間 3,152 ms
コンパイル使用メモリ 77,096 KB
実行使用メモリ 99,636 KB
最終ジャッジ日時 2023-09-11 19:33:52
合計ジャッジ時間 9,477 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    
    public static Scanner sc = new Scanner(System.in);
    public static PrintWriter pw = new PrintWriter(System.out);
    
    public static void main(String[] args) {
        
        int t = 1;
        while( t > 0 ) {
            solve();
            t--;
        }
        
        pw.flush();
        
    }
    
    static void solve() {
        
        int N = sc.nextInt();
        int[] a = new int[N-1];
        int[] b = new int[N-1];
        int[] c = new int[N-1];
        long ans = 0;
        long P = 1000000007;
        
        for( int i = 0; i < N-1; i++ ) {
            a[i] = sc.nextInt()-1;
            b[i] = sc.nextInt()-1;
            c[i] = sc.nextInt();
        }
        
        for( int i = 0; i < 30; i++ ) {
            long k = modpow(2,i,P);
            ans += bfs(N,a,b,c,i,k,P);
            ans %= P;
        }
        
        pw.println(ans);
        
    }
    
    static long bfs(int N, int[] a, int[] b, int[] c, int d, long k, long P) {
        
        long res = 0;
        ArrayList<ArrayList<Integer>> edge = new ArrayList<>();
        for( int i = 0; i < N; i++ ) {
            edge.add(new ArrayList<>());
        }
        
        for( int i = 0; i < N-1; i++ ) {
            if( (c[i]>>d)%2 == 1 ) {
                edge.get(a[i]).add(b[i]);
                edge.get(b[i]).add(a[i]);
            }
        }
        
        LinkedList<Integer> queue = new LinkedList<>();
        boolean[] visited = new boolean[N];
        for( int j = 0; j < N; j++ ) {
            if( visited[j] ) continue;
            queue.offer(j);
            long cnt = 0;
            while( !queue.isEmpty() ) {
                int now = queue.poll();
                if( visited[now] ) continue;
                visited[now] = true;
                cnt++;
                for( int next : edge.get(now) ) {
                    if( !visited[next] ) queue.offer(next);
                }
            }
            res += cnt*(cnt-1)/2%P*k%P;
            res %= P;
        }
        
        return res;
        
    }
    
    static long modpow( long x, long n, long mod ) {
		
		if( n == 0 ) {
			return 1;
		}
		
		long k = 1;
		while( n > 1 ) {
			if( n%2 == 1 ) k *= x;
			x *= x;
			n /= 2;
			x %= mod;
			k %= mod;
		}
		
		return (k*x)%mod;
		
	}
    
}
0