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 194 195 196 197 198
| #include<stack> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std;
typedef long long ll; const int N=300000;
struct NODE{ int to,len;NODE *nxt; NODE(){} NODE(int _t,int _l,NODE *_n):to(_t),len(_l),nxt(_n){} }; struct TREE{ NODE pol[N*2+3],*head[N+3],*ncnt; TREE(){ncnt=pol;} void AddEdge(int u,int v,int len){ NODE *p=++ncnt,*q=++ncnt; *p=NODE(v,len,head[u]);head[u]=p; *q=NODE(u,len,head[v]);head[v]=q; } NODE* operator [](int id){return head[id];} }Tr;
int n,m,ndfn,nheadid;
ll dep[N+3],bzans[N+3][23];
int wson[N+3],siz[N+3],dfn[N+3],headid[N+3],head[N+3],fa[N+3],prv[N+3][23],perdep[N+3];
vector< int > vec[N+3]; vector< pair< int,int > > wit; stack< int > nowprev;
void DFS(int u,int fa){ perdep[u]=perdep[fa]+1; ::fa[u]=fa; siz[u]++; for(NODE *it=Tr[u];it;it=it->nxt){ int v=it->to; if(v==fa) continue; dep[v]=dep[u]+it->len; DFS(v,u); siz[u]+=siz[v]; if(siz[wson[u]]<siz[v]) wson[u]=v; } } void DFN(int u,int fa,int head){ ::head[u]=head; if(u==head) headid[u]=++nheadid; int id=headid[head]; if(vec[id].empty() || vec[id].back()>u) vec[id].push_back(u); dfn[u]=++ndfn; if(wson[u]) DFN(wson[u],u,head); for(NODE *it=Tr[u];it;it=it->nxt){ int v=it->to; if(v==fa || v==wson[u]) continue; DFN(v,u,v); } } void GainPrev(int u){ if(wson[u]) GainPrev(wson[u]); while(!nowprev.empty() && nowprev.top()>u) nowprev.pop(); if(nowprev.empty()) prv[u][0]=0; else{ prv[u][0]=nowprev.top(); bzans[u][0]=(dep[prv[u][0]]-dep[u])*u; } nowprev.push(u); for(int i=1;i<=22;i++){ prv[u][i]=prv[prv[u][i-1]][i-1]; bzans[u][i]=bzans[u][i-1]+bzans[prv[u][i-1]][i-1]; } } int LCA(int u,int v){ while(head[u]!=head[v]){ if(perdep[head[u]]>perdep[head[v]]) swap(u,v); v=fa[head[v]]; } if(perdep[u]>perdep[v]) swap(u,v); return u; } int SearchEnd(int id,int pnt){ int l=0,r=vec[id].size(); while(l+1<r){ int m=(l+r)>>1; if(vec[id][m]>=pnt) l=m; else r=m; } return l; } int main(){
scanf("%d%d",&n,&m); for(int i=1;i<n;i++){ int u,v,l;scanf("%d%d%d",&u,&v,&l); Tr.AddEdge(u,v,l); } DFS(1,0); DFN(1,0,1); for(int i=1;i<=n;i++) if(head[i]==i){ while(!nowprev.empty()) nowprev.pop(); GainPrev(i); } while(m--){ int u,v; scanf("%d%d",&u,&v); if(LCA(u,v)!=u) printf("-1\n"); else{ ll resu=0; if(head[u]==head[v]){ int copu=u; for(int i=22;i>=0;i--) if(prv[copu][i] && perdep[prv[copu][i]]<=perdep[v]){ resu+=bzans[copu][i]; copu=prv[copu][i]; } resu+=copu*(dep[v]-dep[copu]); printf("%lld\n",resu); continue; } int nowp=head[v]; wit.clear(); while(head[fa[nowp]]!=head[u]){ wit.push_back(make_pair(head[fa[nowp]],fa[nowp])); nowp=head[fa[nowp]]; } int lim=fa[nowp]; int cop=u; for(int i=22;i>=0;i--) if(prv[cop][i] && perdep[prv[cop][i]]<=perdep[lim]){ resu+=bzans[cop][i]; cop=prv[cop][i]; } for(int i=wit.size()-1;i>=0;i--){ int id=headid[wit[i].first];lim=wit[i].second; int rem=vec[id][SearchEnd(id,cop)]; if(cop>rem){ resu+=cop*(dep[rem]-dep[cop]); cop=rem; for(int j=22;j>=0;j--) if(prv[cop][j] && perdep[prv[cop][j]]<=perdep[lim]){ resu+=bzans[cop][j]; cop=prv[cop][j]; } } else{ if(prv[rem][0] && perdep[prv[rem][0]]<=perdep[lim]){ resu+=cop*(dep[prv[rem][0]]-dep[cop]); cop=prv[rem][0]; for(int j=22;j>=0;j--) if(prv[cop][j] && perdep[prv[cop][j]]<=perdep[lim]){ resu+=bzans[cop][j]; cop=prv[cop][j]; } } } } int id=headid[head[v]]; int rem=vec[id][SearchEnd(id,cop)]; if(cop>rem){ resu+=cop*(dep[rem]-dep[cop]); cop=rem; for(int i=22;i>=0;i--) if(prv[cop][i] && perdep[prv[cop][i]]<=perdep[v]){ resu+=bzans[cop][i]; cop=prv[cop][i]; } resu+=cop*(dep[v]-dep[cop]); } else{ if(!prv[rem][0]) resu+=cop*(dep[v]-dep[cop]); else{ if(perdep[prv[rem][0]]>=perdep[v]) resu+=cop*(dep[v]-dep[cop]); else{ resu+=cop*(dep[prv[rem][0]]-dep[cop]); cop=prv[rem][0]; for(int i=22;i>=0;i--) if(prv[cop][i] && perdep[prv[cop][i]]<=perdep[v]){ resu+=bzans[cop][i]; cop=prv[cop][i]; } resu+=cop*(dep[v]-dep[cop]); } } } printf("%lld\n",resu); } } return 0; }
|