結果

問題 No.1817 Reversed Edges
ユーザー kusagame12kusagame12
提出日時 2023-11-16 18:40:21
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,730 bytes
コンパイル時間 2,361 ms
コンパイル使用メモリ 86,300 KB
実行使用メモリ 101,944 KB
最終ジャッジ日時 2023-11-16 18:40:28
合計ジャッジ時間 7,331 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
64,756 KB
testcase_01 AC 124 ms
57,836 KB
testcase_02 AC 128 ms
57,860 KB
testcase_03 AC 125 ms
57,956 KB
testcase_04 AC 126 ms
57,968 KB
testcase_05 AC 125 ms
57,864 KB
testcase_06 AC 126 ms
57,696 KB
testcase_07 TLE -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

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();
        
        List<Edge> edges = new ArrayList<>();
        for(int i = 0 ; i < n - 1 ; i++){
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            Edge edge = new Edge(a , b);
            edges.add(edge);
        }
        
        Graph graph = new Graph(edges , n);
        
        int[] ansArr = new int[n];
        boolean[] seen = new boolean[n];
        int ans = 0;
        ansArr[0] = dfs(graph , 0 , seen , ans);
        
        for(int i = 0 ; i < n ; i++){
            seen = new boolean[n];
            seen[i] = true;
            ansArr = dfs(graph , i , seen , ansArr);
        }
        
        for(int i = 0 ; i < n ; i++){
            System.out.println(ansArr[i]);
        }

    }
    
    private static int dfs(Graph graph , int v , boolean[] seen , int ans){
        
        seen[v] = true;
            
        for(Edge edge : graph.getEdgsList(v)){
        
            int next_v = edge.getTo();
            
            if(seen[next_v]){
                continue;
            }
                
            if(next_v < v){
                ans++;
            }
                
            ans = dfs(graph , next_v , seen , ans);
            
        }
        
        return ans;
        
    }
    
    private static int[] dfs(Graph graph , int v , boolean[] seen , int[] ansArr){
        
        seen[v] = true;
            
        for(Edge edge : graph.getEdgsList(v)){
        
            int next_v = edge.getTo();
            
            if(seen[next_v]){
                continue;
            }
            
            if(next_v > v){
                ansArr[next_v] = ansArr[v] + 1;
            }else{
                ansArr[next_v] = ansArr[v] - 1;
            }
            
            ansArr = dfs(graph , next_v , seen , ansArr);
            
        }
        
        return ansArr;
        
    }
    
}

//グラフのエッジ(辺)を格納するクラス
class Edge
{

    private 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  : 頂点の個数
    *
    */
    Graph(List<Edge> edges, int n)
    {
 
        for (int i = 0; i < n; i++) {
            edgeMap.put(i ,new ArrayList<>());
        }
 
        //無向グラフにエッジを追加します
        for (Edge edge: edges)
        {
            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