結果
問題 | No.1752 Up-Down Tree |
ユーザー | 👑 Nachia |
提出日時 | 2021-11-19 23:49:17 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 52 ms / 2,000 ms |
コード長 | 3,560 bytes |
コンパイル時間 | 1,101 ms |
コンパイル使用メモリ | 91,292 KB |
実行使用メモリ | 22,784 KB |
最終ジャッジ日時 | 2024-06-10 11:37:50 |
合計ジャッジ時間 | 2,400 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
22,728 KB |
testcase_01 | AC | 50 ms
22,784 KB |
testcase_02 | AC | 34 ms
9,740 KB |
testcase_03 | AC | 30 ms
9,872 KB |
testcase_04 | AC | 45 ms
16,256 KB |
testcase_05 | AC | 52 ms
18,560 KB |
testcase_06 | AC | 44 ms
14,240 KB |
testcase_07 | AC | 42 ms
14,244 KB |
testcase_08 | AC | 27 ms
6,940 KB |
testcase_09 | AC | 42 ms
8,832 KB |
testcase_10 | AC | 2 ms
6,940 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,948 KB |
testcase_13 | AC | 20 ms
6,940 KB |
testcase_14 | AC | 25 ms
6,940 KB |
testcase_15 | AC | 6 ms
6,944 KB |
testcase_16 | AC | 22 ms
6,944 KB |
testcase_17 | AC | 19 ms
6,944 KB |
testcase_18 | AC | 44 ms
9,088 KB |
testcase_19 | AC | 45 ms
9,088 KB |
testcase_20 | AC | 2 ms
6,944 KB |
testcase_21 | AC | 2 ms
6,940 KB |
ソースコード
#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;