結果

問題 No.317 辺の追加
ユーザー kenjiiiHkenjiiiH
提出日時 2015-12-10 19:37:56
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,716 bytes
コンパイル時間 1,166 ms
コンパイル使用メモリ 84,196 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2023-10-13 10:38:27
合計ジャッジ時間 9,688 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 154 ms
4,580 KB
testcase_03 AC 153 ms
4,352 KB
testcase_04 WA -
testcase_05 AC 136 ms
4,348 KB
testcase_06 AC 177 ms
4,368 KB
testcase_07 AC 58 ms
4,352 KB
testcase_08 WA -
testcase_09 AC 191 ms
5,016 KB
testcase_10 AC 227 ms
5,064 KB
testcase_11 AC 135 ms
5,024 KB
testcase_12 AC 232 ms
4,848 KB
testcase_13 AC 143 ms
4,844 KB
testcase_14 WA -
testcase_15 AC 218 ms
4,836 KB
testcase_16 AC 182 ms
4,912 KB
testcase_17 AC 211 ms
5,028 KB
testcase_18 AC 236 ms
4,892 KB
testcase_19 AC 234 ms
4,972 KB
testcase_20 WA -
testcase_21 AC 92 ms
4,352 KB
testcase_22 AC 50 ms
4,352 KB
testcase_23 WA -
testcase_24 AC 158 ms
4,820 KB
testcase_25 AC 115 ms
4,528 KB
testcase_26 AC 120 ms
4,348 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 14 ms
4,352 KB
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 <vector>
#include <map>
#include <algorithm>

using namespace std;

int N, M;

class UnionFind {
  int n;
  vector<int> par;
  vector<int> rank;
  vector<int> cnt;
public:
  UnionFind(int n) : n(n), cnt(n), par(n), rank(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      cnt[i] = 1;
    }
  }
  
  int find(int x) {
    return par[x] == x ? x : par[x] = find(par[x]);
  }
  
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    
    if (rank[x] < rank[y]) {
      par[x] = y;
      cnt[y] += cnt[x];
    } else {
      par[y] = x;
      cnt[x] += cnt[y];
      if (rank[x] == rank[y])
	++rank[x];
    }
    
  }

  int count(int x) {
    return cnt[x];
  }
  
  bool same(int x, int y) {
    return find(x) == find(y);
  }
};

int main(int argc, char *argv[])
{
  cin >> N >> M;
  UnionFind uf(N);
  
  for (int i = 0; i < M; i++) {
    int v, w;
    cin >> v >> w;
    --v, --w;
    uf.unite(v, w);
  }

  map<int, int> cmp;
  for (int i = 0; i < N; i++) {
    if (uf.find(i) == i) {
      cmp[uf.count(i)]++;
    }
  }

  vector<pair<int, int> > vs(cmp.begin(), cmp.end());
  sort(vs.rbegin(), vs.rend());
  
  vector<int> dp(N+1, -1);
  vector<int> ret(N+1, 99999999);
  for (auto &x : vs) {
    int k = x.first;
    int v = x.second;

    dp[0] = v;
    ret[0] = 0;
    for (int i = 1; i <= N; i++) {
      if (dp[i] >= 0) {
	dp[i] = v;
      } else if (i-k >= 0 && dp[i-k] > 0) {
	dp[i] = dp[i-k]-1;
	ret[i] = min(ret[i], ret[i-k]+1);
      }
      else {
	dp[i] = -1;
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    if (dp[i] == -1)
      cout << -1 << endl;
    else
      cout << ret[i]-1 << endl;
  }
    
  return 0;
}
0