結果
| 問題 | No.317 辺の追加 |
| コンテスト | |
| ユーザー |
kenjiiiH
|
| 提出日時 | 2015-12-10 19:37:56 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,716 bytes |
| 記録 | |
| コンパイル時間 | 931 ms |
| コンパイル使用メモリ | 83,148 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-15 07:41:55 |
| 合計ジャッジ時間 | 9,864 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 21 WA * 17 |
ソースコード
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int N, M;
class UnionFind {
int n;
vector<int> par;
vector<int> rank;
vector<int> cnt;
public:
UnionFind(int n) : n(n), cnt(n), par(n), rank(n) {
for (int i = 0; i < n; i++) {
par[i] = i;
cnt[i] = 1;
}
}
int find(int x) {
return par[x] == x ? x : par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (rank[x] < rank[y]) {
par[x] = y;
cnt[y] += cnt[x];
} else {
par[y] = x;
cnt[x] += cnt[y];
if (rank[x] == rank[y])
++rank[x];
}
}
int count(int x) {
return cnt[x];
}
bool same(int x, int y) {
return find(x) == find(y);
}
};
int main(int argc, char *argv[])
{
cin >> N >> M;
UnionFind uf(N);
for (int i = 0; i < M; i++) {
int v, w;
cin >> v >> w;
--v, --w;
uf.unite(v, w);
}
map<int, int> cmp;
for (int i = 0; i < N; i++) {
if (uf.find(i) == i) {
cmp[uf.count(i)]++;
}
}
vector<pair<int, int> > vs(cmp.begin(), cmp.end());
sort(vs.rbegin(), vs.rend());
vector<int> dp(N+1, -1);
vector<int> ret(N+1, 99999999);
for (auto &x : vs) {
int k = x.first;
int v = x.second;
dp[0] = v;
ret[0] = 0;
for (int i = 1; i <= N; i++) {
if (dp[i] >= 0) {
dp[i] = v;
} else if (i-k >= 0 && dp[i-k] > 0) {
dp[i] = dp[i-k]-1;
ret[i] = min(ret[i], ret[i-k]+1);
}
else {
dp[i] = -1;
}
}
}
for (int i = 1; i <= N; i++) {
if (dp[i] == -1)
cout << -1 << endl;
else
cout << ret[i]-1 << endl;
}
return 0;
}
kenjiiiH