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
Saying...... Is this a provincial topic?
Test instructions is to increase the weight of certain sides of the tree, so that the length of the path from any leaf node to the root is equal
Dis[i] represents the distance from the farthest leaf node in the face tree from I to I
It is obvious that the other leaf nodes under I must also be adjusted to be as large as dis[i]
The distance from any leaf node below I to I is the same after the adjustment.
Therefore, I can treat the sub-tree under I as a leaf node directly.
So every son of X's contribution to the answer is DIS[X]-DIS[Y]-E[I].V.
A DFS is over.
#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include < algorithm> #include <cmath> #include <queue> #include <deque> #include <set> #include <map > #include <ctime>typedef long LL; #define INF 0x7ffffff#define PA pair<int,int> #define Pi 3.1415926535897932384626433832795028841971using namespace Std;inline ll read () {ll x=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} struct Edge{int to,next,v;} E[1000010];int Head[500010];int N,cnt,rot; LL ans;int dis[500010],fa[500010];inline void ins (int u,int v,int W) {e[++cnt].to=v;e[cnt].v=w;e[cnt].next=head[u]; head[u]=cnt;} inline void Insert (int u,int v,int W) {ins (u,v,w); ins (v,u,w);} inline void dfs (int x) {for (int i=head[x];i;i=e[i].next) if (E[i].to!=fa[x]) {fa[e[i].to]=x; DFS (E[I].TO); Dis[x]=max (DIS[X],DIS[E[I].TO]+E[I].V); }for (iNT I=head[x];i;i=e[i].next) if (E[i].to!=fa[x]) ans+=dis[x]-(DIS[E[I].TO]+E[I].V);} int main () {n=read (); Rot=read (); for (int i=1;i<n;i++) {int x=read (), Y=read (), Z=read (), insert (x, y, z);} DFS (ROT);p rintf ("%lld\n", ans);
bzoj1060 [ZJOI2007] Temporal synchronization