結果

問題 No.317 辺の追加
ユーザー 37zigen37zigen
提出日時 2016-03-25 01:04:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,478 ms / 2,000 ms
コード長 1,494 bytes
コンパイル時間 2,138 ms
コンパイル使用メモリ 78,068 KB
実行使用メモリ 70,976 KB
最終ジャッジ日時 2024-04-09 23:39:28
合計ジャッジ時間 49,704 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
54,132 KB
testcase_01 AC 122 ms
54,184 KB
testcase_02 AC 1,178 ms
68,540 KB
testcase_03 AC 1,201 ms
69,944 KB
testcase_04 AC 1,252 ms
70,944 KB
testcase_05 AC 1,162 ms
69,980 KB
testcase_06 AC 1,387 ms
70,184 KB
testcase_07 AC 802 ms
64,052 KB
testcase_08 AC 1,337 ms
70,760 KB
testcase_09 AC 1,296 ms
70,580 KB
testcase_10 AC 1,432 ms
70,184 KB
testcase_11 AC 871 ms
67,724 KB
testcase_12 AC 1,457 ms
70,032 KB
testcase_13 AC 1,041 ms
67,572 KB
testcase_14 AC 1,306 ms
70,892 KB
testcase_15 AC 1,427 ms
70,976 KB
testcase_16 AC 1,308 ms
68,948 KB
testcase_17 AC 1,478 ms
70,628 KB
testcase_18 AC 1,475 ms
70,420 KB
testcase_19 AC 1,338 ms
70,108 KB
testcase_20 AC 1,311 ms
70,840 KB
testcase_21 AC 836 ms
69,772 KB
testcase_22 AC 632 ms
63,756 KB
testcase_23 AC 635 ms
63,952 KB
testcase_24 AC 1,174 ms
69,740 KB
testcase_25 AC 952 ms
69,580 KB
testcase_26 AC 996 ms
69,608 KB
testcase_27 AC 881 ms
69,556 KB
testcase_28 AC 623 ms
63,880 KB
testcase_29 AC 337 ms
59,548 KB
testcase_30 AC 1,348 ms
70,152 KB
testcase_31 AC 1,342 ms
70,084 KB
testcase_32 AC 1,468 ms
70,044 KB
testcase_33 AC 1,450 ms
70,204 KB
testcase_34 AC 1,336 ms
70,052 KB
testcase_35 AC 1,334 ms
70,080 KB
testcase_36 AC 1,328 ms
69,892 KB
testcase_37 AC 1,408 ms
69,656 KB
testcase_38 AC 1,347 ms
70,028 KB
testcase_39 AC 1,341 ms
69,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder317;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
	public static void main(String[] args){

		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++){
			System.out.println(dp[i]-1);
		}
	}
	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