結果

問題 No.277 根掘り葉掘り
ユーザー 37zigen37zigen
提出日時 2016-05-01 20:32:39
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,012 bytes
コンパイル時間 2,285 ms
コンパイル使用メモリ 79,936 KB
実行使用メモリ 87,124 KB
最終ジャッジ日時 2024-04-15 08:33:20
合計ジャッジ時間 24,029 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 139 ms
54,036 KB
testcase_02 AC 139 ms
54,216 KB
testcase_03 AC 167 ms
54,232 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 171 ms
54,312 KB
testcase_08 AC 135 ms
54,144 KB
testcase_09 AC 1,711 ms
84,284 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1,894 ms
84,828 KB
testcase_14 AC 1,826 ms
84,632 KB
testcase_15 AC 1,740 ms
84,460 KB
testcase_16 AC 1,706 ms
84,376 KB
testcase_17 AC 1,729 ms
85,132 KB
testcase_18 AC 1,654 ms
85,000 KB
testcase_19 AC 1,758 ms
84,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;
public class Main {
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		ArrayList<Integer>[] g=new ArrayList[n];
		for(int i=0;i<n;i++){
			g[i]=new ArrayList<Integer>();
		}
		for(int i=0;i<n-1;i++){
			int x=sc.nextInt()-1;
			int y=sc.nextInt()-1;
			g[x].add(y);g[y].add(x);
		}
		int[] dist=new int[n];
		Arrays.fill(dist, Integer.MAX_VALUE/3);
		Queue<Integer> q=new ArrayDeque<Integer>();
		q.add(0);
		for(int i=0;i<n;i++){
			//葉の場合、始点としてキューに追加
			if(g[i].size()<=1){
				dist[i]=0;
				q.add(i);
			}
		}
		while(!q.isEmpty()){
			int v=q.poll();
			for(int u:g[v]){
				if(dist[u]>dist[v]+1){
					dist[u]=dist[v]+1;
					q.add(u);
				}
			}
		}
		for(int i=0;i<n;i++){
			System.out.println(dist[i]);
		}
	}
}
0