#include <cstdio> #include <vector> #include <algorithm> using namespace std; const long long INF = 1000LL*1000*1000*1000*1000*1000; const int MAXN=500005, MAXM=500005; struct NodeResult { long long cost; int begin, end; NodeResult() : cost(0), begin(0), end(0) {} NodeResult(long long cost, int begin, int end) : cost(cost), begin(begin), end(end) {} }; struct IntervalEnd { int x; int id; }; bool operator<(const IntervalEnd& lhs, const IntervalEnd& rhs) { if (lhs.x < rhs.x) return true; else if (lhs.x > rhs.x) return false; return lhs.id < rhs.id; } int N, M; vector<int> edges[MAXN]; int edgesLeft[MAXN]={}; NodeResult nodeRes[MAXN]; bool done[MAXN]={}; bool inQueue[MAXN]={}; vector<int> processingQueue; vector<IntervalEnd> ends; int intervalCount[MAXN]={}; NodeResult processIntervals() { sort(ends.begin(), ends.end()); //First phase (going right to left, finding cost of left-most): long long curCost=0, lastX=ends.rbegin()->x, curX; int passed=0; for (vector<IntervalEnd>::reverse_iterator rvit=ends.rbegin(); rvit!=ends.rend(); ++rvit) { curX = rvit->x; curCost += passed * (lastX-curX); lastX = curX; --intervalCount[rvit->id]; if (intervalCount[rvit->id]==2) ++passed; } //Second phase (going left to right, finding optimal cost): NodeResult res(INF, -1, -1); lastX = ends.begin()->x; int coming = ends.size()/2; int open=0; int closed=0; int curCostLeftMostX = ends.begin()->x; long long newCost; for (vector<IntervalEnd>::iterator vit=ends.begin(); vit!=ends.end(); ++vit) { curX = vit->x; newCost = curCost + closed * (curX-lastX) - coming * (curX-lastX); if (newCost != curCost) { curCost = newCost; curCostLeftMostX = curX; } if (res.cost > curCost || (res.cost == curCost && res.end-res.begin < curX - curCostLeftMostX)) { res.cost = curCost; res.begin = curCostLeftMostX; res.end = curX; } --intervalCount[vit->id]; if (intervalCount[vit->id]==1) { --coming; ++open; } else if (intervalCount[vit->id]==0) { --open; ++closed; } lastX = curX; } return res; } void addToQueue(int node) { if (inQueue[node]) return; processingQueue.push_back(node); inQueue[node]=true; } void processTree() { long long subtreeCost = 0; for (int i=1; i<=M; ++i) addToQueue(i); for (int i=0; i<(int)processingQueue.size(); ++i) { subtreeCost = 0; ends.clear(); int node = processingQueue[i]; done[node]=true; for (vector<int>::iterator vit=edges[node].begin(); vit!=edges[node].end(); ++vit) { if (done[*vit]) { //Gather data for processing: subtreeCost += nodeRes[*vit].cost; IntervalEnd e1, e2; int intervalId = ends.size()/2; intervalCount[intervalId] = 4; e1.id = e2.id = intervalId; e1.x = nodeRes[*vit].begin; e2.x = nodeRes[*vit].end; ends.push_back(e1); ends.push_back(e2); continue; } else { --edgesLeft[node]; --edgesLeft[*vit]; if (edgesLeft[*vit] <= 1) addToQueue(*vit); } } if (ends.size()!=0) { //Not leafs nodeRes[node] = processIntervals(); nodeRes[node].cost += subtreeCost; } } } int main() { scanf("%d %d", &N, &M); int a, b; for (int i=1; i<=N-1; ++i) { scanf("%d %d", &a, &b); edges[a].push_back(b); edges[b].push_back(a); ++edgesLeft[a]; ++edgesLeft[b]; } int point; for (int i=1; i<=M; ++i) { scanf("%d", &point); nodeRes[i].cost=0; nodeRes[i].begin=point; nodeRes[i].end=point; } //Special case: if (N==M) { //Only leafs => N=M=2 printf("%d\n", abs(nodeRes[1].begin-nodeRes[2].begin)); return 0; } processTree(); int root = *processingQueue.rbegin(); printf("%lld\n", nodeRes[root].cost); 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; const long long INF = 1000LL*1000*1000*1000*1000*1000; const int MAXN=500005, MAXM=500005; struct NodeResult { long long cost; int begin, end; NodeResult() : cost(0), begin(0), end(0) {} NodeResult(long long cost, int begin, int end) : cost(cost), begin(begin), end(end) {} }; struct IntervalEnd { int x; int id; }; bool operator<(const IntervalEnd& lhs, const IntervalEnd& rhs) { if (lhs.x < rhs.x) return true; else if (lhs.x > rhs.x) return false; return lhs.id < rhs.id; } int N, M; vector<int> edges[MAXN]; int edgesLeft[MAXN]={}; NodeResult nodeRes[MAXN]; bool done[MAXN]={}; bool inQueue[MAXN]={}; vector<int> processingQueue; vector<IntervalEnd> ends; int intervalCount[MAXN]={}; NodeResult processIntervals() { sort(ends.begin(), ends.end()); //First phase (going right to left, finding cost of left-most): long long curCost=0, lastX=ends.rbegin()->x, curX; int passed=0; for (vector<IntervalEnd>::reverse_iterator rvit=ends.rbegin(); rvit!=ends.rend(); ++rvit) { curX = rvit->x; curCost += passed * (lastX-curX); lastX = curX; --intervalCount[rvit->id]; if (intervalCount[rvit->id]==2) ++passed; } //Second phase (going left to right, finding optimal cost): NodeResult res(INF, -1, -1); lastX = ends.begin()->x; int coming = ends.size()/2; int open=0; int closed=0; int curCostLeftMostX = ends.begin()->x; long long newCost; for (vector<IntervalEnd>::iterator vit=ends.begin(); vit!=ends.end(); ++vit) { curX = vit->x; newCost = curCost + closed * (curX-lastX) - coming * (curX-lastX); if (newCost != curCost) { curCost = newCost; curCostLeftMostX = curX; } if (res.cost > curCost || (res.cost == curCost && res.end-res.begin < curX - curCostLeftMostX)) { res.cost = curCost; res.begin = curCostLeftMostX; res.end = curX; } --intervalCount[vit->id]; if (intervalCount[vit->id]==1) { --coming; ++open; } else if (intervalCount[vit->id]==0) { --open; ++closed; } lastX = curX; } return res; } void addToQueue(int node) { if (inQueue[node]) return; processingQueue.push_back(node); inQueue[node]=true; } void processTree() { long long subtreeCost = 0; for (int i=1; i<=M; ++i) addToQueue(i); for (int i=0; i<(int)processingQueue.size(); ++i) { subtreeCost = 0; ends.clear(); int node = processingQueue[i]; done[node]=true; for (vector<int>::iterator vit=edges[node].begin(); vit!=edges[node].end(); ++vit) { if (done[*vit]) { //Gather data for processing: subtreeCost += nodeRes[*vit].cost; IntervalEnd e1, e2; int intervalId = ends.size()/2; intervalCount[intervalId] = 4; e1.id = e2.id = intervalId; e1.x = nodeRes[*vit].begin; e2.x = nodeRes[*vit].end; ends.push_back(e1); ends.push_back(e2); continue; } else { --edgesLeft[node]; --edgesLeft[*vit]; if (edgesLeft[*vit] <= 1) addToQueue(*vit); } } if (ends.size()!=0) { //Not leafs nodeRes[node] = processIntervals(); nodeRes[node].cost += subtreeCost; } } } int main() { scanf("%d %d", &N, &M); int a, b; for (int i=1; i<=N-1; ++i) { scanf("%d %d", &a, &b); edges[a].push_back(b); edges[b].push_back(a); ++edgesLeft[a]; ++edgesLeft[b]; } int point; for (int i=1; i<=M; ++i) { scanf("%d", &point); nodeRes[i].cost=0; nodeRes[i].begin=point; nodeRes[i].end=point; } //Special case: if (N==M) { //Only leafs => N=M=2 printf("%d\n", abs(nodeRes[1].begin-nodeRes[2].begin)); return 0; } processTree(); int root = *processingQueue.rbegin(); printf("%lld\n", nodeRes[root].cost); return 0; } |