結果

問題 No.2427 Tree Distance Two
ユーザー kusagame12kusagame12
提出日時 2023-11-09 20:21:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,923 ms / 2,000 ms
コード長 1,423 bytes
コンパイル時間 2,581 ms
コンパイル使用メモリ 83,512 KB
実行使用メモリ 207,184 KB
最終ジャッジ日時 2023-11-09 20:22:14
合計ジャッジ時間 37,315 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
54,644 KB
testcase_01 AC 74 ms
54,640 KB
testcase_02 AC 74 ms
52,668 KB
testcase_03 AC 1,877 ms
204,364 KB
testcase_04 AC 76 ms
54,644 KB
testcase_05 AC 1,923 ms
204,436 KB
testcase_06 AC 73 ms
54,636 KB
testcase_07 AC 1,812 ms
204,988 KB
testcase_08 AC 1,796 ms
205,300 KB
testcase_09 AC 1,777 ms
206,020 KB
testcase_10 AC 1,785 ms
204,816 KB
testcase_11 AC 1,699 ms
203,628 KB
testcase_12 AC 1,777 ms
207,184 KB
testcase_13 AC 1,816 ms
204,084 KB
testcase_14 AC 1,312 ms
201,244 KB
testcase_15 AC 76 ms
54,764 KB
testcase_16 AC 78 ms
54,756 KB
testcase_17 AC 74 ms
54,764 KB
testcase_18 AC 80 ms
54,688 KB
testcase_19 AC 77 ms
54,732 KB
testcase_20 AC 240 ms
59,608 KB
testcase_21 AC 254 ms
57,712 KB
testcase_22 AC 172 ms
59,088 KB
testcase_23 AC 136 ms
55,672 KB
testcase_24 AC 237 ms
59,724 KB
testcase_25 AC 784 ms
93,724 KB
testcase_26 AC 1,368 ms
142,404 KB
testcase_27 AC 1,169 ms
124,596 KB
testcase_28 AC 1,719 ms
178,680 KB
testcase_29 AC 1,722 ms
160,904 KB
testcase_30 AC 1,407 ms
140,116 KB
testcase_31 AC 998 ms
107,616 KB
testcase_32 AC 1,325 ms
139,176 KB
testcase_33 AC 1,225 ms
131,520 KB
testcase_34 AC 590 ms
80,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

//無向グラフクラスを使うと、TLEした。
public class Main {
    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] edgeCounts = new int[n+1];
        HashMap<Integer , Set<Integer>> list = new HashMap<>();
        for(int i=1,len=n;i<=len;i++){
            list.put(i ,new HashSet<>());
        }
        for(int i=0,len=n-1;i<len;i++){
            String[] data = br.readLine().split(" ");
            int u = Integer.parseInt(data[0]);
            int v = Integer.parseInt(data[1]);
            list.get(u).add(v);
            list.get(v).add(u);
            edgeCounts[u]++;
            edgeCounts[v]++;
        }
        
        br.close();
        
        StringBuilder sb = new StringBuilder();
        int edgeSum = 0;
        for(int i=1,len=n;i<=len;i++){
            //頂点iがつながっている(隣接する)各頂点がもつ、辺の数を調べ、合計(edgeSum)をとる。
            for(int to : list.get(i)){
                edgeSum += edgeCounts[to];
            }
            //頂点iがもつ辺の数と、辺の数の合計(edgeSum)を引く。
            sb.append(edgeSum - edgeCounts[i]+"\n");
            edgeSum = 0;
        }
        
        System.out.print(sb);

    }

}
0