結果

問題 No.2427 Tree Distance Two
ユーザー kusagame12kusagame12
提出日時 2023-11-09 20:23:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,953 ms / 2,000 ms
コード長 1,487 bytes
コンパイル時間 2,674 ms
コンパイル使用メモリ 81,068 KB
実行使用メモリ 207,416 KB
最終ジャッジ日時 2023-11-09 20:24:37
合計ジャッジ時間 35,853 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
54,756 KB
testcase_01 AC 71 ms
54,644 KB
testcase_02 AC 70 ms
54,648 KB
testcase_03 AC 1,953 ms
204,424 KB
testcase_04 AC 97 ms
54,660 KB
testcase_05 AC 1,885 ms
204,340 KB
testcase_06 AC 73 ms
54,640 KB
testcase_07 AC 1,742 ms
204,960 KB
testcase_08 AC 1,772 ms
205,484 KB
testcase_09 AC 1,714 ms
206,772 KB
testcase_10 AC 1,785 ms
205,088 KB
testcase_11 AC 1,692 ms
203,644 KB
testcase_12 AC 1,739 ms
207,416 KB
testcase_13 AC 1,800 ms
204,284 KB
testcase_14 AC 1,281 ms
201,244 KB
testcase_15 AC 71 ms
54,640 KB
testcase_16 AC 75 ms
54,748 KB
testcase_17 AC 71 ms
54,632 KB
testcase_18 AC 76 ms
54,744 KB
testcase_19 AC 74 ms
54,752 KB
testcase_20 AC 227 ms
59,736 KB
testcase_21 AC 239 ms
59,796 KB
testcase_22 AC 162 ms
58,480 KB
testcase_23 AC 143 ms
55,788 KB
testcase_24 AC 224 ms
59,740 KB
testcase_25 AC 749 ms
93,756 KB
testcase_26 AC 1,336 ms
142,532 KB
testcase_27 AC 1,119 ms
126,688 KB
testcase_28 AC 1,726 ms
178,684 KB
testcase_29 AC 1,675 ms
160,900 KB
testcase_30 AC 1,386 ms
140,708 KB
testcase_31 AC 1,002 ms
107,984 KB
testcase_32 AC 1,276 ms
137,712 KB
testcase_33 AC 1,188 ms
131,588 KB
testcase_34 AC 528 ms
80,552 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];
        //TreeMapからHashMapに変更したら、通った。
        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