結果

問題 No.277 根掘り葉掘り
ユーザー 37zigen37zigen
提出日時 2016-05-01 19:39:01
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 897 bytes
コンパイル時間 2,370 ms
コンパイル使用メモリ 79,388 KB
実行使用メモリ 82,120 KB
最終ジャッジ日時 2024-04-15 08:27:32
合計ジャッジ時間 16,665 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.*;
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;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