結果

問題 No.317 辺の追加
ユーザー しらっ亭しらっ亭
提出日時 2015-12-25 06:34:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 2,489 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 172,876 KB
実行使用メモリ 5,128 KB
最終ジャッジ日時 2023-10-19 03:30:05
合計ジャッジ時間 8,623 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,784 KB
testcase_01 AC 3 ms
4,784 KB
testcase_02 AC 74 ms
5,048 KB
testcase_03 AC 93 ms
5,048 KB
testcase_04 AC 68 ms
5,048 KB
testcase_05 AC 98 ms
4,996 KB
testcase_06 AC 118 ms
5,044 KB
testcase_07 AC 38 ms
4,780 KB
testcase_08 AC 89 ms
5,048 KB
testcase_09 AC 93 ms
5,048 KB
testcase_10 AC 133 ms
5,048 KB
testcase_11 AC 29 ms
5,048 KB
testcase_12 AC 135 ms
5,048 KB
testcase_13 AC 43 ms
5,048 KB
testcase_14 AC 107 ms
5,048 KB
testcase_15 AC 124 ms
5,048 KB
testcase_16 AC 79 ms
5,048 KB
testcase_17 AC 112 ms
5,048 KB
testcase_18 AC 130 ms
5,048 KB
testcase_19 AC 135 ms
5,048 KB
testcase_20 AC 78 ms
5,048 KB
testcase_21 AC 37 ms
4,872 KB
testcase_22 AC 21 ms
4,960 KB
testcase_23 AC 21 ms
4,964 KB
testcase_24 AC 62 ms
4,980 KB
testcase_25 AC 46 ms
5,080 KB
testcase_26 AC 47 ms
5,064 KB
testcase_27 AC 38 ms
5,128 KB
testcase_28 AC 21 ms
4,972 KB
testcase_29 AC 7 ms
4,764 KB
testcase_30 AC 114 ms
5,044 KB
testcase_31 AC 108 ms
5,048 KB
testcase_32 AC 107 ms
5,048 KB
testcase_33 AC 108 ms
5,048 KB
testcase_34 AC 107 ms
5,048 KB
testcase_35 AC 108 ms
5,048 KB
testcase_36 AC 107 ms
5,048 KB
testcase_37 AC 108 ms
5,048 KB
testcase_38 AC 113 ms
5,044 KB
testcase_39 AC 108 ms
5,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define REP(i,n) for (int i = 0; i < (n); i++)
#define RREP(i,n) for (int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define MP(x,y) make_pair((x), (y))
#define MIN(a,b) (a < b ? a : b)
#define MAX(a,b) (a > b ? a : b)
#define SZ(x) ((int)(x).size())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define F3(a) memset(a,0x3f,sizeof(a))
#define bitcount(b) __builtin_popcount(b)
#define GCD(a,b) (__gcd(a,b))
#define GCD3(a,b,c) (GCD(GCD(a,b), c))
#define LCM(a,b) (a/GCD(a,b)*b)
#define LCM3(a,b,c) LCM(LCM(a,b),c)
#define MOD 1000000007
template<class T> inline bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
typedef long long ll;
// -------------------------------------

template<int um> class UF {
  public:
    vector<int> par,rank,cnt;
    UF() {par=rank=vector<int>(um,0); cnt=vector<int>(um,1); for(int i=0;i<um;i++) par[i]=i;}
    void reinit() {REP(i,um) rank[i]=0,cnt[i]=1,par[i]=i;}
    int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
    int count(int x) { return cnt[operator[](x)];}
    int operator()(int x,int y) {
      if((x=operator[](x))==(y=operator[](y))) return x;
      cnt[y]=cnt[x]=cnt[x]+cnt[y];
      if(rank[x]>rank[y]) return par[x]=y;
      rank[x]+=rank[x]==rank[y]; return par[y]=x;
    }
};

int N, M;
int co[100001];
int dp[100001];
UF<100001> uf;

int main() {
  cin >> N >> M;
  REP(i, M) {
    int u, v;
    cin >> u >> v;
    uf(u-1, v-1);
  }
  map<int, int> gs;

  REP(i, N) co[uf[i]]++;
  REP(i, N) if (co[i] != 0) gs[co[i]]++;

  F3(dp);
  dp[0] = 0;

  deque<int> deq;

  for(auto& g : gs) {
    auto v = g.first;
    auto n = g.second;
    int num = n;
    for (int k = 1; num > 0; k <<= 1) {
      int mul = min(k, num);
      // v * mul で weight が mul が1個ある
      RREP(i, N+1) {
        if (i - v * mul >= 0) amin(dp[i], dp[i - v * mul] + mul);
      }
      num -= mul;
    }
  }

  FOR(i, 1, N + 1) {
    cout << (dp[i] <= N ? dp[i] - 1 : -1) << '\n';
  }

  return 0;
}
0