1060: [ZJOI2007] Temporal synchronization time limit:10 Sec Memory limit:162 MB
submit:2101 solved:595
[Submit] [Status] [Discuss] Description small Q in the electronic technology practice class to learn welding circuit board. A circuit board consists of several components, we might call it a node, and use it as a digital .... to label. Each node of the board is connected by a number of disjoint conductors, and for any two nodes of the board, there is only one path (the path refers to a sequence of conductors connecting two elements). There is a special element called the "activator" on the circuit board. When the exciter is working, an excitation current is generated, passing through the wire to each node to which it is connected. And the intermediate node receives the excitation current, obtains the information, and passes the excitation current to the node which is connected with it and has not received the excitation current. Eventually, the intense current will reach some "terminating nodes"-nodes that are no longer forwarded after receiving the excitation current. The propagation of the excitation current on the wire takes time, and for each side E, the excitation current passes through the time it takes to TE, and the forwarding of the excitation current received by the node can be considered instantaneous. The circuit board now requires each "terminating node" to get an excitation circuit at the same time-keeping the tense in sync. Because the current structure does not conform to the requirement of temporal synchronization, it is necessary to change the structure of the connecting line. At present, small Q has a prop, the use of the props, you can make the excitation current through a connecting wire to increase the time of a unit. How many times can the small q use props to make all the "terminating node" tenses synchronized? The first line of Input contains a positive integer n, which represents the number of nodes in the board. The second line contains an integer s, which is the number of the board's activator. Next N-1 line, three integers a, B, t per line. Indicates that the wire is connected to Node A and Node B, and that the excitation current through this conductor requires a T unit time output
Contains only one integer v, the minimum number of items used for small q
Sample Input3
1
1 2 1
1 3 3Sample Output2HINT
n≤500000,te≤1000000
SourceSolution
After reading the question, you will know what to do ...
Find the maximum path of the subtree, and all the rest becomes this value, so dp[i] stores the maximum of the subtree with I as the root of the tree ...
DFS directly out the answer ....
Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 500010#defineMAXM 2000010intN,S,DP[MAXN];Long Longans=0;structedgenode{intNext from, To,ti;} EDGE[MAXM];intHead[maxn],cnt=1;voidAddintUintVintW) {CNT++; Edge[cnt].to=v; EDGE[CNT]. from=u; Edge[cnt].ti=w; Edge[cnt].next=head[u]; head[u]=CNT;}voidInsertintUintVintW) {Add (u,v,w); add (v,u,w);}voidDFS (intNowintLast ) { for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=Last ) DFS (Edge[i].to,now), Dp[now]=max (dp[edge[i].to]+Edge[i].ti,dp[now]); for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=last) ans+= (dp[now]-dp[edge[i].to]-edge[i].ti);}intMain () {N=read (); s=read (); for(intA,b,t,i=1; i<=n-1; i++) A=read (), B=read (), t=read (), insert (a,b,t); DFS (S,0); printf ("%lld\n", ans); return 0;}
This is called a tree DP.
"BZOJ-1060" Temporal synchronization tree DP (Dfs burst search)