結果

問題 No.317 辺の追加
ユーザー motimoti
提出日時 2015-12-10 00:45:52
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,395 bytes
コンパイル時間 1,321 ms
コンパイル使用メモリ 114,232 KB
実行使用メモリ 12,180 KB
最終ジャッジ日時 2023-10-13 09:51:50
合計ジャッジ時間 9,374 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,932 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 724 ms
5,096 KB
testcase_03 AC 178 ms
4,612 KB
testcase_04 TLE -
testcase_05 AC 139 ms
4,352 KB
testcase_06 AC 176 ms
4,740 KB
testcase_07 AC 59 ms
4,352 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 #

#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 UnionFind
{
  vector<int> rank;
  vector<int> rid;

  UnionFind(int n) {
    rank.resize(n);
    rid.assign(n, -1);
  }

  void unite(int u, int v) {
    u = root(u), v = root(v);
    if(u == v) { return ; }
    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 main() {

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

  UnionFind uf(N);

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

  map<int, int> mp;
  rep(i, N) {
    mp[uf.root(i)] ++;
  }
/*
  cout << "----------------\n";
  for(auto& e: mp) {
    cout << e.first << " " << e.second << endl;
  }
  cout << endl;
  cout << "----------------\n";
*/

  vector<int> nums;
  for(auto& e: mp) {
    nums.push_back(e.second);
  }

  vector<pair<int, bool>> dp(1);//(100001, {inf, false});
  dp[0] = {-1, true};

  for(auto& e: nums) {
//    for(auto& u: dp) {
    for(int i=dp.size()-1; i>=0; i--) {
      if(dp[i].second) {
        if(dp.size() <= i + e) {
          int psize = dp.size(); dp.resize(i + e + 1); REP(k, psize, dp.size()) dp[k] = {inf, false};
        }
        if(dp[i].first + 1 < dp[i + e].first) {
          dp[i + e] = {dp[i].first + 1, true};
        }
      }
    }
  }

//  rep(i, 10) {  cout << i << " " << dp[i].first << " " << dp[i].second << endl; }

  REP(i, 1, N+1) {
    if(dp[i].second) {
      cout << dp[i].first << endl;
    }
    else {
      cout << -1 << endl;
    }
  }

  return 0;
}
0