結果

問題 No.2427 Tree Distance Two
ユーザー kusagame12kusagame12
提出日時 2023-11-09 20:01:53
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 3,015 bytes
コンパイル時間 2,611 ms
コンパイル使用メモリ 85,348 KB
実行使用メモリ 767,788 KB
最終ジャッジ日時 2023-11-09 20:02:13
合計ジャッジ時間 18,637 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
54,640 KB
testcase_01 AC 71 ms
54,668 KB
testcase_02 AC 71 ms
54,660 KB
testcase_03 MLE -
testcase_04 AC 73 ms
52,684 KB
testcase_05 MLE -
testcase_06 AC 71 ms
54,652 KB
testcase_07 MLE -
testcase_08 MLE -
testcase_09 MLE -
testcase_10 MLE -
testcase_11 MLE -
testcase_12 MLE -
testcase_13 MLE -
testcase_14 MLE -
testcase_15 AC 77 ms
54,640 KB
testcase_16 AC 79 ms
54,748 KB
testcase_17 AC 71 ms
54,748 KB
testcase_18 AC 82 ms
54,772 KB
testcase_19 AC 79 ms
54,640 KB
testcase_20 AC 523 ms
385,312 KB
testcase_21 AC 643 ms
483,824 KB
testcase_22 AC 168 ms
80,540 KB
testcase_23 AC 141 ms
65,444 KB
testcase_24 AC 532 ms
376,304 KB
testcase_25 MLE -
testcase_26 MLE -
testcase_27 MLE -
testcase_28 MLE -
testcase_29 MLE -
testcase_30 MLE -
testcase_31 MLE -
testcase_32 MLE -
testcase_33 MLE -
testcase_34 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.lang.*;
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];
        int[][] list = new int[n+1][n];
        int[] cnt = new int[n+1];
        
        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[u][cnt[u]] = v;
            cnt[u]++;
            list[v][cnt[v]] = u;
            cnt[v]++;
            edgeCounts[u]++;
            edgeCounts[v]++;
        }
        
        br.close();
        
        StringBuilder sb = new StringBuilder();
        for(int i=1,len=n;i<=len;i++){
            int edgeSum = 0;
            //頂点iがつながっている(隣接する)各頂点がもつ、辺の数を調べ、合計(edgeSum)をとる。
            for(int to : list[i]){
                if(to == 0){
                    continue;
                }
                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