結果
問題 | No.317 辺の追加 |
ユーザー |
|
提出日時 | 2015-12-10 17:27:06 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 619 ms / 2,000 ms |
コード長 | 2,470 bytes |
コンパイル時間 | 2,344 ms |
コンパイル使用メモリ | 175,468 KB |
実行使用メモリ | 14,848 KB |
最終ジャッジ日時 | 2024-09-15 07:38:26 |
合計ジャッジ時間 | 12,548 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:63:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 63 | scanf("%d %d", &u, &v); | ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>#define GET_MACRO(a, b, c, NAME, ...) NAME#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)#define rep2(i, a) rep3 (i, 0, a)#define rep3(i, a, b) for (int i = (a); i < (b); i++)#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)#define repr2(i, a) repr3 (i, 0, a)#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }using namespace std;typedef long long ll;struct UF {vector<int> parent, size;UF(int n) : parent(n), size(n, 1) {rep (i, n) parent[i] = i;}int root(int x) {if (x == parent[x]) return x;else return parent[x] = root(parent[x]);}void merge(int x, int y) {x = root(x); y = root(y);if (x == y) return;if (size[x] < size[y]) swap(x, y);size[x] += size[y];parent[y] = x;}bool same(int x, int y) {return root(x) == root(y);}};struct Stopwatch {double start;Stopwatch() {begin();}void begin() {start = clock();}double elapsed() {double end = clock();double res = (double)(end - start) / CLOCKS_PER_SEC * 1000.0;start = end;return res;}void display(string text) {cerr << text << ":" << elapsed() << "ms" << endl;}};const int inf = 1e9;int dp0[101010], dp1[101010];int main() {int n, m;cin >> n >> m;UF uf(n);rep (i, m) {int u, v;scanf("%d %d", &u, &v);u--; v--;uf.merge(u, v);}map<int, int> mp;rep (i, n) if (uf.root(i) == i) mp[uf.size[i]]++;vector<int> a, num;for (auto kv : mp) {a.push_back(kv.first);num.push_back(kv.second);}rep (i, 101010) dp0[i] = dp1[i] = inf;dp0[0] = 0;int sum = 0;vector<multiset<int>> st(101010);vector<int> ord(101010);rep (i, a.size()) {rep (j, sum + 1) {st[j].clear();dp1[j] = inf;}sum += a[i] * num[i];rep (j, sum + 1) {if (j - a[i] >= 0) {swap(st[j], st[j - a[i]]);ord[j] = ord[j - a[i]] + 1;} else {ord[j] = 0;}st[j].insert(dp0[j] - ord[j]);if (st[j].size() > num[i] + 1) {int pj = j - (num[i] + 1) * a[i];auto it = st[j].find(dp0[pj] - ord[pj]);st[j].erase(it);}int cand = *st[j].begin();chmin(dp1[j], cand + ord[j]);}swap(dp0, dp1);}rep (i, 1, n + 1) {if (dp0[i] > n) dp0[i] = 0;printf("%d\n", dp0[i] - 1);}return 0;}