結果

問題 No.317 辺の追加
ユーザー koba-e964
提出日時 2016-12-17 18:08:45
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 330 ms / 2,000 ms
コード長 2,131 bytes
コンパイル時間 2,223 ms
コンパイル使用メモリ 102,820 KB
実行使用メモリ 205,364 KB
最終ジャッジ日時 2024-12-14 04:27:54
合計ジャッジ時間 14,140 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

const int N = 100010;
VI qf[N]; // QuickFind
int rt[N];
const int B = 500;
int dp[B][N];
const int inf = 1e8;

void solve(const vector<PI> &bag) {
  assert (bag.size() < B);
  REP(i, 0, B) {
    REP(j, 0, N) {
      dp[i][j] = inf;
    }
  }
  dp[0][0] = 0;
  REP(i, 1, bag.size() + 1) {
    PI cur = bag[i - 1];
    REP(j, 0, N) {
      int res = dp[i - 1][j];
      if (j >= cur.first) {
	res = min(res, dp[i - 1][j - cur.first] + cur.second);
      }
      dp[i][j] = res;
    }
  }
}

int main(void){
  int n, m;
  cin >> n >> m;
  REP(i, 0, n) {
    qf[i].push_back(i);
    rt[i] = i;
  }
  REP(i, 0, m) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    if (rt[u] != rt[v]) {
      int ru = rt[u];
      int rv = rt[v];
      if (qf[ru].size() < qf[rv].size()) {
	swap(ru, rv);
      }
      REP(j, 0, qf[rv].size()) {
	qf[ru].push_back(qf[rv][j]);
	rt[qf[rv][j]] = ru;
      }
      qf[rv].clear();
    }
  }
  map<int, int> cons;
  REP(i, 0, n) {
    int s = qf[i].size();
    if (s != 0) {
      if (cons.count(s) == 0) {
	cons[s] = 0;
      }
      cons[s]++;
    }
  }
  vector<PI> bag;
  for (map<int, int>::iterator it = cons.begin(); it != cons.end(); ++it) {
    int p = it->first;
    int q = it->second;
    int v = 1;
    while (v < q) {
      bag.push_back(PI(p * v, v));
      q -= v;
      v *= 2;
    }
    if (q > 0) {
      bag.push_back(PI(p * q, q));
    }
  }
  solve(bag);
  REP(i, 1, n + 1) {
    int res = dp[bag.size()][i];
    cout << (res == inf ? -1 : res - 1) << endl;
  }
}
0