結果

問題 No.317 辺の追加
ユーザー chocorusk
提出日時 2018-11-14 16:24:44
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 334 ms / 2,000 ms
コード長 1,256 bytes
コンパイル時間 1,268 ms
コンパイル使用メモリ 105,740 KB
実行使用メモリ 12,032 KB
最終ジャッジ日時 2024-12-24 13:43:52
合計ジャッジ時間 13,073 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

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