結果

問題 No.317 辺の追加
ユーザー motimoti
提出日時 2015-12-11 12:40:00
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,176 bytes
コンパイル時間 940 ms
コンパイル使用メモリ 106,144 KB
実行使用メモリ 5,236 KB
最終ジャッジ日時 2023-10-13 10:53:51
合計ジャッジ時間 5,425 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <stack>
#include <bitset>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define minimize(a, x) a = std::min(a, x)
#define maximize(a, x) a = std::max(a, x)

typedef long long ll;
int const inf = 1<<29;

struct union_find {
  vector<int> rank_;
  vector<int> size_;
  vector<int> rid_;

  union_find(int n) { rank_.resize(n); rid_.assign(n, -1); size_.resize(n, 1); }

  void unite(int u, int v) {
    u = root(u), v = root(v);
    if(u == v) { return; }
    size_[u] = size_[v] = size_[u] + size_[v];
    if(rank_[u] < rank_[v]) { rid_[u] = v; }
    else { rid_[v] = u; if(rank_[u] == rank_[v]) { rank_[u]++; } }
  }

  bool same(int u, int v) { return root(u) == root(v); }
  int root(int x) { if(rid_[x] < 0) return x; else return rid_[x] = root(rid_[x]); }
  int operator[](int x) { return root(x); }
  int size_of(int x) { return size_[x]; }
  
};

int main() {

  int N, M; cin >> N >> M;

  union_find uf(N + 1);

  rep(i, M) {
    int u, v; cin >> u >> v; u--, v--;
    uf.unite(u, v);
  }

  vector<int> sizecnt(N + 1);
  rep(i, N+1) {
    if(uf[i] == i) {
      sizecnt[uf.size_of(uf[i])] ++;
    }
  }

  vector<int> dp(N + 1, inf);
  dp[0] = 0;

  // programming contest challenge book p.303
  int W = 0;
  for(int i = 1; i <= N; i++) {
    int num = sizecnt[i];
    for(int k = 1; num > 0; k *= 2) {
      int mul = min(k, num);
      num -= mul;
      W += i * mul;
      for(int j = W; j >= i * mul; j--) {
        if(dp[j - i * mul] >= inf) { continue; }
        minimize(dp[j], dp[j - i * mul] + mul);
      }
    }
  }

  for(int i = 1; i <= N; i++) {
    printf("%d\n", dp[i] == inf ? -1 : dp[i] - 1);
  }

  return 0;
}
0