#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int maxn = 500500; struct segment { int left, right; segment(int x=0) {left=right=x;} segment(int x, int y) {left=x; right=y;} }; int n,m; vi g[maxn]; int c[maxn]; long long R; segment result[maxn]; segment dfs(int u, int par) { if (u < m) { return result[u] = segment(c[u]); } vector<segment> child; set<pii> events; vi t; FOR(i,g[u].size()) { int v = g[u][i]; if (v==par) continue; dfs(v,u); //printf("result(%d) = %d %d (%d)\n", v, result[v].left, result[v].right, result[v].ispoint); //printf("Teraz wynik to %lld = %lld + %lld\n", R, rtemp, R-rtemp); events.insert(mp(result[v].left, child.size())); if (result[v].left != result[v].right) events.insert(mp(result[v].right, child.size())); child.pb(result[v]); } /* printf("wierzcholek %d: ", u); FOR(i,child.size()) printf("[%d %d (%d)], ", child[i].left, child[i].right, child[i].ispoint); printf("\n"); printf("Wynik do tej pory to %lld\n", R); */ int lastleft = -1, lastright = -1; while (!events.empty()) { // zewnetrzne elementy int lowpos = events.begin()->se, highpos = events.rbegin()->se; segment low = child[lowpos], high = child[highpos]; // to juz ostatni if (events.size() == 1) { if (low.left == -1) { if (lastleft == -1) { low.left=low.right; } else { low.left = lastleft; } } if (low.right == -1) { if (lastright == -1) { low.right=low.left; } else { low.right = lastright; } } return result[u] = low; } // przedzial po lewej -- zostaw prawy koniec if (low.left!=low.right && low.left != -1 && low.right != -1) { events.erase(mp(low.left, lowpos)); child[lowpos].left = -1; lastleft = low.left; continue; } // przedzial po prawej -- zostaw lewy koniec if (high.left!=high.right && high.left != -1 && high.right != -1) { events.erase(mp(high.right, highpos)); child[highpos].right = -1; lastright = high.right; continue; } // lewy po lewej i prawy po prawej -- darmowy przedzial if (events.size() == 2 && low.right == -1 && high.left == -1) { segment ret = segment(low.left, high.right); return result[u] = ret; } // lewy koniec po lewej -- za darmo if (low.right == -1) { events.erase(mp(low.left, lowpos)); lastleft = low.left; continue; } // prawy koniec po prawej -- za darmo if (high.left == -1) { events.erase(mp(high.right, highpos)); lastright = high.right; continue; } // lewy koniec po prawej / prawy koniec po lewej / punkt -- usun // ostatni if (events.size() == 2) { segment ret(low.right, high.left); return result[u] = ret; } // nie ostatni events.erase(mp(low.right, lowpos)); events.erase(mp(high.left, highpos)); lastleft = low.right; lastright = high.left; } } int med(int a, int b, int c) { if (a>b) swap(a,b); if (a>c) swap(a,c); if (b>c) swap(b,c); return b; } void dfstest(int u, int par) { if (u < m) return; if (par == -1) { c[u] = result[u].left; } else { if (result[u].left == result[u].right) c[u] = result[u].left; else c[u] = med(result[u].left, result[u].right, c[par]); } FOR(i,g[u].size()) { int v = g[u][i]; if (v==par) continue; dfstest(v,u); } } int main () { scanf("%d%d", &n, &m); FOR(i,n-1) { int a,b; scanf("%d%d", &a, &b); a--; b--; g[a].pb(b); g[b].pb(a); } /* FOR(i,n) { printf("%d: ", i); FOR(j,g[i].size()) printf("%d ", g[i][j]); printf("\n"); } */ FOR(i,m) scanf("%d", &c[i]); dfs(m,-1); //printf("result(%d) = %d %d (%d)\n", m, result[m].left, result[m].right, result[m].ispoint); dfstest(m,-1); R=0; FOR(i,n) FOR(j,g[i].size()) if (c[i] > c[g[i][j]]) R += c[i]-c[g[i][j]]; printf("%lld\n", R); 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 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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int maxn = 500500; struct segment { int left, right; segment(int x=0) {left=right=x;} segment(int x, int y) {left=x; right=y;} }; int n,m; vi g[maxn]; int c[maxn]; long long R; segment result[maxn]; segment dfs(int u, int par) { if (u < m) { return result[u] = segment(c[u]); } vector<segment> child; set<pii> events; vi t; FOR(i,g[u].size()) { int v = g[u][i]; if (v==par) continue; dfs(v,u); //printf("result(%d) = %d %d (%d)\n", v, result[v].left, result[v].right, result[v].ispoint); //printf("Teraz wynik to %lld = %lld + %lld\n", R, rtemp, R-rtemp); events.insert(mp(result[v].left, child.size())); if (result[v].left != result[v].right) events.insert(mp(result[v].right, child.size())); child.pb(result[v]); } /* printf("wierzcholek %d: ", u); FOR(i,child.size()) printf("[%d %d (%d)], ", child[i].left, child[i].right, child[i].ispoint); printf("\n"); printf("Wynik do tej pory to %lld\n", R); */ int lastleft = -1, lastright = -1; while (!events.empty()) { // zewnetrzne elementy int lowpos = events.begin()->se, highpos = events.rbegin()->se; segment low = child[lowpos], high = child[highpos]; // to juz ostatni if (events.size() == 1) { if (low.left == -1) { if (lastleft == -1) { low.left=low.right; } else { low.left = lastleft; } } if (low.right == -1) { if (lastright == -1) { low.right=low.left; } else { low.right = lastright; } } return result[u] = low; } // przedzial po lewej -- zostaw prawy koniec if (low.left!=low.right && low.left != -1 && low.right != -1) { events.erase(mp(low.left, lowpos)); child[lowpos].left = -1; lastleft = low.left; continue; } // przedzial po prawej -- zostaw lewy koniec if (high.left!=high.right && high.left != -1 && high.right != -1) { events.erase(mp(high.right, highpos)); child[highpos].right = -1; lastright = high.right; continue; } // lewy po lewej i prawy po prawej -- darmowy przedzial if (events.size() == 2 && low.right == -1 && high.left == -1) { segment ret = segment(low.left, high.right); return result[u] = ret; } // lewy koniec po lewej -- za darmo if (low.right == -1) { events.erase(mp(low.left, lowpos)); lastleft = low.left; continue; } // prawy koniec po prawej -- za darmo if (high.left == -1) { events.erase(mp(high.right, highpos)); lastright = high.right; continue; } // lewy koniec po prawej / prawy koniec po lewej / punkt -- usun // ostatni if (events.size() == 2) { segment ret(low.right, high.left); return result[u] = ret; } // nie ostatni events.erase(mp(low.right, lowpos)); events.erase(mp(high.left, highpos)); lastleft = low.right; lastright = high.left; } } int med(int a, int b, int c) { if (a>b) swap(a,b); if (a>c) swap(a,c); if (b>c) swap(b,c); return b; } void dfstest(int u, int par) { if (u < m) return; if (par == -1) { c[u] = result[u].left; } else { if (result[u].left == result[u].right) c[u] = result[u].left; else c[u] = med(result[u].left, result[u].right, c[par]); } FOR(i,g[u].size()) { int v = g[u][i]; if (v==par) continue; dfstest(v,u); } } int main () { scanf("%d%d", &n, &m); FOR(i,n-1) { int a,b; scanf("%d%d", &a, &b); a--; b--; g[a].pb(b); g[b].pb(a); } /* FOR(i,n) { printf("%d: ", i); FOR(j,g[i].size()) printf("%d ", g[i][j]); printf("\n"); } */ FOR(i,m) scanf("%d", &c[i]); dfs(m,-1); //printf("result(%d) = %d %d (%d)\n", m, result[m].left, result[m].right, result[m].ispoint); dfstest(m,-1); R=0; FOR(i,n) FOR(j,g[i].size()) if (c[i] > c[g[i][j]]) R += c[i]-c[g[i][j]]; printf("%lld\n", R); return 0; } |