結果

問題 No.317 辺の追加
ユーザー 37zigen37zigen
提出日時 2016-03-24 17:37:36
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,506 bytes
コンパイル時間 2,061 ms
コンパイル使用メモリ 79,304 KB
実行使用メモリ 70,128 KB
最終ジャッジ日時 2024-04-09 23:32:37
合計ジャッジ時間 7,981 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
61,224 KB
testcase_01 AC 127 ms
54,040 KB
testcase_02 TLE -
testcase_03 AC 1,383 ms
70,128 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
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 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder317;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
	static class UnionFind{
		static int[] data;
		UnionFind(int size){
			data=new int[size];
			Arrays.fill(data, -1);
		}
		boolean unionSet(int x,int y){
			x=root(x);
			y=root(y);
			if(x!=y){
				if(data[y]<data[x]){
					int tmp=x;
					x=y;
					y=tmp;
				}
				data[x]+=data[y];
				data[y]=x;
			}
			return x!=y;	
		}
		boolean findSet(int x,int y){
			return root(x)==root(y);
		}
		int root(int x){
			return data[x]<0?x:root(data[x]);
		}
		int size(int x){
			return -data[root(x)];
		}
	}

	public static void main(String[] args){

		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		UnionFind uf=new UnionFind(n+1);
		for(int i=0;i<m;i++){
			uf.unionSet(sc.nextInt(), sc.nextInt());
		}
		int[] d=new int[n+1];
		int[] nodes=new int[n+1];
		Arrays.fill(nodes, 0);
		Arrays.fill(d,-1);
		ArrayList<Integer> list=new ArrayList<>();
		for(int i=1;i<=n;i++){
			if(!list.contains(uf.root(i))){
				list.add(uf.root(i));
				nodes[uf.size(i)]++;
			}
		}

		for(int i=1;i<=n;i++){
			if(nodes[i]==0)continue;
			for(int j=0;j<nodes[i];j++){
				for(int k=n;k-i>=0;k--){
					if(k-i==0){
						d[k]=0;
						continue;
					}else if(d[k-i]!=-1){
						if(d[k]!=-1){
							d[k]=Math.min(d[k], d[k-i]+1);
						}else if(d[k]==-1){
							d[k]=d[k-i]+1;
						}
					}
				}
			}
		}
		for(int i=1;i<=n;i++){
			System.out.println(d[i]);
		}
	}
}
0