提出 #30680400
ソースコード 拡げる
Copy
#include <vector>#include <algorithm>namespace nachia{template<class Elem>struct LeftistHeap{private:struct Node{Node* l = nullptr;Node* r = nullptr;int height = 1;Elem key;Node(Elem new_key) : key(new_key) {}};Node* root;int m_size;
#include <vector> #include <algorithm> namespace nachia{ template< class Elem > struct LeftistHeap{ private: struct Node{ Node* l = nullptr; Node* r = nullptr; int height = 1; Elem key; Node(Elem new_key) : key(new_key) {} }; Node* root; int m_size; public: LeftistHeap(){ root = nullptr; m_size = 0; } LeftistHeap(LeftistHeap&& src){ root = src.root; m_size = src.m_size; src.root = nullptr; src.m_size = 0; } LeftistHeap& operator=(LeftistHeap&& src){ root = src.root; m_size = src.m_size; src.root = nullptr; src.m_size = 0; return *this; } void clear(){ if(root){ ::std::vector<Node*> p = {root}; while(p.size()){ auto c = p.back(); p.pop_back(); if(c->l) p.push_back(c->l); if(c->r) p.push_back(c->r); delete(c); } root = nullptr; } } ~LeftistHeap(){ clear(); } private: static Node* meld_node_norecursive(Node* root, Node* anotherroot){ if(anotherroot == nullptr){ return root; } if(root == nullptr){ root = anotherroot; return root; } ::std::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) ::std::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){ std::swap(c->l, c->r); c->height = 1; } else if(c->r == nullptr){ c->height = 1; } else{ if(c->l->height < c->r->height){ ::std::swap(c->l, c->r); } c->height = c->r->height + 1; } st_ritr++; } return root; } public: void push(const Elem& val){ Node* new_node = new Node(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(LeftistHeap& r){ root = meld_node_norecursive(root, r.root); m_size += r.m_size; r.root = nullptr; r.m_size = 0; } }; } // namespace nachia #include <iostream> #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<(int)(n); i++) using m32 = atcoder::static_modint<998244353>; namespace Que{ using LeftistHeap = nachia::LeftistHeap<int>; } int main(){ int N; cin >> N; vector<int> A(N); A[0] = 0; for(int i=1; i<N; i++) cin >> A[i]; vector<vector<int>> adj(N); rep(i,N-1){ int u,v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } vector<int> bfs = {0}; vector<int> P(N, -1); rep(i,bfs.size()){ int p = bfs[i]; for(int e : adj[p]) if(P[p] != e){ bfs.push_back(e); P[e] = p; } } vector<Que::LeftistHeap> dp(N); for(int i=N-1; i>=1; i--){ int p = bfs[i]; if(dp[p].size()) dp[p].pop(); dp[p].push(A[p]); dp[P[p]].meld(dp[p]); } if(dp[0].size()) dp[0].pop(); int ans = (dp[0].size() ? dp[0].top() : 0); cout << ans << '\n'; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } ios_do_not_sync_instance;
提出情報
提出日時 | |
---|---|
問題 | G - Game on Tree 3 |
ユーザ | Nachia |
言語 | C++ (GCC 9.2.1) |
得点 | 600 |
コード長 | 4397 Byte |
結果 | AC |
実行時間 | 125 ms |
メモリ | 26748 KB |
ジャッジ結果
セット名 | Sample | All | ||||
---|---|---|---|---|---|---|
得点 / 配点 | 0 / 0 | 600 / 600 | ||||
結果 |
|
|
セット名 | テストケース |
---|---|
Sample | example0.txt, example1.txt |
All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, 060.txt, 061.txt, 062.txt, example0.txt, example1.txt |
ケース名 | 結果 | 実行時間 | メモリ |
---|---|---|---|
000.txt | AC | 5 ms | 3628 KB |
001.txt | AC | 91 ms | 26748 KB |
002.txt | AC | 109 ms | 24928 KB |
003.txt | AC | 117 ms | 23800 KB |
004.txt | AC | 107 ms | 25004 KB |
005.txt | AC | 110 ms | 24656 KB |
006.txt | AC | 95 ms | 21356 KB |
007.txt | AC | 75 ms | 19592 KB |
008.txt | AC | 86 ms | 23172 KB |
009.txt | AC | 86 ms | 23208 KB |
010.txt | AC | 87 ms | 22940 KB |
011.txt | AC | 86 ms | 23060 KB |
012.txt | AC | 77 ms | 21296 KB |
013.txt | AC | 51 ms | 11844 KB |
014.txt | AC | 111 ms | 22652 KB |
015.txt | AC | 5 ms | 3956 KB |
016.txt | AC | 90 ms | 18004 KB |
017.txt | AC | 85 ms | 18116 KB |
018.txt | AC | 88 ms | 18476 KB |
019.txt | AC | 33 ms | 7964 KB |
020.txt | AC | 33 ms | 8168 KB |
021.txt | AC | 39 ms | 9424 KB |
022.txt | AC | 87 ms | 18088 KB |
023.txt | AC | 118 ms | 22904 KB |
024.txt | AC | 111 ms | 23036 KB |
025.txt | AC | 122 ms | 22952 KB |
026.txt | AC | 120 ms | 23080 KB |
027.txt | AC | 123 ms | 23020 KB |
028.txt | AC | 125 ms | 23084 KB |
029.txt | AC | 118 ms | 23044 KB |
030.txt | AC | 117 ms | 22980 KB |
031.txt | AC | 120 ms | 22976 KB |
032.txt | AC | 119 ms | 23084 KB |
033.txt | AC | 123 ms | 22908 KB |
034.txt | AC | 115 ms | 23004 KB |
035.txt | AC | 118 ms | 23052 KB |
036.txt | AC | 118 ms | 22848 KB |
037.txt | AC | 108 ms | 23040 KB |
038.txt | AC | 107 ms | 22908 KB |
039.txt | AC | 109 ms | 23036 KB |
040.txt | AC | 109 ms | 22972 KB |
041.txt | AC | 117 ms | 23080 KB |
042.txt | AC | 106 ms | 23028 KB |
043.txt | AC | 105 ms | 22976 KB |
044.txt | AC | 112 ms | 22952 KB |
045.txt | AC | 108 ms | 22976 KB |
046.txt | AC | 107 ms | 22948 KB |
047.txt | AC | 113 ms | 22956 KB |
048.txt | AC | 108 ms | 23040 KB |
049.txt | AC | 109 ms | 23036 KB |
050.txt | AC | 109 ms | 22892 KB |
051.txt | AC | 112 ms | 23028 KB |
052.txt | AC | 110 ms | 23080 KB |
053.txt | AC | 109 ms | 22904 KB |
054.txt | AC | 117 ms | 22908 KB |
055.txt | AC | 110 ms | 23012 KB |
056.txt | AC | 110 ms | 22896 KB |
057.txt | AC | 110 ms | 23048 KB |
058.txt | AC | 108 ms | 23024 KB |
059.txt | AC | 109 ms | 22904 KB |
060.txt | AC | 112 ms | 23080 KB |
061.txt | AC | 111 ms | 23016 KB |
062.txt | AC | 108 ms | 23084 KB |
example0.txt | AC | 2 ms | 3468 KB |
example1.txt | AC | 2 ms | 3568 KB |