結果

問題 No.317 辺の追加
ユーザー kenjiiiHkenjiiiH
提出日時 2015-12-10 19:22:45
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,634 bytes
コンパイル時間 664 ms
コンパイル使用メモリ 73,812 KB
実行使用メモリ 5,156 KB
最終ジャッジ日時 2023-10-13 10:36:51
合計ジャッジ時間 9,342 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
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 <vector>
#include <map>

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<int> dp(N+1, -1);
  vector<int> ret(N+1, 99999999);
  for (auto &x : cmp) {
    int k = x.first;
    int v = x.second;

    cout << k << " " << v << endl;
    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