結果

問題 No.2427 Tree Distance Two
ユーザー kusagame12kusagame12
提出日時 2023-11-09 18:52:32
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,692 bytes
コンパイル時間 2,901 ms
コンパイル使用メモリ 81,268 KB
実行使用メモリ 161,552 KB
最終ジャッジ日時 2023-11-09 18:53:28
合計ジャッジ時間 49,069 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 143 ms
57,968 KB
testcase_01 AC 171 ms
57,824 KB
testcase_02 AC 145 ms
58,096 KB
testcase_03 TLE -
testcase_04 AC 143 ms
58,076 KB
testcase_05 TLE -
testcase_06 AC 143 ms
58,080 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,736 ms
154,816 KB
testcase_15 AC 153 ms
58,092 KB
testcase_16 AC 162 ms
58,348 KB
testcase_17 AC 144 ms
57,860 KB
testcase_18 AC 156 ms
57,968 KB
testcase_19 AC 149 ms
58,224 KB
testcase_20 AC 390 ms
62,944 KB
testcase_21 AC 387 ms
65,148 KB
testcase_22 AC 267 ms
61,820 KB
testcase_23 AC 250 ms
61,704 KB
testcase_24 AC 388 ms
62,904 KB
testcase_25 AC 1,154 ms
89,572 KB
testcase_26 TLE -
testcase_27 AC 1,559 ms
109,228 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 AC 1,976 ms
125,424 KB
testcase_31 AC 1,497 ms
104,444 KB
testcase_32 AC 1,921 ms
124,948 KB
testcase_33 AC 1,762 ms
113,456 KB
testcase_34 AC 850 ms
79,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Graph graph = new Graph(n);
        
        //List<Edge> edges = new ArrayList<>();
        int[] edgeCounts = new int[n];
        for (int i = 0 ; i < n - 1; i++){
            int u = sc.nextInt() - 1;
            int v = sc.nextInt() - 1;
            Edge e = new Edge(u , v);
            graph.addEdge(e);
            edgeCounts[u]++;
            edgeCounts[v]++;
        } 
        
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0 ; i < n ; i++){
            int edgeSum = 0;
            //頂点iがつながっている(隣接する)各頂点がもつ、辺の数を調べ、合計(edgeSum)をとる。
            for(Edge edge : graph.getEdgsList(i)){
                edgeSum += edgeCounts[edge.getTo()];
            }
            //頂点iがもつ辺の数と、辺の数の合計(edgeSum)を引く。
            sb.append(edgeSum - edgeCounts[i]+"\n");
        }
        
        System.out.print(sb);

    }

}

//グラフのエッジ(辺)を格納するクラス
class Edge
{
    public int from, to;
 
 	/**コンストラクタ
 	* @param : from 頂点 
 	* @param : to つながっている頂点
 	*/
   Edge(int from, int to)
    {
        this.from = from;
        this.to = to;
    }
    
    public int getFrom(){
        return from;
    }
    
    public int getTo(){
        return to;
    }
    
    public void setFrom(int from){
        this.from = from;
    }
    
    public void setTo(int to){
        this.to = to;
    }
 
}
 
//無向グラフクラス
class Graph
{
    //隣接リストを表すリストのリスト(ある頂点が、どの頂点とつながっているかを保持するリスト)
    Map<Integer , List<Edge>> edgeMap = new HashMap<>();
 
    /**コンストラクタ
    * @param edges  : 辺
    * @param n  : 頂点の個数
    *
    */
    public Graph(int n)
    {
 
        for (int i = 0; i < n; i++) {
            edgeMap.put(i ,new ArrayList<>());
        }
        
    }
    
    public void addEdge(Edge edge){
        
        int from = edge.getFrom();
        int to = edge.getTo();
 
        List<Edge> tmp1 = edgeMap.get(from);
        tmp1.add(edge);
        edgeMap.put(from , tmp1);
            
        List<Edge> tmp2 = edgeMap.get(to);
        Edge tmpEdge = new Edge(to , from);
        tmp2.add(tmpEdge);
        edgeMap.put(to , tmp2);
        
    }
    
    public int size() {
		return edgeMap.size();
	}

	public List<Edge> getEdgsList(int vNum) {
		return edgeMap.get(vNum);
	}
	
}
0