#include <algorithm> #include <cassert> #include <cctype> #include <iostream> #include <unistd.h> #include <utility> #include <vector> using namespace std; typedef long long LL; class Input { public: Input() { bufpos = bufend = buffer; eof = false; } bool Eof() { return eof; } char Peek() { if(bufpos == bufend) Grab(); return *bufpos; } unsigned char UPeek() { return static_cast<unsigned char>(Peek()); } void SkipWS(); template<class T> T Get(); void operator()() {} template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) { arg = Get<Arg>(); operator()(args...); } private: static const int BUFSIZE = 1<<16; char buffer[BUFSIZE]; char *bufpos; char *bufend; bool eof; void Grab(); }; void Input::Grab() { if(eof) return; bufpos = buffer; bufend = buffer + read(0, buffer, BUFSIZE); if(bufend==bufpos) { eof=true; *bufpos=0; } } template<> inline char Input::Get<char>() { char res = Peek(); ++bufpos; return res; } void Input::SkipWS() { while(isspace(UPeek())) Get<char>(); } template<> unsigned Input::Get<unsigned>() { SkipWS(); unsigned x = 0; while(isdigit(UPeek())) { x = 10u * x + (Get<char>()-'0'); } return x; } template<> int Input::Get<int>() { SkipWS(); bool neg = false; if(Peek()=='-') { neg=true; Get<char>(); } unsigned x = Get<unsigned>(); if (neg) x = -x; return static_cast<int>(x); } Input IN; //---------------- struct Node { int degree; Node **neighbors; Node *parent; // Optimal values are in range optMin..optMax, and subtree then costs optCost. int optMin; int optMax; long long optCost; Node() : degree{0}, neighbors{nullptr}, parent{nullptr}, optMin{0}, optMax{0}, optCost{0} {} }; int numForeign; vector<Node> nodes; // 16 MB vector<pair<Node*,Node*>> edges; // 4 MB vector<Node*> neighborStorage; // 4 MB Node *root; vector<Node*> topologicalOrder; // 2 MB void ReadInput() { int n; IN(n, numForeign); nodes.resize(n); edges.resize(n-1); for (auto &e : edges) { int a, b; IN(a, b); e.first = &nodes[a-1]; e.second = &nodes[b-1]; ++e.first->degree; ++e.second->degree; } for (int i=0; i<numForeign; ++i) { Node &node = nodes[i]; int r; IN(r); node.optMin = r; node.optMax = r; } } void ComputeNeighbors() { neighborStorage.resize(2 * edges.size()); // Prepare to fill backwards. Node **nextStorage = &neighborStorage[0];; for (Node &node : nodes) { nextStorage += node.degree; node.neighbors = nextStorage; } assert(nextStorage == &neighborStorage[0] + neighborStorage.size()); // Fill. for (const auto &e : edges) { *(--e.first->neighbors) = e.second; *(--e.second->neighbors) = e.first; } } void TopologicalSort() { root = &nodes[0]; topologicalOrder.reserve(nodes.size()); vector<Node*> st; // 2 MB st.reserve(nodes.size()); st.push_back(root); while (!st.empty()) { Node *const node = st.back(); st.pop_back(); topologicalOrder.push_back(node); for (int i=0; i < node->degree; ++i) { Node *const x = node->neighbors[i]; if (x == node->parent) continue; x->parent = node; st.push_back(x); } } assert(topologicalOrder.size() == nodes.size()); reverse(topologicalOrder.begin(), topologicalOrder.end()); } long long Solve() { vector<int> kinks; // 4 MB kinks.reserve(2 * nodes.size()); for (Node *const node : topologicalOrder) { if (node >= &nodes[0] + numForeign) { // Compute kinks. kinks.clear(); for (int i=0; i < node->degree; ++i) { const Node *const child = node->neighbors[i]; if (child == node->parent) continue; kinks.push_back(child->optMin); kinks.push_back(child->optMax); } assert(kinks.size() >= 2u); vector<int>::iterator middle = kinks.begin() + kinks.size() / 2; nth_element(kinks.begin(), middle, kinks.end()); node->optMax = *middle; node->optMin = *max_element(kinks.begin(), middle); } node->optCost = 0; int val = node->optMin; for (int i=0; i < node->degree; ++i) { const Node *const child = node->neighbors[i]; if (child == node->parent) continue; node->optCost += child->optCost; if (val > child->optMax) { node->optCost += (val - child->optMax); } else if (val < child->optMin) { node->optCost += (child->optMin - val); } } } return root->optCost; } int main() { ReadInput(); ComputeNeighbors(); TopologicalSort(); long long res = Solve(); cout << res << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | #include <algorithm> #include <cassert> #include <cctype> #include <iostream> #include <unistd.h> #include <utility> #include <vector> using namespace std; typedef long long LL; class Input { public: Input() { bufpos = bufend = buffer; eof = false; } bool Eof() { return eof; } char Peek() { if(bufpos == bufend) Grab(); return *bufpos; } unsigned char UPeek() { return static_cast<unsigned char>(Peek()); } void SkipWS(); template<class T> T Get(); void operator()() {} template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) { arg = Get<Arg>(); operator()(args...); } private: static const int BUFSIZE = 1<<16; char buffer[BUFSIZE]; char *bufpos; char *bufend; bool eof; void Grab(); }; void Input::Grab() { if(eof) return; bufpos = buffer; bufend = buffer + read(0, buffer, BUFSIZE); if(bufend==bufpos) { eof=true; *bufpos=0; } } template<> inline char Input::Get<char>() { char res = Peek(); ++bufpos; return res; } void Input::SkipWS() { while(isspace(UPeek())) Get<char>(); } template<> unsigned Input::Get<unsigned>() { SkipWS(); unsigned x = 0; while(isdigit(UPeek())) { x = 10u * x + (Get<char>()-'0'); } return x; } template<> int Input::Get<int>() { SkipWS(); bool neg = false; if(Peek()=='-') { neg=true; Get<char>(); } unsigned x = Get<unsigned>(); if (neg) x = -x; return static_cast<int>(x); } Input IN; //---------------- struct Node { int degree; Node **neighbors; Node *parent; // Optimal values are in range optMin..optMax, and subtree then costs optCost. int optMin; int optMax; long long optCost; Node() : degree{0}, neighbors{nullptr}, parent{nullptr}, optMin{0}, optMax{0}, optCost{0} {} }; int numForeign; vector<Node> nodes; // 16 MB vector<pair<Node*,Node*>> edges; // 4 MB vector<Node*> neighborStorage; // 4 MB Node *root; vector<Node*> topologicalOrder; // 2 MB void ReadInput() { int n; IN(n, numForeign); nodes.resize(n); edges.resize(n-1); for (auto &e : edges) { int a, b; IN(a, b); e.first = &nodes[a-1]; e.second = &nodes[b-1]; ++e.first->degree; ++e.second->degree; } for (int i=0; i<numForeign; ++i) { Node &node = nodes[i]; int r; IN(r); node.optMin = r; node.optMax = r; } } void ComputeNeighbors() { neighborStorage.resize(2 * edges.size()); // Prepare to fill backwards. Node **nextStorage = &neighborStorage[0];; for (Node &node : nodes) { nextStorage += node.degree; node.neighbors = nextStorage; } assert(nextStorage == &neighborStorage[0] + neighborStorage.size()); // Fill. for (const auto &e : edges) { *(--e.first->neighbors) = e.second; *(--e.second->neighbors) = e.first; } } void TopologicalSort() { root = &nodes[0]; topologicalOrder.reserve(nodes.size()); vector<Node*> st; // 2 MB st.reserve(nodes.size()); st.push_back(root); while (!st.empty()) { Node *const node = st.back(); st.pop_back(); topologicalOrder.push_back(node); for (int i=0; i < node->degree; ++i) { Node *const x = node->neighbors[i]; if (x == node->parent) continue; x->parent = node; st.push_back(x); } } assert(topologicalOrder.size() == nodes.size()); reverse(topologicalOrder.begin(), topologicalOrder.end()); } long long Solve() { vector<int> kinks; // 4 MB kinks.reserve(2 * nodes.size()); for (Node *const node : topologicalOrder) { if (node >= &nodes[0] + numForeign) { // Compute kinks. kinks.clear(); for (int i=0; i < node->degree; ++i) { const Node *const child = node->neighbors[i]; if (child == node->parent) continue; kinks.push_back(child->optMin); kinks.push_back(child->optMax); } assert(kinks.size() >= 2u); vector<int>::iterator middle = kinks.begin() + kinks.size() / 2; nth_element(kinks.begin(), middle, kinks.end()); node->optMax = *middle; node->optMin = *max_element(kinks.begin(), middle); } node->optCost = 0; int val = node->optMin; for (int i=0; i < node->degree; ++i) { const Node *const child = node->neighbors[i]; if (child == node->parent) continue; node->optCost += child->optCost; if (val > child->optMax) { node->optCost += (val - child->optMax); } else if (val < child->optMin) { node->optCost += (child->optMin - val); } } } return root->optCost; } int main() { ReadInput(); ComputeNeighbors(); TopologicalSort(); long long res = Solve(); cout << res << '\n'; } |