#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5+10; typedef long long int ll; typedef pair<int, ll> pill; const ll INF = 1000000000000000000LL; #define mp make_pair #define pb push_back #define st first #define nd second int n, m, p[MAXN]; vector<int> g[MAXN]; vector<pill> r[MAXN]; void dfs_up(int u) { if (u <= m) return; //r[u].clear(); set<int> candidates; for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; if (v == p[u]) continue; p[v] = u; dfs_up(v); for (int j = 0; j < int(r[v].size()); j++) candidates.insert(r[v][j].st); } r[u].clear(); while (!candidates.empty()) { r[u].pb(mp(*candidates.begin(), 0)); candidates.erase(candidates.begin()); } /*if (int(candidates.size())%2 == 0) { r[u].pb(mp(candidates[int(candidates.size())/2-1], 0)); r[u].pb(mp(candidates[int(candidates.size())/2], 0)); } else { int i = int(candidates.size())/2; if (i-1 >= 0) r[u].pb(mp(candidates[i-1], 0)); r[u].pb(mp(candidates[i], 0)); if (i+1 < int(candidates.size())) r[u].pb(mp(candidates[i+1], 0)); }*/ } void dfs_down(int u) { set<int> candidates; for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; for (int j = 0; j < int(r[v].size()); j++) { candidates.insert(r[v][j].st); } } r[u].clear(); while (!candidates.empty()) { r[u].pb(mp(*candidates.begin(), 0)); candidates.erase(candidates.begin()); } /*if (int(candidates.size())%2 == 0) { r[u].pb(mp(candidates[int(candidates.size())/2-1], 0)); r[u].pb(mp(candidates[int(candidates.size())/2], 0)); } else { int i = int(candidates.size())/2; if (i-1 >= 0) r[u].pb(mp(candidates[i-1], 0)); r[u].pb(mp(candidates[i], 0)); if (i+1 < int(candidates.size())) r[u].pb(mp(candidates[i+1], 0)); }*/ for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; if (v == p[u] || v <= m) continue; dfs_down(v); } } void dfs_solve(int u) { for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; if (v == p[u]) continue; dfs_solve(v); } for (int c = 0; c < int(r[u].size()); c++) { r[u][c].nd = 0; for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; if (v == p[u]) continue; ll tmp = INF; for (int k = 0; k < int(r[v].size()); k++) { tmp = min(tmp, abs(r[u][c].st-r[v][k].st) + r[v][k].nd); } r[u][c].nd += tmp; } //printf("r[%d][%d] = (%d, %lld)\n", u, c, r[u][c].st, r[u][c].nd); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); g[u].pb(v); g[v].pb(u); } for (int i = 1; i <= m; i++) { int _r; scanf("%d", &_r); r[i].pb(mp(_r, 0)); } ll kochamjulie = INF; dfs_up(n); dfs_down(n); dfs_up(n); dfs_down(n); dfs_solve(n); for (int i = 0; i < int(r[n].size()); i++) { kochamjulie = min(kochamjulie, r[n][i].nd); } printf("%lld\n", kochamjulie); 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 131 132 133 134 135 | #include <bits/stdc++.h> using namespace std; const int MAXN = 5e5+10; typedef long long int ll; typedef pair<int, ll> pill; const ll INF = 1000000000000000000LL; #define mp make_pair #define pb push_back #define st first #define nd second int n, m, p[MAXN]; vector<int> g[MAXN]; vector<pill> r[MAXN]; void dfs_up(int u) { if (u <= m) return; //r[u].clear(); set<int> candidates; for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; if (v == p[u]) continue; p[v] = u; dfs_up(v); for (int j = 0; j < int(r[v].size()); j++) candidates.insert(r[v][j].st); } r[u].clear(); while (!candidates.empty()) { r[u].pb(mp(*candidates.begin(), 0)); candidates.erase(candidates.begin()); } /*if (int(candidates.size())%2 == 0) { r[u].pb(mp(candidates[int(candidates.size())/2-1], 0)); r[u].pb(mp(candidates[int(candidates.size())/2], 0)); } else { int i = int(candidates.size())/2; if (i-1 >= 0) r[u].pb(mp(candidates[i-1], 0)); r[u].pb(mp(candidates[i], 0)); if (i+1 < int(candidates.size())) r[u].pb(mp(candidates[i+1], 0)); }*/ } void dfs_down(int u) { set<int> candidates; for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; for (int j = 0; j < int(r[v].size()); j++) { candidates.insert(r[v][j].st); } } r[u].clear(); while (!candidates.empty()) { r[u].pb(mp(*candidates.begin(), 0)); candidates.erase(candidates.begin()); } /*if (int(candidates.size())%2 == 0) { r[u].pb(mp(candidates[int(candidates.size())/2-1], 0)); r[u].pb(mp(candidates[int(candidates.size())/2], 0)); } else { int i = int(candidates.size())/2; if (i-1 >= 0) r[u].pb(mp(candidates[i-1], 0)); r[u].pb(mp(candidates[i], 0)); if (i+1 < int(candidates.size())) r[u].pb(mp(candidates[i+1], 0)); }*/ for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; if (v == p[u] || v <= m) continue; dfs_down(v); } } void dfs_solve(int u) { for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; if (v == p[u]) continue; dfs_solve(v); } for (int c = 0; c < int(r[u].size()); c++) { r[u][c].nd = 0; for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; if (v == p[u]) continue; ll tmp = INF; for (int k = 0; k < int(r[v].size()); k++) { tmp = min(tmp, abs(r[u][c].st-r[v][k].st) + r[v][k].nd); } r[u][c].nd += tmp; } //printf("r[%d][%d] = (%d, %lld)\n", u, c, r[u][c].st, r[u][c].nd); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); g[u].pb(v); g[v].pb(u); } for (int i = 1; i <= m; i++) { int _r; scanf("%d", &_r); r[i].pb(mp(_r, 0)); } ll kochamjulie = INF; dfs_up(n); dfs_down(n); dfs_up(n); dfs_down(n); dfs_solve(n); for (int i = 0; i < int(r[n].size()); i++) { kochamjulie = min(kochamjulie, r[n][i].nd); } printf("%lld\n", kochamjulie); return 0; } |