結果
問題 | No.317 辺の追加 |
ユーザー | koyumeishi |
提出日時 | 2015-12-10 02:42:59 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 1,437 ms / 2,000 ms |
コード長 | 2,757 bytes |
コンパイル時間 | 1,000 ms |
コンパイル使用メモリ | 101,184 KB |
実行使用メモリ | 12,412 KB |
最終ジャッジ日時 | 2023-10-13 10:14:19 |
合計ジャッジ時間 | 16,350 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,352 KB |
testcase_01 | AC | 2 ms
4,348 KB |
testcase_02 | AC | 73 ms
8,980 KB |
testcase_03 | AC | 67 ms
8,436 KB |
testcase_04 | AC | 384 ms
9,676 KB |
testcase_05 | AC | 60 ms
7,700 KB |
testcase_06 | AC | 80 ms
9,124 KB |
testcase_07 | AC | 24 ms
5,096 KB |
testcase_08 | AC | 1,437 ms
11,296 KB |
testcase_09 | AC | 106 ms
10,316 KB |
testcase_10 | AC | 104 ms
11,152 KB |
testcase_11 | AC | 366 ms
10,440 KB |
testcase_12 | AC | 114 ms
11,044 KB |
testcase_13 | AC | 649 ms
10,764 KB |
testcase_14 | AC | 95 ms
10,544 KB |
testcase_15 | AC | 107 ms
11,004 KB |
testcase_16 | AC | 302 ms
10,824 KB |
testcase_17 | AC | 92 ms
10,680 KB |
testcase_18 | AC | 104 ms
11,212 KB |
testcase_19 | AC | 118 ms
11,012 KB |
testcase_20 | AC | 537 ms
10,744 KB |
testcase_21 | AC | 22 ms
6,808 KB |
testcase_22 | AC | 12 ms
5,132 KB |
testcase_23 | AC | 15 ms
5,456 KB |
testcase_24 | AC | 36 ms
9,460 KB |
testcase_25 | AC | 27 ms
7,624 KB |
testcase_26 | AC | 28 ms
7,940 KB |
testcase_27 | AC | 23 ms
7,000 KB |
testcase_28 | AC | 15 ms
5,352 KB |
testcase_29 | AC | 4 ms
4,348 KB |
testcase_30 | AC | 863 ms
12,160 KB |
testcase_31 | AC | 766 ms
12,412 KB |
testcase_32 | AC | 766 ms
12,188 KB |
testcase_33 | AC | 765 ms
12,180 KB |
testcase_34 | AC | 753 ms
12,148 KB |
testcase_35 | AC | 769 ms
12,092 KB |
testcase_36 | AC | 768 ms
12,156 KB |
testcase_37 | AC | 786 ms
12,200 KB |
testcase_38 | AC | 862 ms
12,248 KB |
testcase_39 | AC | 769 ms
12,240 KB |
ソースコード
#include <iostream> #include <vector> #include <cstdio> #include <sstream> #include <map> #include <string> #include <algorithm> #include <queue> #include <cmath> #include <set> using namespace std; class UnionFindTree{ typedef struct { int parent; int rank; }base_node; vector<base_node> node; public: UnionFindTree(int n){ node.resize(n); for(int i=0; i<n; i++){ node[i].parent=i; node[i].rank=0; } } int find(int x){ if(node[x].parent == x) return x; else{ return node[x].parent = find(node[x].parent); } } bool same(int x, int y){ return find(x) == find(y); } void unite(int x, int y){ x = find(node[x].parent); y = find(node[y].parent); if(x==y) return; if(node[x].rank < node[y].rank){ node[x].parent = y; }else if(node[x].rank > node[y].rank){ node[y].parent = x; }else{ node[x].rank++; unite(x,y); } } }; #include <bitset> typedef bitset<100000> bits; #include <cassert> int main(){ int n,m; cin >> n >> m; vector<vector<int>> g(n); UnionFindTree uft(n); for(int i=0; i<m; i++){ int u,v; scanf("%d%d", &u, &v); u--;v--; g[u].push_back(v); g[v].push_back(u); uft.unite(u,v); } vector<int> cnt(n, 0); for(int i=0; i<n; i++){ int r = uft.find(i); cnt[r]++; } map<int,int> w; vector<int> unko; for(int i=0; i<n; i++){ if(cnt[i]) w[cnt[i]] += 1; if(cnt[i]) unko.push_back(cnt[i]); } sort(unko.rbegin(), unko.rend()); vector<int> ans(n+1, 5*n); queue<int> pos; pos.push(0); ans[0] =-1; for(auto itr=w.begin(); itr!=w.end(); itr++){ for(int k=1; itr->second>0; k<<=1){ int c = min(itr->second, k); vector<int> next; while(pos.size()){ int p = pos.front(); pos.pop(); next.push_back(p); if(ans[p + c * itr->first] > ans[p] + c){ ans[p+c * itr->first] = ans[p] + c; next.push_back(p + c * itr->first); } } ; sort(next.begin(), next.end()); next.erase( unique(next.begin(), next.end()), next.end()); for(auto itr_=next.rbegin(); itr_!=next.rend(); itr_++){ pos.push(*itr_); } itr->second -= c; } } /* //for(int i=0; i<unko.size(); i++){ for(auto itr=w.rbegin(); itr!=w.rend(); itr++){ vector<int> next; while(pos.size()){ int p = pos.front(); pos.pop(); next.push_back(p); for(int k=0; k<itr->second; k++){ if(ans[p + itr->first] > ans[p] + 1){ ans[p + itr->first] = ans[p] + 1; next.push_back(p + itr->first); p += itr->first; } } } sort(next.begin(), next.end()); next.erase( unique(next.begin(), next.end()), next.end()); for(auto itr_=next.rbegin(); itr_!=next.rend(); itr_++){ pos.push(*itr_); } } */ for(int i=1; i<=n; i++){ if(ans[i] == 5*n) ans[i] = -1; printf("%d\n", ans[i]); } return 0; }