結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 152 ms
6,944 KB
testcase_03 AC 153 ms
6,940 KB
testcase_04 AC 155 ms
6,944 KB
testcase_05 AC 129 ms
6,940 KB
testcase_06 AC 166 ms
6,940 KB
testcase_07 AC 54 ms
6,940 KB
testcase_08 AC 187 ms
6,940 KB
testcase_09 AC 188 ms
6,944 KB
testcase_10 AC 221 ms
6,940 KB
testcase_11 AC 134 ms
6,940 KB
testcase_12 AC 216 ms
6,940 KB
testcase_13 AC 143 ms
6,940 KB
testcase_14 AC 192 ms
6,940 KB
testcase_15 AC 208 ms
6,944 KB
testcase_16 AC 174 ms
6,940 KB
testcase_17 AC 201 ms
6,940 KB
testcase_18 AC 216 ms
6,940 KB
testcase_19 AC 223 ms
6,940 KB
testcase_20 AC 177 ms
6,940 KB
testcase_21 AC 88 ms
6,940 KB
testcase_22 AC 46 ms
6,944 KB
testcase_23 AC 47 ms
6,944 KB
testcase_24 AC 150 ms
6,940 KB
testcase_25 AC 115 ms
6,940 KB
testcase_26 AC 127 ms
6,940 KB
testcase_27 AC 92 ms
6,940 KB
testcase_28 AC 50 ms
6,944 KB
testcase_29 AC 12 ms
6,944 KB
testcase_30 AC 226 ms
6,944 KB
testcase_31 AC 213 ms
6,940 KB
testcase_32 AC 219 ms
6,940 KB
testcase_33 AC 211 ms
6,944 KB
testcase_34 AC 207 ms
6,948 KB
testcase_35 AC 213 ms
6,944 KB
testcase_36 AC 214 ms
6,940 KB
testcase_37 AC 212 ms
6,940 KB
testcase_38 AC 216 ms
6,940 KB
testcase_39 AC 206 ms
6,944 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;
    for(int i=n;i>=0;i--)
      if(i+d<=n) chmin(dp[i+d],dp[i]+c);
  }  
  
  for(int i=1;i<=n;i++){
    if(dp[i]==INF) dp[i]=0;
    cout<<dp[i]-1<<endl;
  }  
  return 0;
}
0