結果

問題 No.2638 Initial fare
ユーザー tentententen
提出日時 2024-02-26 17:10:51
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,698 bytes
コンパイル時間 2,801 ms
コンパイル使用メモリ 88,640 KB
実行使用メモリ 172,404 KB
最終ジャッジ日時 2024-02-26 17:11:19
合計ジャッジ時間 25,815 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
61,556 KB
testcase_01 AC 72 ms
54,780 KB
testcase_02 AC 69 ms
54,652 KB
testcase_03 AC 75 ms
54,656 KB
testcase_04 AC 1,142 ms
103,272 KB
testcase_05 AC 1,244 ms
113,736 KB
testcase_06 AC 66 ms
54,508 KB
testcase_07 AC 1,220 ms
114,408 KB
testcase_08 AC 993 ms
105,076 KB
testcase_09 AC 1,374 ms
120,336 KB
testcase_10 AC 1,164 ms
116,608 KB
testcase_11 AC 72 ms
54,768 KB
testcase_12 AC 1,296 ms
113,224 KB
testcase_13 AC 1,005 ms
104,232 KB
testcase_14 AC 1,178 ms
115,792 KB
testcase_15 AC 1,160 ms
116,424 KB
testcase_16 AC 79 ms
54,764 KB
testcase_17 AC 1,432 ms
124,692 KB
testcase_18 AC 1,161 ms
115,100 KB
testcase_19 AC 1,241 ms
125,468 KB
testcase_20 AC 1,224 ms
119,992 KB
testcase_21 AC 1,263 ms
135,204 KB
testcase_22 AC 73 ms
54,900 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[][] counts;
    static int[] parents1;
    static int[] parents2;
    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);
        }
        parents1 = new int[n];
        parents2 = new int[n];
        counts = new int[n][4];
        setValue(0, -1, -1);
        setAns(0, 0);
        System.out.println(ans / 2);
    }
    
    static void setValue(int idx, int p1, int p2) {
        counts[idx][0] = 1;
        parents1[idx] = p1;
        parents2[idx] = p2;
        graph.get(idx).stream().filter(x -> x != p1).forEach(x -> {
            setValue(x, idx, p1);
            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 (parents1[idx] >= 0) {
            ans += counts[parents1[idx]][2] - counts[idx][1];
            ans += counts[parents1[idx]][1] - 1;
            ans++;
        }
        if (parents2[idx] >= 0) {
            ans += counts[parents2[idx]][1] - 1;
            ans++;
            if (parents2[idx] != 0) {
                ans++;
            }
        }
        graph.get(idx).stream().filter(x -> x != p).forEach(x -> 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