"Bzoj 3991" [SDOI2015] Treasure Hunt Game

Source: Internet
Author: User

3991: [SDOI2015] Treasure Hunt Game

Time limit:40 Sec Memory limit:128 MB
submit:251 solved:137
[Submit] [Status] [Discuss]
Description

Little B is currently playing a scavenger hunt, and the game's map has n villages and N-1 roads, and there is only one route between any two villages. At the beginning of the game, the player can select a village, transfer it to the village in an instant, and then walk on the map, and if there is a treasure in a village, consider finding the treasure in the village until all the treasures are found and returned to the village where they were originally transferred. Little b wants to evaluate the difficulty of the game, so he needs to know the shortest distance the player will need to walk to find all the treasures. But the game treasures often change, sometimes a village will suddenly appear treasures, sometimes a village of treasures will suddenly disappear, so small b need to constantly update data, but small B is too lazy, not willing to calculate, so he asked you for help. To simplify the problem, we think that at the very beginning, there were no treasures in all the villages.

Input

The first line, two integers n, m, where M is the number of changes in the treasure.

The next N-1 line, with three integers x, y, and z for each row, indicates that there is a Z-length path between village x and Y.
The next M-line, an integer t per line, represents the operation of a treasure change. If there is no treasure in the village T before the operation, there is treasure in the village after operation; If there is a treasure in the village T before the operation, there is no treasure in the village after operation.
Output

M line, one integer per line, where the integer of line I indicates the shortest distance that the player will need to walk after the first operation to find all the treasures. If there are only treasures in one village, or if there are no treasures in all villages, the output is 0.

Sample Input

4 5

1 2 30

2 3 50

2 4 60

2

3

4

2

1
Sample Output

0

100

220

220

280
HINT

1<=n<=100000

1<=m<=100000

For all the data, 1<=z<=10^9
Source

Round 1 Thank you yts1999 for uploading

Set maintains the DFS sequence.

Test instructions is probably a requirement to dynamically maintain the length of some tree chains.

We can see that the required path resembles an "octopus" , and we can find the path length between the 22 adjacent to the DFS sequence, plus the path length between the Dfs order's most forward and the DFS sequence.

#include <cstring>#include <cstdio>#include <cstdlib>#include <iostream>#include <algorithm>#include <cmath>#include <set>#define LL Long Long#define M 100005using namespace STD; Set<int>S Set<int>:: Iterator it;intd[m],id[m],v[m],w[m],now=0, tot=0, H[m],n,m;intf[100005][ -]; LL Ans,pre[m];structedge{intX,y,ne; LL v;} e[m*3];voidAddedge (intXintY,ll v) {e[++tot].y=y;    E[TOT].NE=H[X];    E[tot].v=v; H[x]=tot;}voidReadint&AMP;TMP) {tmp=0;CharCh=getchar (); for(;ch<' 0 '|| Ch>' 9 '; Ch=getchar ()); for(; ch>=' 0 '&&ch<=' 9 '; Ch=getchar ()) tmp=tmp*Ten+ch-' 0 ';}voidDfsintXintFaintDEP) {f[x][0]=FA;    W[id[x]=++now]=x; D[X]=DEP; for(intI=h[x];i;i=e[i].ne) {intY=E[I].Y;if(Y==FA)Continue;        PRE[Y]=PRE[X]+E[I].V; DFS (y,x,dep+1); }}voidST () { for(intj=1; j<= -; j + +) for(intI=1; i<=n;i++) f[i][j]=f[f[i][j-1]][j-1];}voidMove (int&x,intDEP) { for(intI= -; i>=0; i--)if(D[F[X][I]]&GT;=DEP) x=f[x][i];}intGetlca (intXintY) {if(D[x]>d[y]) swap (x, y);if(D[x]!=d[y]) Move (Y,d[x]);if(x==y)returnX for(intI= -; i>=0; i--)if(F[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];returnf[x][0];} LL Dis (intXintY) {returnPre[x]+pre[y]-pre[getlca (x, y)]*2;}intNeintx) {it=s.find (id[x]);return++it==s.end ()?0: W[*it];}intprintx) {it=s.find (id[x]);returnIt==s.begin ()?0: W[*--it];}voidADD (intx) {S.insert (id[x]);intL=PR (x), R=ne (x);if(l) Ans+=dis (l,x);if(r) Ans+=dis (X,R);if(L&&r) Ans-=dis (l,r);}voidErase (intx) {intL=PR (x), R=ne (x);if(l) Ans-=dis (l,x);if(r) Ans-=dis (X,R);if(L&&r) Ans+=dis (l,r); S.erase (Id[x]);}intMain () {read (n), read (m); for(intI=1; i<n;i++) {intx, y; LL v;scanf("%d%d%lld", &x,&y,&v);    Addedge (X,y,v), Addedge (Y,X,V); } DFS (1,0,1);    ST (); ans=0; for(intI=1; i<=m;i++) {intX Read (x);if(V[x]) Erase (x);ElseADD (x); v[x]^=1;printf("%lld\n", S.size ()? Ans+dis (W[*s.begin ()],w[*--s.end ()):0); }return 0;}

"Bzoj 3991" [SDOI2015] Treasure Hunt Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.