結果

問題 No.277 根掘り葉掘り
ユーザー 37zigen37zigen
提出日時 2016-05-01 19:41:29
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,011 bytes
コンパイル時間 2,317 ms
コンパイル使用メモリ 79,032 KB
実行使用メモリ 87,292 KB
最終ジャッジ日時 2024-04-15 08:28:29
合計ジャッジ時間 23,693 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 140 ms
54,276 KB
testcase_02 AC 143 ms
54,388 KB
testcase_03 AC 164 ms
54,036 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 169 ms
54,300 KB
testcase_08 AC 138 ms
53,892 KB
testcase_09 AC 1,640 ms
86,540 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1,737 ms
86,272 KB
testcase_14 AC 1,650 ms
86,208 KB
testcase_15 AC 1,729 ms
85,648 KB
testcase_16 AC 1,714 ms
86,916 KB
testcase_17 AC 1,715 ms
87,292 KB
testcase_18 AC 1,765 ms
85,232 KB
testcase_19 AC 1,698 ms
85,984 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