結果
問題 |
No.317 辺の追加
|
ユーザー |
![]() |
提出日時 | 2015-12-10 19:22:45 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,634 bytes |
コンパイル時間 | 629 ms |
コンパイル使用メモリ | 74,280 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-15 07:40:30 |
合計ジャッジ時間 | 9,110 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 2 |
other | WA * 38 |
ソースコード
#include <iostream> #include <vector> #include <map> 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<int> dp(N+1, -1); vector<int> ret(N+1, 99999999); for (auto &x : cmp) { int k = x.first; int v = x.second; cout << k << " " << v << endl; 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; }