結果

問題 No.1752 Up-Down Tree
ユーザー 👑 NachiaNachia
提出日時 2021-11-19 23:04:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,788 bytes
コンパイル時間 972 ms
コンパイル使用メモリ 88,492 KB
実行使用メモリ 14,680 KB
最終ジャッジ日時 2023-08-30 10:09:44
合計ジャッジ時間 3,450 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
14,576 KB
testcase_01 AC 64 ms
14,680 KB
testcase_02 AC 58 ms
11,988 KB
testcase_03 AC 62 ms
11,984 KB
testcase_04 AC 61 ms
13,420 KB
testcase_05 AC 66 ms
13,832 KB
testcase_06 AC 63 ms
12,792 KB
testcase_07 AC 64 ms
12,792 KB
testcase_08 AC 30 ms
6,744 KB
testcase_09 AC 53 ms
9,300 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 2 ms
4,380 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#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;
int ans = 0;

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() == 1){
    ans += res.top(); res.pop();
  }
  if(res.size() == 0){
    res.push(1);
    return res;
  }
  int ans1 = res.top(); res.pop();
  int ans2 = res.top(); res.pop();
  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);
  }

  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