結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 159 ms
5,376 KB
testcase_03 AC 154 ms
5,376 KB
testcase_04 AC 160 ms
5,376 KB
testcase_05 AC 131 ms
5,376 KB
testcase_06 AC 176 ms
5,376 KB
testcase_07 AC 58 ms
5,376 KB
testcase_08 AC 189 ms
5,376 KB
testcase_09 AC 190 ms
5,376 KB
testcase_10 AC 218 ms
5,376 KB
testcase_11 AC 132 ms
5,376 KB
testcase_12 AC 220 ms
5,376 KB
testcase_13 AC 149 ms
5,376 KB
testcase_14 AC 204 ms
5,376 KB
testcase_15 AC 213 ms
5,376 KB
testcase_16 AC 180 ms
5,376 KB
testcase_17 AC 210 ms
5,376 KB
testcase_18 AC 215 ms
5,376 KB
testcase_19 AC 214 ms
5,376 KB
testcase_20 AC 177 ms
5,376 KB
testcase_21 AC 87 ms
5,376 KB
testcase_22 AC 47 ms
5,376 KB
testcase_23 AC 46 ms
5,376 KB
testcase_24 AC 151 ms
5,376 KB
testcase_25 AC 107 ms
5,376 KB
testcase_26 AC 115 ms
5,376 KB
testcase_27 AC 88 ms
5,376 KB
testcase_28 AC 46 ms
5,376 KB
testcase_29 AC 12 ms
5,376 KB
testcase_30 AC 221 ms
5,376 KB
testcase_31 AC 217 ms
6,944 KB
testcase_32 AC 220 ms
5,376 KB
testcase_33 AC 213 ms
5,376 KB
testcase_34 AC 216 ms
5,376 KB
testcase_35 AC 214 ms
5,376 KB
testcase_36 AC 230 ms
5,376 KB
testcase_37 AC 215 ms
5,376 KB
testcase_38 AC 216 ms
5,376 KB
testcase_39 AC 208 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> cnt(n+1,0);
  for(int i=0;i<n;i++)
    if(uf.find(i)==i) cnt[uf.r[i]]++;

  using P = pair<int, int>;
  vector<P> vs;
  
  for(int t=1;t<=n;t++){
    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