結果
問題 | No.1928 Make a Binary Tree |
ユーザー |
![]() |
提出日時 | 2021-12-01 11:00:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 290 ms / 3,000 ms |
コード長 | 1,512 bytes |
コンパイル時間 | 1,227 ms |
コンパイル使用メモリ | 106,332 KB |
最終ジャッジ日時 | 2025-01-26 02:37:52 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
#include <iostream> #include <vector> #include <utility> #include <atcoder/segtree> #include <atcoder/dsu> #include <assert.h> using namespace atcoder; using namespace std; vector<vector<int>> Graph; vector<bool> checked; pair<int,int> op(pair<int,int> a,pair<int,int> b){ return a>b?a:b; } pair<int,int> e(){ return make_pair(-1,-1); } segtree<pair<int,int>,op,e> seg; int searched=0; void _debug(){ int N=Graph.size(); for(int i=0;i<N;i++){ pair<int,int> p=seg.get(i); cout<<"("<<p.first<<","<<p.second<<") "; }cout<<endl; } int dfs(int i){ //cerr<<i<<" "<<searched<<endl; int pre=searched; checked[i]=true; for(int to:Graph.at(i)){ if(!checked.at(to)){ int cnt=dfs(to); seg.set(searched,make_pair(cnt,searched)); //cout<<"set:"<<cnt<<" "<<searched<<endl; searched++; } } //cout<<"i:"<<i<<endl; //_debug(); int res=1; for(int j=0;j<2;j++){ pair<int,int> tmp=seg.prod(pre,searched); if(tmp.first==-1){ break; } res+=tmp.first; seg.set(tmp.second,e()); } return res; } int main(){ int N; cin>>N; assert(1<=N && N<=200000); dsu uf(N); Graph.resize(N); checked.resize(N,false); for(int i=0;i<N-1;i++){ int x,y; cin>>x>>y; assert(1<=x && 1<=y && x<=N && y<=N); assert(!uf.same(x-1,y-1)); uf.merge(x-1,y-1); Graph[x-1].push_back(y-1); Graph[y-1].push_back(x-1); } seg=segtree<pair<int,int>,op,e>(N); int ans=dfs(0); cout<<ans<<endl; //_debug(); return 0; }