結果
問題 | No.1098 LCAs |
ユーザー | tenten |
提出日時 | 2023-02-01 13:50:05 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,519 bytes |
コンパイル時間 | 2,878 ms |
コンパイル使用メモリ | 85,492 KB |
実行使用メモリ | 117,196 KB |
最終ジャッジ日時 | 2024-07-01 09:55:40 |
合計ジャッジ時間 | 23,652 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 68 ms
51,044 KB |
testcase_01 | AC | 69 ms
51,240 KB |
testcase_02 | AC | 68 ms
50,912 KB |
testcase_03 | AC | 68 ms
51,280 KB |
testcase_04 | AC | 69 ms
51,152 KB |
testcase_05 | AC | 69 ms
51,096 KB |
testcase_06 | AC | 70 ms
51,052 KB |
testcase_07 | AC | 69 ms
51,088 KB |
testcase_08 | AC | 70 ms
51,224 KB |
testcase_09 | AC | 69 ms
51,224 KB |
testcase_10 | AC | 68 ms
50,936 KB |
testcase_11 | AC | 70 ms
51,044 KB |
testcase_12 | AC | 68 ms
50,824 KB |
testcase_13 | AC | 107 ms
51,712 KB |
testcase_14 | AC | 106 ms
51,880 KB |
testcase_15 | AC | 106 ms
51,808 KB |
testcase_16 | AC | 104 ms
51,760 KB |
testcase_17 | AC | 106 ms
51,980 KB |
testcase_18 | AC | 1,068 ms
94,176 KB |
testcase_19 | AC | 1,119 ms
98,108 KB |
testcase_20 | AC | 1,034 ms
96,340 KB |
testcase_21 | AC | 1,102 ms
95,604 KB |
testcase_22 | AC | 1,072 ms
95,876 KB |
testcase_23 | AC | 1,049 ms
99,464 KB |
testcase_24 | AC | 1,056 ms
99,628 KB |
testcase_25 | AC | 918 ms
95,292 KB |
testcase_26 | AC | 1,049 ms
100,084 KB |
testcase_27 | AC | 944 ms
101,344 KB |
testcase_28 | TLE | - |
testcase_29 | TLE | - |
testcase_30 | AC | 1,270 ms
110,840 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static long[] counts; static long[] ans; static ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; graph.get(a).add(b); graph.get(b).add(a); } counts = new long[n]; ans = new long[n]; getCount(0, 0); System.out.println(String.join("\n", Arrays.stream(ans).mapToObj(String::valueOf).toArray(String[]::new))); } static long getCount(int idx, int p) { counts[idx] = 1; for (int x : graph.get(idx)) { if (x == p) { continue; } counts[idx] += getCount(x, idx); } ans[idx] = counts[idx]; for (int x : graph.get(idx)) { if (x == p) { continue; } ans[idx] += counts[x] * (counts[idx] - counts[x]); } return counts[idx]; } } 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(); } }