結果

問題 No.317 辺の追加
ユーザー motimoti
提出日時 2015-12-11 12:42:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 113 ms / 2,000 ms
コード長 2,170 bytes
コンパイル時間 850 ms
コンパイル使用メモリ 106,112 KB
実行使用メモリ 5,108 KB
最終ジャッジ日時 2023-10-13 10:53:57
合計ジャッジ時間 5,781 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 60 ms
4,612 KB
testcase_03 AC 79 ms
4,456 KB
testcase_04 AC 49 ms
4,984 KB
testcase_05 AC 84 ms
4,352 KB
testcase_06 AC 101 ms
4,480 KB
testcase_07 AC 32 ms
4,352 KB
testcase_08 AC 58 ms
4,900 KB
testcase_09 AC 74 ms
4,872 KB
testcase_10 AC 109 ms
4,796 KB
testcase_11 AC 21 ms
4,980 KB
testcase_12 AC 110 ms
4,796 KB
testcase_13 AC 30 ms
4,960 KB
testcase_14 AC 87 ms
4,880 KB
testcase_15 AC 103 ms
4,804 KB
testcase_16 AC 58 ms
4,984 KB
testcase_17 AC 93 ms
4,852 KB
testcase_18 AC 110 ms
4,880 KB
testcase_19 AC 113 ms
4,984 KB
testcase_20 AC 55 ms
4,836 KB
testcase_21 AC 33 ms
4,352 KB
testcase_22 AC 18 ms
4,352 KB
testcase_23 AC 18 ms
4,352 KB
testcase_24 AC 55 ms
4,964 KB
testcase_25 AC 39 ms
4,460 KB
testcase_26 AC 42 ms
4,464 KB
testcase_27 AC 33 ms
4,352 KB
testcase_28 AC 18 ms
4,352 KB
testcase_29 AC 5 ms
4,348 KB
testcase_30 AC 72 ms
4,804 KB
testcase_31 AC 71 ms
4,840 KB
testcase_32 AC 71 ms
5,108 KB
testcase_33 AC 70 ms
4,900 KB
testcase_34 AC 71 ms
4,976 KB
testcase_35 AC 71 ms
4,880 KB
testcase_36 AC 70 ms
4,804 KB
testcase_37 AC 71 ms
4,984 KB
testcase_38 AC 72 ms
4,876 KB
testcase_39 AC 71 ms
4,840 KB
権限があれば一括ダウンロードができます

ソースコード

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);

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

  vector<int> sizecnt(N + 1);
  rep(i, N) {
    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