結果
問題 | No.317 辺の追加 |
ユーザー |
![]() |
提出日時 | 2015-12-10 02:01:10 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 223 ms / 2,000 ms |
コード長 | 1,573 bytes |
コンパイル時間 | 1,088 ms |
コンパイル使用メモリ | 76,156 KB |
実行使用メモリ | 5,464 KB |
最終ジャッジ日時 | 2024-09-15 07:17:07 |
合計ジャッジ時間 | 6,375 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:33:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 33 | scanf("%d%d", &n, &m); | ~~~~~^~~~~~~~~~~~~~~~ main.cpp:37:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 37 | scanf("%d%d", &u, &v); | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <cstdio>#include <iostream>#include <vector>#include <deque>#include <numeric>#include <algorithm>#include <utility>using namespace std;typedef pair<int,int> pii;struct UF{vector<int> par, cnt;explicit UF(int n) : par(n), cnt(n, 1) {iota(par.begin(), par.end(), 0);}int find(int x){return x != par[x] ? par[x] = find(par[x]) : x;}bool unite(int x, int y){x = find(x);y = find(y);if(x == y){ return false; }if(cnt[x] > cnt[y]){ swap(x, y); }par[x] = y;cnt[y] += cnt[x];return true;}};int main(){int n, m;scanf("%d%d", &n, &m);UF uf(n + 1);for(int i = 0; i < m; ++i){int u, v;scanf("%d%d", &u, &v);uf.unite(u, v);}vector<int> com;for(int i = 1; i <= n; ++i){if(uf.find(i) == i){com.push_back(uf.cnt[i]);}}sort(com.begin(), com.end());com.push_back(-1);const int INF = 1010101010;vector<int> dp1(n + 1, INF), dp2;dp1[0] = 0;deque<pii> dq;for(int f = 0; com[f] != -1; ){int d = com[f];int t;for(t = f + 1; com[t] == d; ++t);int cnt = t - f;dp2 = dp1;for(int r = 0; r < d; ++r){dq.clear();for(int i = 0; i * d + r <= n; ++i){int idx = i * d + r;if(dq.front().second == i - cnt - 1){dq.pop_front();}int val = dp1[idx] - i;while(!dq.empty() && dq.back().first >= val){dq.pop_back();}dq.emplace_back(val, i);dp2[idx] = min(dp2[idx], dq.front().first + i);}}dp1.swap(dp2);f = t;}for(int i = 1; i <= n; ++i){printf("%d\n", dp1[i] == INF ? -1 : dp1[i] - 1);}}