結果

問題 No.317 辺の追加
ユーザー 37zigen37zigen
提出日時 2016-03-25 01:08:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,060 ms / 2,000 ms
コード長 1,573 bytes
コンパイル時間 1,969 ms
コンパイル使用メモリ 77,624 KB
実行使用メモリ 69,724 KB
最終ジャッジ日時 2024-04-09 23:42:28
合計ジャッジ時間 33,896 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
53,744 KB
testcase_01 AC 105 ms
52,984 KB
testcase_02 AC 748 ms
67,836 KB
testcase_03 AC 870 ms
69,448 KB
testcase_04 AC 761 ms
65,768 KB
testcase_05 AC 927 ms
69,560 KB
testcase_06 AC 989 ms
69,564 KB
testcase_07 AC 582 ms
60,416 KB
testcase_08 AC 789 ms
64,104 KB
testcase_09 AC 839 ms
69,384 KB
testcase_10 AC 1,060 ms
69,524 KB
testcase_11 AC 454 ms
59,568 KB
testcase_12 AC 1,042 ms
69,724 KB
testcase_13 AC 621 ms
59,900 KB
testcase_14 AC 945 ms
69,336 KB
testcase_15 AC 1,007 ms
68,960 KB
testcase_16 AC 789 ms
67,844 KB
testcase_17 AC 963 ms
68,904 KB
testcase_18 AC 1,040 ms
69,472 KB
testcase_19 AC 980 ms
69,640 KB
testcase_20 AC 846 ms
68,652 KB
testcase_21 AC 544 ms
60,620 KB
testcase_22 AC 425 ms
59,152 KB
testcase_23 AC 429 ms
58,536 KB
testcase_24 AC 663 ms
67,660 KB
testcase_25 AC 590 ms
63,864 KB
testcase_26 AC 580 ms
63,904 KB
testcase_27 AC 547 ms
61,988 KB
testcase_28 AC 396 ms
58,080 KB
testcase_29 AC 268 ms
58,768 KB
testcase_30 AC 885 ms
69,496 KB
testcase_31 AC 855 ms
69,504 KB
testcase_32 AC 921 ms
69,184 KB
testcase_33 AC 953 ms
69,320 KB
testcase_34 AC 861 ms
69,208 KB
testcase_35 AC 840 ms
69,420 KB
testcase_36 AC 863 ms
69,336 KB
testcase_37 AC 896 ms
69,352 KB
testcase_38 AC 873 ms
69,496 KB
testcase_39 AC 856 ms
69,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder317;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		PrintWriter pw=new PrintWriter(System.out);
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		DJSet ds=new DJSet(n);
		for(int i=0;i<m;i++){
			ds.union(sc.nextInt()-1,sc.nextInt()-1);
		}
		int[] dp=new int[n+1];
		int[] f=new int[n+1];
		for(int i=0;i<n;i++){
			if(ds.upper[i]<0){
				f[-ds.upper[i]]++;
			}
		}
		Arrays.fill(dp, 999999999);
		dp[0]=0;
		int sum=0;
		for(int i=1;i<=n;i++){
			for(int d=1;f[i]>0;d*=2){
				int x=Math.min(f[i],d);
				f[i]-=x;//辺数iのグラフがf[i]個ある。
				sum+=x*i;
				for(int j=sum;j>=x*i;j--){
					dp[j]=Math.min(dp[j], dp[j-x*i]+x);
				}
				
			}
		}
		for(int i=0;i<=n;i++){
			if(dp[i]>1000000)dp[i]=0;
		}
		for(int i=1;i<=n;i++){
			pw.println(dp[i]-1);
		}
		pw.close();
	}
	public static class DJSet{
		public int[] upper;
		public DJSet(int n){
			upper=new int[n];
			Arrays.fill(upper, -1);
		}
		public int root(int x){
			return upper[x]<0? x:root(upper[x]);
		}
		public boolean equiv(int x,int y){
			return root(x)==root(y);
		}
		public boolean union(int x,int y){
				x=root(x);
				y=root(y);
				if(x!=y){
					if(upper[y]<upper[x]){
						int d=y;
						y=x;
						x=d;
					}
					upper[x]+=upper[y];
					upper[y]=x;
				}
				return x!=y;
		}
		public int size(int x){
			return -upper[root(x)];
		}
		public int count(){
			int ct=0;
			for(int u:upper)
				if(u<0)ct++;
			return ct;
		}
	}
}
0