結果

問題 No.2427 Tree Distance Two
ユーザー kusagame12kusagame12
提出日時 2023-11-09 19:25:09
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,944 bytes
コンパイル時間 2,470 ms
コンパイル使用メモリ 85,160 KB
実行使用メモリ 221,168 KB
最終ジャッジ日時 2023-11-09 19:25:22
合計ジャッジ時間 7,446 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 141 ms
57,680 KB
testcase_01 AC 142 ms
58,028 KB
testcase_02 AC 145 ms
57,692 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

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

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] edgeCounts = new int[n];
        Map<Integer , Set<Integer>> list = new HashMap<>();
        for(int i = 0 ; i < n ; i++){
            list.put(i ,new HashSet<>());
        }
        for (int i = 0 ; i < n - 1; i++){
            int u = sc.nextInt() - 1;
            int v = sc.nextInt() - 1;
            Set<Integer> tmp1 = list.get(u);
            tmp1.add(v);
            list.replace(u , tmp1);
            Set<Integer> tmp2 = list.get(v);
            tmp2.add(u);
            list.replace(v , tmp2);
            edgeCounts[u]++;
            edgeCounts[v]++;
        }
        
        sc.close();
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0 ; i < n ; i++){
            int edgeSum = 0;
            //頂点iがつながっている(隣接する)各頂点がもつ、辺の数を調べ、合計(edgeSum)をとる。
            for(int to : list.get(i)){
                edgeSum += edgeCounts[to];
            }
            //頂点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