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
| #include <bits/stdc++.h> #define F(n) Fi(i,n) #define Fi(i,n) Fl(i,0,n) #define Fl(i,l,n) for(int i=l;i<n;i++) using namespace std; const int N = 1e4+1, lgN = 18; typedef pair<int,long long> PIL; vector<PIL> G[N]; struct htree{ htree *fa; int sz, dep; PIL tofa, fromch; long long DIS[lgN]; htree():fa(NULL),sz(0),dep(0),tofa(0,0),fromch(0,0){ } }HT[N]; bool vis[N]; int count(int now, int no=-1){ int cnt = 1; for(PIL v:G[now])if(v.first != no && !vis[v.first]){ cnt += count(v.first,now); } return cnt; } int find_hr(int now, int no=-1, bool init=true){ static int sz, tsz; if(init)sz = count(now,no); int tmx = 0, tsum = 0; for(PIL v:G[now])if(v.first != no && !vis[v.first]){ int hr = find_hr(v.first, now, false); if(hr != -1)return hr; tsum += tsz; tmx = max(tmx, tsz); } tmx = max(tmx, sz-tsum-1); if(tmx <= sz/2)return now; tsz = tsum + 1; return -1; } void build_dep(int now,int no,int dep,long long dis){ HT[now].DIS[dep] = dis; for(PIL v:G[now])if(v.first != no && !vis[v.first]){ build_dep(v.first, now, dep, dis+v.second); } } int build_ht(int now, int dep=0){ int hr = find_hr(now); vis[hr] = 1; HT[hr].dep = dep; build_dep(hr,-1,dep,0); for(PIL v:G[hr])if(!vis[v.first]){ int ch = build_ht(v.first, dep+1); HT[ch].fa = HT+hr; } return hr; } void update_ht(int now,int u){ htree *ht = &HT[now]; int dep = ht->dep; ht->sz += u; while(dep--){ ht->tofa.first += u; ht->tofa.second += u*HT[now].DIS[dep]; ht = ht->fa; ht->fromch.first += u; ht->fromch.second += u*HT[now].DIS[dep]; } } long long query_ht(int now){ htree *ht = &HT[now]; int dep = ht->dep; long long res = HT[now].fromch.second; while(dep--){ htree *fa = ht->fa; res += fa->fromch.second; res -= ht->tofa.second; res += (fa->fromch.first + fa->sz - ht->tofa.first)*HT[now].DIS[dep]; ht = fa; } return res; } int pos[N]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int t,n,m,k,a,b,c; cin>>t; while(t--){ fill(HT,HT+N,htree()); memset(pos,0,sizeof(pos)); memset(vis,0,sizeof(vis)); fill(G,G+N,vector<PIL>()); cin>>n>>m>>k; F(n-1){ cin>>a>>b>>c; a--;b--; G[a].push_back(PIL(b,c)); G[b].push_back(PIL(a,c)); } build_ht(0); update_ht(0,m-1); F(k){ cin>>a>>b; a--;b--; if(a == 0){ pos[0] = b; }else{ update_ht(pos[a],-1); pos[a] = b; update_ht(pos[a],1); } cout<<query_ht(pos[0])<<endl; } } }
|