結果

問題 No.317 辺の追加
ユーザー rickythetarickytheta
提出日時 2015-12-10 00:13:58
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,658 bytes
コンパイル時間 1,307 ms
コンパイル使用メモリ 159,792 KB
実行使用メモリ 8,476 KB
最終ジャッジ日時 2023-10-13 08:59:14
合計ジャッジ時間 5,360 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,476 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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)%MOD

struct 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);
  }
  vl szs;
  REP(i,n){
    if(uf.root(i)==i)szs.push_back(uf.size(i));
  }
  map<ll,ll> dp;
  map<ll,ll>::iterator iter;
  dp[0] = -1;
  ll t = szs.size();
  REP(i,t){
    iter = dp.end();
    while(iter!=dp.begin()){
      --iter;
      ll id = iter->first;
      ll val = iter->second;
      if(id+szs[i]<=n){
        if(dp.count(id+szs[i]) == 1)
          dp[id+szs[i]] = min(dp[id+szs[i]],val+1);
        else
          dp[id+szs[i]] = val+1;
      }
    }
  }
  REPR(i,n+1){
    if(dp.count(i) == 1){
      cout << dp[i] << endl;
    }else{
      cout << -1 << endl;
    }
  }
  return 0;
}
0