結果

問題 No.317 辺の追加
ユーザー chocoruskchocorusk
提出日時 2018-11-14 16:24:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 304 ms / 2,000 ms
コード長 1,256 bytes
コンパイル時間 1,388 ms
コンパイル使用メモリ 103,024 KB
実行使用メモリ 12,064 KB
最終ジャッジ日時 2023-08-26 01:07:48
合計ジャッジ時間 13,150 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,740 KB
testcase_01 AC 3 ms
5,680 KB
testcase_02 AC 185 ms
9,336 KB
testcase_03 AC 190 ms
10,068 KB
testcase_04 AC 185 ms
8,628 KB
testcase_05 AC 170 ms
9,696 KB
testcase_06 AC 223 ms
10,836 KB
testcase_07 AC 69 ms
7,568 KB
testcase_08 AC 215 ms
8,300 KB
testcase_09 AC 238 ms
10,228 KB
testcase_10 AC 289 ms
11,888 KB
testcase_11 AC 149 ms
6,716 KB
testcase_12 AC 283 ms
12,064 KB
testcase_13 AC 165 ms
7,356 KB
testcase_14 AC 253 ms
10,796 KB
testcase_15 AC 277 ms
11,852 KB
testcase_16 AC 218 ms
9,140 KB
testcase_17 AC 265 ms
11,108 KB
testcase_18 AC 289 ms
11,988 KB
testcase_19 AC 304 ms
12,004 KB
testcase_20 AC 212 ms
8,772 KB
testcase_21 AC 103 ms
8,272 KB
testcase_22 AC 58 ms
6,972 KB
testcase_23 AC 57 ms
7,048 KB
testcase_24 AC 176 ms
10,192 KB
testcase_25 AC 127 ms
9,596 KB
testcase_26 AC 131 ms
9,444 KB
testcase_27 AC 105 ms
8,732 KB
testcase_28 AC 58 ms
7,228 KB
testcase_29 AC 16 ms
6,056 KB
testcase_30 AC 239 ms
9,304 KB
testcase_31 AC 234 ms
9,516 KB
testcase_32 AC 230 ms
9,332 KB
testcase_33 AC 233 ms
9,388 KB
testcase_34 AC 231 ms
9,296 KB
testcase_35 AC 231 ms
9,308 KB
testcase_36 AC 233 ms
9,320 KB
testcase_37 AC 235 ms
9,312 KB
testcase_38 AC 235 ms
9,304 KB
testcase_39 AC 230 ms
9,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int INF=1e8;
vector<int> g[100000];
bool used[100000];
int ct;
void dfs(int x){
    used[x]=1;
    ct++;
    for(auto y:g[x]){
        if(!used[y]){
            dfs(y);
        }
    }
}
int main()
{
    int n, m;
    cin>>n>>m;
	for(int i=0; i<m; i++){
	    int a, b;
	    cin>>a>>b;
	    a--; b--;
	    g[a].push_back(b);
	    g[b].push_back(a);
	}
	unordered_map<int, int> mp;
	for(int i=0; i<n; i++){
	    if(!used[i]){
	        ct=0;
	        dfs(i);
	        mp[ct]++;
	    }
	}
	int dp[100001];
	fill(dp, dp+n+1, INF);
	dp[0]=0;
	for(auto p:mp){
	    int x=p.first, c=p.second;
	    for(int k=0; c>0; k++){
	        int key=min(c, 1<<k);
	        c-=key;
	        for(int i=n; i>=key*x; i--){
	            dp[i]=min(dp[i], dp[i-key*x]+key);
	        }
	    }
	}
	for(int i=1; i<=n; i++){
	    if(dp[i]==INF) cout<<-1<<endl;
	    else cout<<dp[i]-1<<endl;
	}
	return 0;
}
0