結果

問題 No.2427 Tree Distance Two
ユーザー kusagame12kusagame12
提出日時 2023-11-09 20:24:39
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,487 bytes
コンパイル時間 3,360 ms
コンパイル使用メモリ 83,852 KB
実行使用メモリ 212,328 KB
最終ジャッジ日時 2023-11-09 20:25:30
合計ジャッジ時間 26,157 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
54,740 KB
testcase_01 AC 74 ms
54,760 KB
testcase_02 AC 72 ms
54,648 KB
testcase_03 TLE -
testcase_04 AC 71 ms
54,752 KB
testcase_05 TLE -
testcase_06 AC 70 ms
54,656 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 1,573 ms
192,588 KB
testcase_15 AC 82 ms
54,764 KB
testcase_16 AC 83 ms
52,800 KB
testcase_17 AC 78 ms
54,772 KB
testcase_18 AC 84 ms
54,772 KB
testcase_19 AC 78 ms
54,760 KB
testcase_20 AC 252 ms
59,912 KB
testcase_21 AC 272 ms
61,840 KB
testcase_22 AC 174 ms
58,820 KB
testcase_23 AC 166 ms
56,192 KB
testcase_24 AC 263 ms
59,856 KB
testcase_25 AC 979 ms
94,208 KB
testcase_26 AC 1,981 ms
146,888 KB
testcase_27 AC 1,366 ms
111,908 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 AC 1,533 ms
108,412 KB
testcase_32 TLE -
testcase_33 AC 1,851 ms
134,180 KB
testcase_34 AC 1,360 ms
80,916 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に変更したら、通った。
        TreeMap<Integer , Set<Integer>> list = new TreeMap<>();
        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