結果

問題 No.317 辺の追加
ユーザー tnakao0123tnakao0123
提出日時 2016-04-04 00:50:49
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,237 bytes
コンパイル時間 874 ms
コンパイル使用メモリ 89,172 KB
実行使用メモリ 13,884 KB
最終ジャッジ日時 2024-04-10 11:07:44
合計ジャッジ時間 8,631 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,884 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 602 ms
6,940 KB
testcase_03 AC 138 ms
6,944 KB
testcase_04 TLE -
testcase_05 AC 94 ms
6,944 KB
testcase_06 AC 123 ms
6,944 KB
testcase_07 AC 36 ms
6,940 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 317.cc: No.317 辺の追加 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;

struct UFT {
  int links[MAX_N], ranks[MAX_N], sizes[MAX_N];

  void clear(int n) {
    for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1;
  }
  UFT() {}
  UFT(int n) { clear(n); }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

UFT uft;
vi cs;
int dp[MAX_N + 1];

/* subroutines */

/* main */

int main() {
  int n, m;
  cin >> n >> m;

  uft.clear(n);
  
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    if (! uft.same(u, v)) uft.merge(u, v);
  }

  for (int i = 0; i < n; i++)
    if (uft.root(i) == i) cs.push_back(uft.size(i));
  int cn = cs.size();
  //for (int i = 0; i < cn; i++) printf("%d ", cs[i]); putchar('\n');

  for (int i = 0; i <= n; i++) dp[i] = INF;
  dp[0] = 0;

  for (int i = 0; i < cn; i++) {
    int &ci = cs[i];
    for (int j = n - ci, k = n; j >= 0; j--, k--)
      if (dp[j] < INF) {
	int d = dp[j] + 1;
	if (dp[k] > d) dp[k] = d;
      }
  }

  for (int i = 1; i <= n; i++)
    printf("%d\n", (dp[i] >= INF) ? -1 : dp[i] - 1);
  cout.flush();
  return 0;
}
0