結果

問題 No.2638 Initial fare
ユーザー tentententen
提出日時 2024-02-26 17:18:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,882 ms / 2,000 ms
コード長 2,704 bytes
コンパイル時間 2,951 ms
コンパイル使用メモリ 79,660 KB
実行使用メモリ 117,536 KB
最終ジャッジ日時 2024-04-02 08:17:59
合計ジャッジ時間 29,635 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
53,968 KB
testcase_01 AC 55 ms
53,984 KB
testcase_02 AC 58 ms
53,968 KB
testcase_03 AC 57 ms
53,964 KB
testcase_04 AC 1,173 ms
92,628 KB
testcase_05 AC 1,063 ms
103,160 KB
testcase_06 AC 56 ms
53,964 KB
testcase_07 AC 967 ms
101,328 KB
testcase_08 AC 853 ms
97,536 KB
testcase_09 AC 1,023 ms
102,428 KB
testcase_10 AC 887 ms
98,432 KB
testcase_11 AC 55 ms
53,956 KB
testcase_12 AC 913 ms
101,456 KB
testcase_13 AC 904 ms
99,704 KB
testcase_14 AC 859 ms
98,184 KB
testcase_15 AC 916 ms
98,172 KB
testcase_16 AC 56 ms
51,916 KB
testcase_17 AC 1,092 ms
100,476 KB
testcase_18 AC 954 ms
100,276 KB
testcase_19 AC 971 ms
93,300 KB
testcase_20 AC 1,126 ms
103,716 KB
testcase_21 AC 1,156 ms
99,948 KB
testcase_22 AC 60 ms
53,992 KB
testcase_23 AC 1,882 ms
117,536 KB
testcase_24 AC 1,489 ms
111,616 KB
testcase_25 AC 57 ms
53,988 KB
testcase_26 AC 1,019 ms
101,088 KB
testcase_27 AC 1,038 ms
105,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    static int[][] counts;
    static int[] parents;
    static long ans = 0;
    static List<List<Integer>> graph;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        graph = new ArrayList<>();
        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);
        }
        parents = new int[n];
        counts = new int[n][4];
        setValue(0, -1);
        setAns(0, 0);
        System.out.println(ans / 2);
    }
    
    static void setValue(int idx, int p) {
        counts[idx][0] = 1;
        parents[idx] = p;
        for (int x : graph.get(idx)) {
            if (x != p) {
                setValue(x, idx);
                for (int i = 1; i <= 3; i++) {
                    counts[idx][i] += counts[x][i - 1];
                }
            }
        }
    }
    
    static void setAns(int idx, int p) {
        for (int i = 1; i <= 3; i++) {
            ans += counts[idx][i];
        }
        if (parents[idx] >= 0) {
            ans += counts[parents[idx]][2] - counts[idx][1];
            ans += counts[parents[idx]][1] - 1;
            ans++;
            if (parents[parents[idx]] >= 0) {
                ans += counts[parents[parents[idx]]][1] - 1;
                ans++;
                if (parents[parents[idx]] != 0) {
                    ans++;
                }
            }
        }
        for (int x : graph.get(idx)) {
            if (x != p) {
                setAns(x, idx);
            }
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0