結果
問題 | No.317 辺の追加 |
ユーザー |
|
提出日時 | 2015-12-10 15:42:16 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 1,840 ms / 2,000 ms |
コード長 | 1,282 bytes |
コンパイル時間 | 624 ms |
コンパイル使用メモリ | 63,192 KB |
実行使用メモリ | 8,084 KB |
最終ジャッジ日時 | 2024-09-15 07:36:23 |
合計ジャッジ時間 | 9,165 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:32:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 32 | scanf("%d%d",&N,&M); | ~~~~~^~~~~~~~~~~~~~ main.cpp:38:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 38 | scanf("%d%d",&a,&b); | ~~~~~^~~~~~~~~~~~~~ main.cpp:49:28: warning: ‘tbelow’ may be used uninitialized in this function [-Wmaybe-uninitialized] 49 | else tbelow+=e; | ~~~~~~^~~
ソースコード
#include <vector> #include <unordered_map> #include <algorithm> #include <cstdio> using namespace std; class union_find{ public: int *parent,*rank,n; 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(vector<int> &edges){ unordered_map<int,int> r; for(int i=0;i<n-1;i++)++r[root(i)]; for(auto &e:r)edges.push_back(e.second); sort(edges.begin(),edges.end()); } }; int main(){ const int INF=1<<29; int N,M; scanf("%d%d",&N,&M); vector<int>edges; edges.reserve(N); union_find uf(N+1); for(;M--;){ int a,b; scanf("%d%d",&a,&b); uf.unite(a-1,b-1); } uf.enumerate(edges); //vector<int>bag(N+1,INF); int *bag=uf.parent; bag[0]=-1; for(int i=1;i<=N;i++)bag[i]=INF; int t=0,tbelow,prev=0; for(auto &e:edges){ if(prev!=e)prev=e,tbelow=e; else tbelow+=e; t+=e; for(int i=t;i>=tbelow;i--)if(bag[i-e]<INF){ bag[i]=min(bag[i],bag[i-e]+1); } } for(int i=1;i<=N;i++)printf("%d\n",bag[i]==INF ? -1 : bag[i]); }