#include <algorithm> #include <cstdlib> #include <iostream> #include <utility> #include <vector> using namespace std; namespace { class Solver { private: int n; int m; using Range = pair<int, int>; vector<vector<int>> neigh; vector<int> width; void dfs(int v, vector<int>& res, vector<int>& parent) const { if (v < m) return; for (auto const& w: neigh[v]) { if (w == parent[v]) continue; parent[w] = v; dfs(w, res, parent); } res.push_back(v); } static int cost(Range const& r, int x) { if (x < r.first) return r.first - x; if (x > r.second) return x - r.second; return 0; } void calculate(int v, vector<Range>& range, vector<long long>& res, vector<int> const& parent) const { res[v] = 0; vector<int> subs; subs.reserve(2 * neigh[v].size()); for (auto const& w: neigh[v]) { if (w == parent[v]) continue; subs.push_back(range[w].first); subs.push_back(range[w].second); res[v] += res[w]; } auto mid = subs.size() / 2 - 1; nth_element(subs.begin(), subs.begin() + mid, subs.end()); range[v].first = subs[mid]; range[v].second = *min_element(subs.begin() + mid + 1, subs.end()); auto x = range[v].first; for (auto const& w: neigh[v]) { if (w == parent[v]) continue; res[v] += cost(range[w], x); } } public: Solver(int n_, int m_): n{n_}, m{m_}, neigh(n), width(m) { } void add(int a, int b) { neigh[a].push_back(b); neigh[b].push_back(a); } void set(int a, int r) { width[a] = r; } long long operator()() const { if (n == 2) { return abs(width[0] - width[1]); } vector<int> todo; todo.reserve(n - m); vector<int> parent(n); parent[m] = -1; dfs(m, todo, parent); vector<Range> range(n); for (int i = 0; i < m; ++i) { range[i] = {width[i], width[i]}; } vector<long long> res(n); for (auto const& v: todo) { calculate(v, range, res, parent); } return res[m]; } }; } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; Solver solver{n, m}; for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; solver.add(a-1, b-1); } for (int i = 0; i < m; ++i) { int r; cin >> r; solver.set(i, r); } cout << solver() << '\n'; return 0; }
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 | #include <algorithm> #include <cstdlib> #include <iostream> #include <utility> #include <vector> using namespace std; namespace { class Solver { private: int n; int m; using Range = pair<int, int>; vector<vector<int>> neigh; vector<int> width; void dfs(int v, vector<int>& res, vector<int>& parent) const { if (v < m) return; for (auto const& w: neigh[v]) { if (w == parent[v]) continue; parent[w] = v; dfs(w, res, parent); } res.push_back(v); } static int cost(Range const& r, int x) { if (x < r.first) return r.first - x; if (x > r.second) return x - r.second; return 0; } void calculate(int v, vector<Range>& range, vector<long long>& res, vector<int> const& parent) const { res[v] = 0; vector<int> subs; subs.reserve(2 * neigh[v].size()); for (auto const& w: neigh[v]) { if (w == parent[v]) continue; subs.push_back(range[w].first); subs.push_back(range[w].second); res[v] += res[w]; } auto mid = subs.size() / 2 - 1; nth_element(subs.begin(), subs.begin() + mid, subs.end()); range[v].first = subs[mid]; range[v].second = *min_element(subs.begin() + mid + 1, subs.end()); auto x = range[v].first; for (auto const& w: neigh[v]) { if (w == parent[v]) continue; res[v] += cost(range[w], x); } } public: Solver(int n_, int m_): n{n_}, m{m_}, neigh(n), width(m) { } void add(int a, int b) { neigh[a].push_back(b); neigh[b].push_back(a); } void set(int a, int r) { width[a] = r; } long long operator()() const { if (n == 2) { return abs(width[0] - width[1]); } vector<int> todo; todo.reserve(n - m); vector<int> parent(n); parent[m] = -1; dfs(m, todo, parent); vector<Range> range(n); for (int i = 0; i < m; ++i) { range[i] = {width[i], width[i]}; } vector<long long> res(n); for (auto const& v: todo) { calculate(v, range, res, parent); } return res[m]; } }; } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; Solver solver{n, m}; for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; solver.add(a-1, b-1); } for (int i = 0; i < m; ++i) { int r; cin >> r; solver.set(i, r); } cout << solver() << '\n'; return 0; } |