結果
問題 | No.317 辺の追加 |
ユーザー |
![]() |
提出日時 | 2017-04-24 22:32:38 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 219 ms / 2,000 ms |
コード長 | 1,665 bytes |
コンパイル時間 | 1,932 ms |
コンパイル使用メモリ | 178,840 KB |
実行使用メモリ | 148,320 KB |
最終ジャッジ日時 | 2024-09-13 12:10:08 |
合計ジャッジ時間 | 8,954 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 |
ソースコード
#include<bits/stdc++.h>using namespace std;#define rep(i,a,b) for(int i=a;i<b;i++)#define INF INT_MAX/2template<int NV> struct UF {int par[NV], cnt[NV];UF() { rep(i, 0, NV) par[i] = i, cnt[i] = 1; }int operator[](int x) { return par[x] == x ? x : par[x] = operator[](par[x]); }void operator()(int x, int y) { // xをyにくっつけるx = operator[](x); y = operator[](y);if (x != y) par[x] = y, cnt[y] += cnt[x];}};int N, M;UF<101010> uf;int dp[1010][101010];//-----------------------------------------------------------------------------------int main() {cin >> N >> M;rep(i, 0, M) {int a, b; scanf("%d%d", &a, &b);a--; b--;uf(a, b);}map<int, int> cnt;rep(i, 0, N) if (i == uf[i]) {//printf("<%d = %d>\n", i, uf.cnt[i]);cnt[uf.cnt[i]]++;}vector<pair<int, int>> items;for (auto p : cnt) {//printf("[%d %d]\n", p.first, p.second);for (int i = 1; i <= p.second; i *= 2) {int sm = i - 1;int j = min(p.second - sm, i);items.push_back({p.first * j, j});if (p.second - sm < i) break;}}int n = items.size();rep(i, 0, n + 1) rep(j, 0, N + 1) dp[i][j] = INF;dp[0][0] = 0;rep(i, 0, n) rep(j, 0, N + 1){dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);if (j + items[i].first <= N)dp[i + 1][j + items[i].first] = min(dp[i + 1][j + items[i].first], dp[i][j] + items[i].second);}rep(j, 1, N + 1) {if (dp[n][j] == INF) printf("-1\n");else printf("%d\n", dp[n][j] - 1);}}