Bzoj 1060: [ZJOI2007] temporal sync tree dp

Source: Internet
Author: User

1060: [ZJOI2007] Temporal synchronization
Description

The small q learns the welding circuit board in the electronic craft practice class. 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?

Input

The first line 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 connects Node A with node B, and that the excitation current passes through this conductor and requires 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

"Data Size"

For 100% of data, n≤500000

For all the data, te≤1000000

Source

Exercises

Set F[x] Indicates the maximum time of the leaf node to X

Then the f[x]=f[son]+edge.v answer is Sigma f[x]-f[son]-edge.v
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<map>using namespacestd;Const intN = 5e5+ -, M =30005, mod =1000000007, INF =0x3f3f3f3f; typedefLong Longll;//The difference is 1, the same is 0intn,s,head[n],t=1, f[n];ll ans;structedge{intTo,next,v;} e[n*2];voidAddintUintVintW) {e[t].to = V,e[t].next = HEAD[U],E[T].V = w,head[u]=t++;}voidDfsintXintFA) {     for(intI=head[x];i;i=E[i].next) {        if(fa==e[i].to)Continue;        DFS (E[I].TO,X); F[X]= Max (f[x],f[e[i].to]+e[i].v); }     for(intI=head[x];i;i=E[i].next) {        if(fa==e[i].to)Continue; Ans+=f[x]-f[e[i].to]-e[i].v; }}voidsolve () {DFS (s,s); cout<<ans<<Endl;}intMain () {scanf ("%d%d",&n,&s);  for(intI=1; i<n;i++) {        intA,b,c; scanf ("%d%d%d",&a,&b,&c);    Add (a,b,c); add (b,a,c);    } solve (); return 0;} 

Bzoj 1060: [ZJOI2007] temporal sync tree dp

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.