結果

問題 No.1752 Up-Down Tree
ユーザー 👑 NachiaNachia
提出日時 2021-11-19 23:49:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 3,560 bytes
コンパイル時間 1,648 ms
コンパイル使用メモリ 90,640 KB
実行使用メモリ 22,552 KB
最終ジャッジ日時 2023-08-30 11:28:34
合計ジャッジ時間 4,378 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
22,552 KB
testcase_01 AC 82 ms
22,528 KB
testcase_02 AC 47 ms
9,680 KB
testcase_03 AC 50 ms
9,744 KB
testcase_04 AC 75 ms
16,252 KB
testcase_05 AC 80 ms
18,756 KB
testcase_06 AC 69 ms
14,104 KB
testcase_07 AC 69 ms
14,168 KB
testcase_08 AC 37 ms
6,728 KB
testcase_09 AC 69 ms
8,740 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 28 ms
5,752 KB
testcase_14 AC 34 ms
6,448 KB
testcase_15 AC 7 ms
4,384 KB
testcase_16 AC 32 ms
6,280 KB
testcase_17 AC 24 ms
5,676 KB
testcase_18 AC 79 ms
8,928 KB
testcase_19 AC 75 ms
8,876 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <atcoder/modint>
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(n); i++)

using m32 = atcoder::static_modint<1'000'000'007>;


struct leftist_heap{
  using Elem = int;
  struct Node{
    Node* l;
    Node* r;
    int height;
    Elem key;
  };

  Node* root;
  int m_size;

  leftist_heap(){
    root = nullptr;
    m_size = 0;
  }

  static Node* meld_node_norecursive(Node* root, Node* anotherroot){
    if(anotherroot == nullptr){
      return root;
    }
    if(root == nullptr){
      root = anotherroot;
      return root;
    }
    vector<Node*> st;
    st.reserve(root->height + anotherroot->height);
    Node** p1 = &root;
    Node** p2 = &anotherroot;
    while(true){
      if(*p1 == nullptr){
        *p1 = *p2;
        break;
      }
      if((*p1)->key < (*p2)->key) swap(*p1, *p2);
      st.push_back(*p1);
      p1 = &(*p1)->r;
    }
    auto st_ritr = st.rbegin();
    while(st_ritr != st.rend()){
      Node* c = *st_ritr;
      if(c->l == nullptr){
        swap(c->l, c->r);
        c->height = 1;
      }
      else if(c->r == nullptr){
        c->height = 1;
      }
      else{
        if(c->l->height < c->r->height){
          swap(c->l, c->r);
        }
        c->height = c->r->height + 1;
      }
      st_ritr++;
    }
    return root;
  }

  void push(const Elem& val){
    Node* new_node = new Node();
    new_node->height = 1;
    new_node->l = nullptr;
    new_node->r = nullptr;
    new_node->key = val;
    root = meld_node_norecursive(root, new_node);
    m_size += 1;
  }

  Elem top(){
    return root->key;
  }

  void pop(){
    auto old_root = root;
    root = meld_node_norecursive(old_root->l, old_root->r);
    delete(old_root);
    m_size -= 1;
  }

  int size(){
    return m_size;
  }

  void meld(leftist_heap r){
    m_size += r.m_size;
    root = meld_node_norecursive(root, r.root);
    r.root = nullptr;
  }
};


int N;
vector<vector<int>> E;

/*
leftist_heap dfs(int p, int pre=-1){
  leftist_heap res;
  for(int e : E[p]) if(e != pre){
    res.meld(dfs(e,p));
  }
  if(res.size() == 0){
    res.push(1);
    return res;
  }
  if(res.size() == 1){
    auto v = res.top(); res.pop();
    if(v % 2 == 0){ res.push(v); res.push(1); }
    else{ res.push(v+1); }
    return res;
  }
  int ans1 = res.top(); res.pop();
  int ans2 = res.top(); res.pop();
  if(ans1 % 2 == 0 && ans2 % 2 == 0){ res.push(ans1+ans2); res.push(1); }
  else{ res.push(ans1+ans2+1); }
  return res;
}
*/

priority_queue<int> dfs(int p, int pre=-1){
  priority_queue<int> res;
  for(int e : E[p]) if(e != pre){
    auto res2 = dfs(e,p);
    if(res.size() < res2.size()) swap(res, res2);
    while(!res2.empty()){ res.push(res2.top()); res2.pop(); }
  }
  if(res.size() == 0){
    res.push(1);
    return res;
  }
  if(res.size() == 1){
    auto v = res.top(); res.pop();
    res.push(v+1);
    return res;
  }
  int ans1 = res.top(); res.pop();
  int ans2 = res.top(); res.pop();
  if(ans1 % 2 == 0 && ans2 % 2 == 0){ res.push(ans1+ans2); res.push(1); }
  else{ res.push(ans1+ans2+1); }
  return res;
}

int main(){
  cin >> N;
  E.resize(N);
  rep(i,N-1){
    int u,v; cin >> u >> v; u--; v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }

  auto ans = dfs(0).top();
  cout << ans << endl;
  return 0;
}




struct ios_do_not_sync {
    ios_do_not_sync() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0