結果

問題 No.317 辺の追加
ユーザー startcppstartcpp
提出日時 2018-03-07 16:43:37
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,305 bytes
コンパイル時間 556 ms
コンパイル使用メモリ 66,196 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-10 08:57:31
合計ジャッジ時間 9,440 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
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 AC 89 ms
6,940 KB
testcase_22 AC 50 ms
6,940 KB
testcase_23 WA -
testcase_24 AC 163 ms
6,944 KB
testcase_25 AC 118 ms
6,940 KB
testcase_26 AC 123 ms
6,944 KB
testcase_27 AC 96 ms
6,940 KB
testcase_28 WA -
testcase_29 AC 13 ms
6,940 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 222 ms
6,940 KB
testcase_33 AC 226 ms
6,944 KB
testcase_34 WA -
testcase_35 AC 215 ms
6,944 KB
testcase_36 AC 216 ms
6,940 KB
testcase_37 AC 211 ms
6,944 KB
testcase_38 WA -
testcase_39 AC 204 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#define rep(i, n) for(i = 0; i < n; i++)
#define rrep(i, n) for(i = n - 1; i >= 0; i--)
using namespace std;
typedef pair<int, int> P;

void chmin(int &a, int b) { a = min(a, b); }
struct UF {
	int par[100000];
	int sz[100000];
	UF() { for (int i = 0; i < 100000; i++) { par[i] = i; sz[i] = 1; } }
	int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); }
	void unit(int x, int y) { x = root(x); y = root(y); if (x != y) { par[x] = y; sz[y] += sz[x]; } }
	bool isRoot(int x) { return x == root(x); }
	int getSize(int x) { return sz[root(x)]; }
};

int INF = 11451419;
int n, m, u, v;
UF uf;
int cnt[100001];
vector<P> a;	//a[i] = (num, cost)
int dp[200001];	//dp([id])[sigma] = min_cnt

int main() {
	int i, j;
	
	cin >> n >> m;
	rep(i, m) {
		cin >> u >> v;
		u--; v--;
		uf.unit(u, v);
	}
	rep(i, n) {
		if (uf.isRoot(i)) {
			cnt[uf.getSize(i)]++;
		}
	}
	
	rep(i, n + 1) {
		rep(j, 20) {
			if ((cnt[i] >> j) & 1) {
				a.push_back(P(i * (1 << j), (1 << j)));
			}
		}
	}
	
	rep(i, n + 1) dp[i] = INF; dp[0] = 0;
	rep(i, a.size()) {
		rrep(j, n + 1) {
			chmin(dp[j + a[i].first], dp[j] + a[i].second);
		}
	}
	
	rep(i, n) {
		if (dp[i + 1] >= INF) cout << -1 << endl;
		else cout << dp[i + 1] - 1 << endl;
	}
	return 0;
}
0