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
| #include<bits/stdc++.h> #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> #define W while #define I inline #define RI register int #define LL long long #define Cn const #define CI Cn int& #define gc getchar #define D isdigit(c=gc()) #define pc(c) putchar((c)) #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) using namespace std; namespace Debug{ Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; namespace FastIO{ Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),D);x*=f;} Ts I void read(Ty& x,Ar&... y){read(x),read(y...);} Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);} Tp I void writeln(Cn Ty& x){write(x),pc('\n');} }using namespace FastIO; Cn int N=2.5e5+10;Cn LL inf=1e13; int n,fir[N],nxt[N<<1],son[N<<1],w[N<<1],tot;LL d[N],Ans; #define PA pair<int,int> #define MP make_pair #define fi first #define se second PA a[N]; I void Add(CI x,CI y,CI z){nxt[++tot]=fir[x],fir[x]=tot,son[tot]=y,w[tot]=z;} #define to son[i] class Tree{ private: int cnt;struct node{int l,r,d;LL v;}T[N*40]; public: int rt[N]; I int M(RI x,RI y){ if(!x!y) return x+y;if(T[x].v>T[y].v) swap(x,y); T[x].r=M(T[x].r,y);if(T[T[x].l].d<T[T[x].r].d) swap(T[x].l,T[x].r); return T[x].d=T[T[x].r].d+1,x; } I void P(int& x,LL v){T[++cnt]=(node){0,0,0,v},x=M(x,cnt);} I void O(int& x){x=M(T[x].l,T[x].r);} I LL top(CI x){return T[rt[x]].v;} I void pop(CI x){O(rt[x]);} }p,q; I void DFS(CI x,CI fa){ W(a[x].se--) p.P(p.rt[x],d[x]);W(a[x].fi--) q.P(q.rt[x],d[x]-inf); RI i;LL tp,tq,t;for(i=fir[x];i;i=nxt[i]) to^fa&&(d[to]=d[x]+w[i],DFS(to,x),p.rt[x]=p.M(p.rt[x],p.rt[to]),q.rt[x]=q.M(q.rt[x],q.rt[to]),0); W(p.rt[x]&&q.rt[x]&&(t=(tp=p.top(x))+(tq=q.top(x))-2*d[x])<0) Ans+=t,p.pop(x),q.pop(x),p.P(p.rt[x],tp-t),q.P(q.rt[x],tq-t); } int main(){ freopen("rock.in","r",stdin),freopen("rock.out","w",stdout); RI i,x,y,z,X=0;for(read(n),i=1;i<n;i++) read(x,y,z),Add(x,y,z),Add(y,x,z); for(i=1;i<=n;i++) read(a[i].fi,a[i].se),x=min(a[i].fi,a[i].se),a[i].fi-=x,a[i].se-=x,X+=a[i].se; return DFS(1,0),writeln(Ans+X*inf),0; }
|