結果
| 問題 | 
                            No.1098 LCAs
                             | 
                    
| コンテスト | |
| ユーザー | 
                             tenten
                         | 
                    
| 提出日時 | 2021-03-12 18:32:14 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                            (最新)
                                AC
                                 
                             
                            (最初)
                            
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,155 bytes | 
| コンパイル時間 | 2,395 ms | 
| コンパイル使用メモリ | 78,212 KB | 
| 実行使用メモリ | 111,444 KB | 
| 最終ジャッジ日時 | 2024-10-14 07:58:14 | 
| 合計ジャッジ時間 | 20,620 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 TLE * 1 | 
ソースコード
import java.util.*;
import java.io.*;
public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static long[] ans;
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		for (int i = 0; i < n; i++) {
		    graph.add(new ArrayList<>());
		}
		for (int i = 0; i < n - 1; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    int a = Integer.parseInt(line[0]) - 1;
		    int b = Integer.parseInt(line[1]) - 1;
		    graph.get(a).add(b);
		    graph.get(b).add(a);
		}
		ans = new long[n];
		getChildren(0, 0);
		StringBuilder sb = new StringBuilder();
		for (long x : ans) {
		    sb.append(x).append("\n");
		}
        System.out.print(sb);
   }
   
   static int getChildren(int idx, int parent) {
       int count = 1;
       for (int x : graph.get(idx)) {
           if (x == parent) {
               continue;
           }
           int y = getChildren(x, idx);
           ans[idx] += 2L * y * count;
           count += y;
       }
       ans[idx]++;
       return count;
   }
}
            
            
            
        
            
tenten