結果
問題 | No.317 辺の追加 |
ユーザー | ciel |
提出日時 | 2015-12-11 05:26:51 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 55 ms / 2,000 ms |
コード長 | 1,814 bytes |
コンパイル時間 | 1,616 ms |
コンパイル使用メモリ | 74,592 KB |
実行使用メモリ | 8,104 KB |
最終ジャッジ日時 | 2023-10-13 10:51:43 |
合計ジャッジ時間 | 5,816 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
4,348 KB |
testcase_01 | AC | 1 ms
4,352 KB |
testcase_02 | AC | 35 ms
4,352 KB |
testcase_03 | AC | 38 ms
4,352 KB |
testcase_04 | AC | 40 ms
4,468 KB |
testcase_05 | AC | 37 ms
4,352 KB |
testcase_06 | AC | 46 ms
4,348 KB |
testcase_07 | AC | 15 ms
4,348 KB |
testcase_08 | AC | 44 ms
5,716 KB |
testcase_09 | AC | 45 ms
4,348 KB |
testcase_10 | AC | 54 ms
4,352 KB |
testcase_11 | AC | 28 ms
8,104 KB |
testcase_12 | AC | 55 ms
4,348 KB |
testcase_13 | AC | 34 ms
6,548 KB |
testcase_14 | AC | 48 ms
4,352 KB |
testcase_15 | AC | 52 ms
4,352 KB |
testcase_16 | AC | 38 ms
4,468 KB |
testcase_17 | AC | 50 ms
4,352 KB |
testcase_18 | AC | 54 ms
4,348 KB |
testcase_19 | AC | 54 ms
4,352 KB |
testcase_20 | AC | 41 ms
4,684 KB |
testcase_21 | AC | 16 ms
4,348 KB |
testcase_22 | AC | 9 ms
4,348 KB |
testcase_23 | AC | 10 ms
4,348 KB |
testcase_24 | AC | 27 ms
4,348 KB |
testcase_25 | AC | 20 ms
4,352 KB |
testcase_26 | AC | 20 ms
4,352 KB |
testcase_27 | AC | 17 ms
4,352 KB |
testcase_28 | AC | 9 ms
4,352 KB |
testcase_29 | AC | 3 ms
4,352 KB |
testcase_30 | AC | 54 ms
4,352 KB |
testcase_31 | AC | 52 ms
4,352 KB |
testcase_32 | AC | 51 ms
4,348 KB |
testcase_33 | AC | 52 ms
4,352 KB |
testcase_34 | AC | 52 ms
4,348 KB |
testcase_35 | AC | 52 ms
4,348 KB |
testcase_36 | AC | 51 ms
4,348 KB |
testcase_37 | AC | 52 ms
4,348 KB |
testcase_38 | AC | 54 ms
4,352 KB |
testcase_39 | AC | 51 ms
4,352 KB |
ソースコード
#include <vector> #include <unordered_map> #include <algorithm> #include <cstdio> using namespace std; class union_find{ int *parent,*rank,n; public: int root(int a){return parent[a]==a?a:(parent[a]=root(parent[a]));} union_find(int _n){n=_n;parent=new int[n];rank=new int[n];for(int i=0;i<n;i++)parent[i]=i,rank[i]=0;} ~union_find(){delete []parent;delete []rank;} void unite(int a,int b){ int x=root(a),y=root(b); if(rank[x]<rank[y]){ parent[x]=y; }else{ parent[y]=x; if(rank[x]==rank[y])rank[x]++; } } void enumerate(unordered_map<int,int> &edges){ unordered_map<int,int> r; for(int i=0;i<n;i++)++r[root(i)]; for(auto &e:r)edges[e.second]++; } }; int main(){ const int INF=1<<29; int N,M; scanf("%d%d",&N,&M); union_find uf(N); for(;M--;){ int a,b; scanf("%d%d",&a,&b); uf.unite(a-1,b-1); } unordered_map<int,int>edges; uf.enumerate(edges); vector<int>bag(N+1,INF); bag[0]=-1; #if 0 int t=0,tbelow,prev=0; for(auto &e:edges){ int s=e.second; int d=1; while(s){ int x=d; if(x>s)x=s; s-=x; int y=e.first*x; t+=y; for(int i=t;i>=y&&bag[i];i--)if(bag[i-y]<INF){ bag[i]=min(bag[i],bag[i-y]+x); } d*=2; } /* if(prev!=e)prev=e,tbelow=e; else tbelow+=e; t+=e; for(int i=t;i>=tbelow&&bag[i];i--)if(bag[i-e]<INF){ bag[i]=min(bag[i],bag[i-e]+1); } */ } #endif int sum=0; for(auto &c:edges){ int s=c.second; int d=1; while(s){ int e=d; if(e>s)e=s; s-=e; int ce=c.first*e; sum+=ce; for(int k=sum-ce;k>=0;--k){ if(bag[k+ce]>e+bag[k]){ bag[k+ce]=e+bag[k]; } } d*=2; } } for(int i=1;i<=N;i++)printf("%d\n",bag[i]==INF ? -1 : bag[i]); }