結果

問題 No.1928 Make a Binary Tree
ユーザー harurun
提出日時 2021-12-09 10:20:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 242 ms / 3,000 ms
コード長 1,232 bytes
コンパイル時間 1,182 ms
コンパイル使用メモリ 104,948 KB
最終ジャッジ日時 2025-01-26 07:13:46
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/segtree>
using namespace atcoder;

#include <iostream>
#include <vector>
#include <utility>
#include <deque>
using namespace std;

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);
}
int main(){
  int N;
  cin>>N;
  vector<vector<int>> Graph(N);
  for(int i=0;i<N-1;i++){
    int x,y;
    cin>>x>>y;
    Graph[x-1].push_back(y-1);
    Graph[y-1].push_back(x-1);
  }
  segtree<pair<int,int>,op,e> seg(N);
  deque<int> que;
  que.push_back(N);
  que.push_back(0);
  vector<bool> checked(N,false);
  vector<int> pre(N);
  vector<int> cnt(N,1);
  int searched=0;
  while(!que.empty()){
    int q=que.back();que.pop_back();
    if(q<N){
      pre[q]=searched;
      checked[q]=true;
      for(int to:Graph[q]){
        if(!checked[to]){
          que.push_back(to+N);
          que.push_back(to);
        }
      }
    }else{
      for(int j=0;j<2;j++){
        pair<int,int> tmp=seg.prod(pre[q-N],searched);
        if(tmp.first==-1){
          break;
        }
        cnt[q-N]+=tmp.first;
        seg.set(tmp.second,e());
      }
      seg.set(searched,make_pair(cnt[q-N],searched));
      searched++;
    }
  }
  cout<<cnt[0]<<endl;
  return 0;
}
0