結果

問題 No.317 辺の追加
ユーザー kuuso1kuuso1
提出日時 2015-12-12 21:45:39
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,601 bytes
コンパイル時間 2,950 ms
コンパイル使用メモリ 109,688 KB
実行使用メモリ 32,528 KB
最終ジャッジ日時 2023-10-13 11:52:56
合計ジャッジ時間 12,698 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		UnionFind UF=new UnionFind(N);
		for(int i=0;i<M;i++){
			var d=ria();
			UF.Union(d[0]-1,d[1]-1);
		}
		
		Dictionary<int,int> D=new Dictionary<int,int>();
		for(int i=0;i<N;i++){
			if(!UF.isRoot(i))continue;
			int mem=UF.MemCnt(i);
			if(D.ContainsKey(mem)==false)D.Add(mem,0);
			D[mem]++;
		}
		
		int[] dp=new int[N+1];
		for(int i=1;i<=N;i++)dp[i]=N+1;
		
		foreach(var size in D.Keys){
			int cnt=D[size];
			int mul=1;
			while(cnt>0){
				int cnt0=mul<=cnt?mul:cnt;
				cnt-=mul;
				for(int i=N;i>=0;i--){
					if(dp[i]>N)continue;
					dp[i+size*cnt0]=Math.Min(dp[i+size*cnt0],dp[i]+cnt0);
				}
				mul*=2;
			}
		}
		/*
		for(int i=1;i<=N;i++){
			Console.WriteLine(dp[i]>N?-1:dp[i]-1);
		}
		*/
		Console.WriteLine(String.Join("\n",dp.Select(x=>x>N?-1:x-1).ToArray()));
	}
	int N,M;
	public Sol(){
		var d=ria();
		N=d[0];M=d[1];
	}




	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(){return Console.ReadLine().Split(' ');}
	static int[] ria(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>int.Parse(e));}
	static long[] rla(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>long.Parse(e));}
	static double[] rda(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>double.Parse(e));}
}

class UnionFind{
	
	int[] parent;
	int[] mem;
	int compo;
	int N;
	public UnionFind(int n_){
		Initialize(n_);
	}
	
	public void Initialize(int n_){
		N=n_;
		parent=new int[N];
		mem=new int[N];
		for(int i=0;i<N;i++){
			parent[i]=i;
			mem[i]=1;
		}
		compo=N;
	}
	
	public int Parent(int a){
		if(parent[a]==a)return a;
		return parent[a]=Parent(parent[a]);
	}
	
	public bool areSameGp(int a,int b){
		return Parent(a)==Parent(b);
	}
	
	public void Union(int a,int b){
		a=Parent(a);b=Parent(b);//Parent()を呼ぶことでa以上のノードは全てparentがrootになる
		if(a==b)return;
		parent[a]=b;
		mem[b]+=mem[a];
		compo-=1;
	}
	
	public bool isRoot(int x){
		return x==parent[x];
	}
	public int MemCnt(int x){
		return mem[x];
	}
	
	public void Dump(){
		for(int i=0;i<parent.Length;i++){
			Console.Write(i==0?"{0}":" {0}",parent[i]);
		}
		Console.WriteLine("");
	}
	
	public int Compo{
		get{
			return compo;
		}
	}
	
}
0