結果

問題 No.317 辺の追加
ユーザー beetbeet
提出日時 2018-11-07 16:25:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 268 ms / 2,000 ms
コード長 1,604 bytes
コンパイル時間 2,622 ms
コンパイル使用メモリ 209,308 KB
実行使用メモリ 5,468 KB
最終ジャッジ日時 2024-04-30 22:27:31
合計ジャッジ時間 13,451 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 169 ms
5,376 KB
testcase_03 AC 169 ms
5,376 KB
testcase_04 AC 188 ms
5,376 KB
testcase_05 AC 150 ms
5,376 KB
testcase_06 AC 198 ms
5,376 KB
testcase_07 AC 63 ms
5,376 KB
testcase_08 AC 227 ms
5,376 KB
testcase_09 AC 218 ms
5,376 KB
testcase_10 AC 257 ms
5,376 KB
testcase_11 AC 156 ms
5,376 KB
testcase_12 AC 254 ms
5,376 KB
testcase_13 AC 172 ms
5,468 KB
testcase_14 AC 226 ms
5,376 KB
testcase_15 AC 243 ms
5,376 KB
testcase_16 AC 205 ms
5,376 KB
testcase_17 AC 233 ms
5,376 KB
testcase_18 AC 251 ms
5,376 KB
testcase_19 AC 255 ms
5,376 KB
testcase_20 AC 213 ms
5,376 KB
testcase_21 AC 100 ms
5,376 KB
testcase_22 AC 54 ms
5,376 KB
testcase_23 AC 54 ms
5,376 KB
testcase_24 AC 171 ms
5,376 KB
testcase_25 AC 120 ms
5,376 KB
testcase_26 AC 128 ms
5,376 KB
testcase_27 AC 102 ms
5,376 KB
testcase_28 AC 54 ms
5,376 KB
testcase_29 AC 14 ms
5,376 KB
testcase_30 AC 268 ms
5,376 KB
testcase_31 AC 256 ms
5,376 KB
testcase_32 AC 266 ms
5,376 KB
testcase_33 AC 255 ms
5,376 KB
testcase_34 AC 255 ms
5,376 KB
testcase_35 AC 264 ms
5,376 KB
testcase_36 AC 255 ms
5,376 KB
testcase_37 AC 259 ms
5,376 KB
testcase_38 AC 267 ms
5,376 KB
testcase_39 AC 257 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct UnionFind{
  int n;
  vector<int> r,p;
  UnionFind(){}
  UnionFind(int sz):n(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}
  int find(int x){
    return (x==p[x]?x:p[x]=find(p[x]));
  }
  bool same(int x,int y){
    return find(x)==find(y);
  }
  void unite(int x,int y){
    x=find(x);y=find(y);
    if(x==y) return;
    if(r[x]<r[y]) swap(x,y);
    r[x]+=r[y];
    p[y]=x;
  }
};

//INSERT ABOVE HERE
signed main(){
  int n,m;
  cin>>n>>m;
  UnionFind uf(n);
  for(int i=0;i<m;i++){
    int x,y;
    cin>>x>>y;
    x--;y--;
    uf.unite(x,y);
  }

  vector<int> v;
  for(int i=0;i<n;i++)
    if(uf.find(i)==i) v.emplace_back(uf.r[i]);

  const int BS = 400;
  vector<int> cnt(BS,0);

  using P = pair<int, int>;
  vector<P> vs;
  for(int x:v){
    if(x<BS) cnt[x]++;
    else vs.emplace_back(x,1);
  }
  
  for(int t=1;t<BS;t++){
    if(!cnt[t]) continue;
    for(int k=1;k<=cnt[t];k<<=1){
      vs.emplace_back(t*k,k);
      cnt[t]-=k;
    }
    if(cnt[t]) vs.emplace_back(t*cnt[t],cnt[t]);
  }

  const int INF = 1e9;
  vector<int> dp(n+1,INF);
  dp[0]=0;
  
  for(P p:vs){
    int d=p.first,c=p.second;
    vector<int> nx(n+1,INF);    
    for(int i=0;i<=n;i++){
      chmin(nx[i],dp[i]);      
      if(i+d<=n) chmin(nx[i+d],dp[i]+c);
    }
    swap(dp,nx);
  }
  
  for(int i=1;i<=n;i++){
    if(dp[i]==INF) dp[i]=0;
    cout<<dp[i]-1<<endl;
  }  
  return 0;
}
0