結果
問題 |
No.1098 LCAs
|
ユーザー |
![]() |
提出日時 | 2020-06-26 22:26:33 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,258 bytes |
コンパイル時間 | 2,314 ms |
コンパイル使用メモリ | 78,032 KB |
実行使用メモリ | 110,480 KB |
最終ジャッジ日時 | 2024-07-04 22:10:46 |
合計ジャッジ時間 | 22,945 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 TLE * 2 |
ソースコード
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class Main { static int n; static List<List<Integer>> list; static Obj[] ans; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); n = Integer.parseInt(sa[0]); list = new ArrayList<>(n); for (int i = 0; i < n; i++) { list.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { sa = br.readLine().split(" "); int v = Integer.parseInt(sa[0]) - 1; int w = Integer.parseInt(sa[1]) - 1; list.get(v).add(w); list.get(w).add(v); } br.close(); ans = new Obj[n]; dfs(0, -1); PrintWriter pw = new PrintWriter(System.out); for (Obj o : ans) { pw.println(o.val); } pw.flush(); } static Obj dfs(int x, int p) { Obj o = new Obj(); long total = 0; for (int c : list.get(x)) { if (c != p) { Obj res = dfs(c, x); o.size += res.size; total += res.total; } } o.size++; o.total = (long) o.size * o.size; o.val = o.total - total; return ans[x] = o; } static class Obj { int size; long val, total; } }