結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-01 11:00:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 329 ms / 3,000 ms
コード長 1,512 bytes
コンパイル時間 1,444 ms
コンパイル使用メモリ 105,864 KB
実行使用メモリ 34,860 KB
最終ジャッジ日時 2024-04-23 22:02:44
合計ジャッジ時間 12,303 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 12 ms
6,944 KB
testcase_16 AC 8 ms
6,940 KB
testcase_17 AC 6 ms
6,944 KB
testcase_18 AC 11 ms
6,944 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 8 ms
6,940 KB
testcase_21 AC 5 ms
6,944 KB
testcase_22 AC 8 ms
6,944 KB
testcase_23 AC 6 ms
6,944 KB
testcase_24 AC 10 ms
6,944 KB
testcase_25 AC 12 ms
6,940 KB
testcase_26 AC 267 ms
19,108 KB
testcase_27 AC 154 ms
12,736 KB
testcase_28 AC 179 ms
13,312 KB
testcase_29 AC 326 ms
20,808 KB
testcase_30 AC 142 ms
12,160 KB
testcase_31 AC 203 ms
16,472 KB
testcase_32 AC 311 ms
20,364 KB
testcase_33 AC 161 ms
12,732 KB
testcase_34 AC 203 ms
16,520 KB
testcase_35 AC 229 ms
20,580 KB
testcase_36 AC 239 ms
27,444 KB
testcase_37 AC 202 ms
20,580 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 280 ms
27,164 KB
testcase_40 AC 279 ms
27,040 KB
testcase_41 AC 277 ms
27,036 KB
testcase_42 AC 280 ms
27,036 KB
testcase_43 AC 285 ms
26,952 KB
testcase_44 AC 283 ms
26,964 KB
testcase_45 AC 273 ms
27,032 KB
testcase_46 AC 277 ms
27,288 KB
testcase_47 AC 276 ms
27,040 KB
testcase_48 AC 275 ms
26,972 KB
testcase_49 AC 254 ms
34,860 KB
testcase_50 AC 320 ms
20,700 KB
testcase_51 AC 321 ms
20,752 KB
testcase_52 AC 321 ms
20,748 KB
testcase_53 AC 320 ms
20,748 KB
testcase_54 AC 329 ms
20,648 KB
testcase_55 AC 239 ms
26,924 KB
testcase_56 AC 323 ms
20,880 KB
testcase_57 AC 325 ms
20,940 KB
testcase_58 AC 319 ms
20,864 KB
testcase_59 AC 210 ms
21,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0