#include<cstdio> #include<iostream> #include<algorithm> #include<vector> using namespace std; const int N=500000; vector<int> t[N]; int nums[N]; int n,m; struct interval { int p; int k; interval(): p(-1), k(-1) {} interval(int pp, int kk): p(pp), k(kk) {} } intervals[N]; struct point { int x; bool p; point(int xx, bool pp): x(xx), p(pp) {} bool operator<(point a) const { return x < a.x;} }; interval getInterval(vector<point>&& points) { interval ii(-1,-1); sort(points.begin(),points.end()); int finished=0; int x=0; int untouched=points.size()/2; int i=0; while(i<points.size()) { int f_prev=finished; int u_prev=untouched; int x_prev=x; x=points[i].x; while(i < points.size() && points[i].x==x) { if(points[i].p) --untouched; else ++finished; ++i; } // pierwszy moment gdy nie oplaca sie juz isc w prawo if(untouched < finished) { if(u_prev==f_prev) ii.p=x_prev; else ii.p=x; ii.k=x; break; } } return ii; } void doIntervals(int v,int parent) { if(v < m) { //lisc intervals[v].p=nums[v]; intervals[v].k=nums[v]; return; } vector<point> points; for(auto w : t[v]) if(w!=parent) { //if(v==4) cout << w << endl; doIntervals(w,v); //if(v==4) cout << intervals[w].p << ", " << intervals[w].k << endl; points.push_back(point(intervals[w].p,true)); points.push_back(point(intervals[w].k,false)); } //if(v==4) { for(auto poi : points) cout << poi.x << ", " << poi.p << " "; // cout << endl; //} intervals[v]=getInterval(move(points)); } void propagateNums(int v,int parent) { if(v < m) return; if(nums[parent] > intervals[v].k) nums[v]=intervals[v].k; else { if(nums[parent] < intervals[v].p) nums[v]=intervals[v].p; else nums[v]=nums[parent]; } for(auto w : t[v]) if(w!=parent) propagateNums(w,v); } long long cost(int v, int parent) { if(v < m) return 0; long long c=0; for(auto w : t[v]) if(w!=parent) { c+=cost(w,v); c+=abs(nums[v]-nums[w]); } return c; } int main() { scanf("%d",&n); scanf("%d",&m); int v,w; for(int i=0; i<n-1; ++i) { scanf("%d%d",&v,&w); t[v-1].push_back(w-1); t[w-1].push_back(v-1); } fill(nums,nums+N,0); for(int i=0; i<m; ++i) scanf("%d",nums+i); int root=m; //points.clear(); //points.push_back(point(1,true)); //points.push_back(point(6,false)); //points.push_back(point(5,true)); //points.push_back(point(8,false)); // points.push_back(point(10,true)); // points.push_back(point(10,false)); // points.push_back(point(11,true)); // points.push_back(point(11,false)); // points.push_back(point(8,true)); // points.push_back(point(8,true)); // points.push_back(point(8,true)); // points.push_back(point(9,false)); // points.push_back(point(10,false)); // points.push_back(point(11,false)); //interval ii = getInterval(); //cout << ii.p << ", " << ii.k << endl; doIntervals(root,-1); //cout << "root(5):" << endl; //cout << intervals[root].p << ", " << intervals[root].k << endl; //cout << "6:" << endl; //cout << intervals[5].p << ", " << intervals[5].k << endl; nums[root]=intervals[root].p; propagateNums(root,-1); //cout << "4:" << nums[4] << endl; //cout << "5:" << nums[5] << endl; printf("%lld\n",cost(root,-1)); 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 | #include<cstdio> #include<iostream> #include<algorithm> #include<vector> using namespace std; const int N=500000; vector<int> t[N]; int nums[N]; int n,m; struct interval { int p; int k; interval(): p(-1), k(-1) {} interval(int pp, int kk): p(pp), k(kk) {} } intervals[N]; struct point { int x; bool p; point(int xx, bool pp): x(xx), p(pp) {} bool operator<(point a) const { return x < a.x;} }; interval getInterval(vector<point>&& points) { interval ii(-1,-1); sort(points.begin(),points.end()); int finished=0; int x=0; int untouched=points.size()/2; int i=0; while(i<points.size()) { int f_prev=finished; int u_prev=untouched; int x_prev=x; x=points[i].x; while(i < points.size() && points[i].x==x) { if(points[i].p) --untouched; else ++finished; ++i; } // pierwszy moment gdy nie oplaca sie juz isc w prawo if(untouched < finished) { if(u_prev==f_prev) ii.p=x_prev; else ii.p=x; ii.k=x; break; } } return ii; } void doIntervals(int v,int parent) { if(v < m) { //lisc intervals[v].p=nums[v]; intervals[v].k=nums[v]; return; } vector<point> points; for(auto w : t[v]) if(w!=parent) { //if(v==4) cout << w << endl; doIntervals(w,v); //if(v==4) cout << intervals[w].p << ", " << intervals[w].k << endl; points.push_back(point(intervals[w].p,true)); points.push_back(point(intervals[w].k,false)); } //if(v==4) { for(auto poi : points) cout << poi.x << ", " << poi.p << " "; // cout << endl; //} intervals[v]=getInterval(move(points)); } void propagateNums(int v,int parent) { if(v < m) return; if(nums[parent] > intervals[v].k) nums[v]=intervals[v].k; else { if(nums[parent] < intervals[v].p) nums[v]=intervals[v].p; else nums[v]=nums[parent]; } for(auto w : t[v]) if(w!=parent) propagateNums(w,v); } long long cost(int v, int parent) { if(v < m) return 0; long long c=0; for(auto w : t[v]) if(w!=parent) { c+=cost(w,v); c+=abs(nums[v]-nums[w]); } return c; } int main() { scanf("%d",&n); scanf("%d",&m); int v,w; for(int i=0; i<n-1; ++i) { scanf("%d%d",&v,&w); t[v-1].push_back(w-1); t[w-1].push_back(v-1); } fill(nums,nums+N,0); for(int i=0; i<m; ++i) scanf("%d",nums+i); int root=m; //points.clear(); //points.push_back(point(1,true)); //points.push_back(point(6,false)); //points.push_back(point(5,true)); //points.push_back(point(8,false)); // points.push_back(point(10,true)); // points.push_back(point(10,false)); // points.push_back(point(11,true)); // points.push_back(point(11,false)); // points.push_back(point(8,true)); // points.push_back(point(8,true)); // points.push_back(point(8,true)); // points.push_back(point(9,false)); // points.push_back(point(10,false)); // points.push_back(point(11,false)); //interval ii = getInterval(); //cout << ii.p << ", " << ii.k << endl; doIntervals(root,-1); //cout << "root(5):" << endl; //cout << intervals[root].p << ", " << intervals[root].k << endl; //cout << "6:" << endl; //cout << intervals[5].p << ", " << intervals[5].k << endl; nums[root]=intervals[root].p; propagateNums(root,-1); //cout << "4:" << nums[4] << endl; //cout << "5:" << nums[5] << endl; printf("%lld\n",cost(root,-1)); return 0; } |