結果
問題 | No.317 辺の追加 |
ユーザー |
![]() |
提出日時 | 2015-12-12 01:35:58 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 242 ms / 2,000 ms |
コード長 | 1,818 bytes |
コンパイル時間 | 1,370 ms |
コンパイル使用メモリ | 170,404 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-15 08:33:52 |
合計ジャッジ時間 | 9,796 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 |
ソースコード
#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef vector<int> vi;typedef vector<ll> vl;typedef complex<double> P;typedef pair<int,int> pii;#define REP(i,n) for(ll i=0;i<n;++i)#define REPR(i,n) for(ll i=1;i<n;++i)#define FOR(i,a,b) for(ll i=a;i<b;++i)#define DEBUG(x) cout<<#x<<": "<<x<<endl#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl#define ALL(a) (a).begin(),(a).end()#define MOD (ll)(1e9+7)#define ADD(a,b) a=((a)+(b))%MOD#define FIX(a) ((a)%MOD+MOD)%MODstruct UF{vi data;UF(int size):data(size,-1){}int root(int a){return data[a]<0 ? a : data[a]=root(data[a]);}void unite(int a,int b){a=root(a);b=root(b);if(a!=b){if(data[b]<data[a])swap(a,b);data[a] += data[b];data[b] = a;}}bool same(int a,int b){return root(a) == root(b);}int size(int a){return -data[root(a)];}};int main(){ll n,m;cin >> n >> m;UF uf(n);REP(i,m){int u,v;cin>>u>>v;--u;--v;uf.unite(u,v);}map<ll,ll> szs;REP(i,n){if(uf.root(i)==i) szs[uf.size(i)]+=1;}vl dp(n+1,1e18);dp[0] = -1;map<ll,ll>::iterator iter2;iter2 = szs.begin();while(iter2!=szs.end()){ll num = iter2->first;ll cnt = iter2->second;++iter2;// 2の累乗ごとにステップすると良い(らしい)// 3のステップが欲しい場合は、1のステップの結果を2のステップで飛ばせば良いfor(ll i=0;cnt>0;++i){ll t = min((ll)1<<i,cnt);cnt -= t;REP(_p,n-num*t+1){ll p = (n-num*t)-_p;dp[p+num*t] = min(dp[p+num*t],dp[p]+t);}}}REPR(i,n+1){if(dp[i]!=1e18){cout << dp[i] << endl;}else{cout << -1 << endl;}}return 0;}