It is really hard to think of such a question in the on-site competition. I have read other people's reports and made it myself. I also want to leave a commemorative note, or I will have some experience when I do it later.
Simplified means that the distance between all nodes on a tree and a node X is the maximum, while the distance is the weight of the edge with the smallest weight in the path of the node to the node X; that's how to find node X.
First of all, it is easy to think that for an edge with the smallest weight value V, it must be the distance between all the nodes on the left or right, that is, the distance between the left and right is always V;
Therefore, we can think of recursion. Each time we divide the left and right parts into two parts, we can calculate the optimal distance and sum of the left and right parts separately, and then we can choose between them. If the values of all sides are the same, anyway, it will time out;
Because recursion selects the smallest edge each time. If we do this in the forward direction, we start to operate the edge right from the large edge and treat the first n nodes as N heaps, merge each heap gradually with edge weight from large to small (take the best merge method, for example, if a is merged to B, it is to take x Nodes in B, on the contrary, get the x node in node.
Question connection: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 4882
My code:
# Include <stdio. h>
# Include <algorithm>
Long long Fa [200001];
Long long N, path [200001] [3];
Long long belong [200001] [2];
Int CMP (const void * a, const void * B)
{
If (long *) B) [2]-(long *) A) [2]> = 0)
Return 1;
Return-1;
}
Long long find (long)
{
If (Fa [a] =)
Return;
Return Fa [a] = find (Fa [a]);
}
Int main ()
{
Long long I, A, B;
While (scanf ("% LLD", & N )! = EOF)
{
For (I = 1; I <= n-1; I ++)
Scanf ("% LLD", & path [I] [0], & path [I] [1], & path [I] [2]);
Qsort (path + 1, n-1, sizeof (path [0]), CMP );
For (I = 1; I <= N; I ++)
{
Fa [I] = I;
Belong [I] [0] = 0;
Belong [I] [1] = 1;
}
For (I = 1; I <= n-1; I ++)
{
A = find (path [I] [0]);
B = find (path [I] [1]);
If (belong [a] [0] + path [I] [2] * belong [B] [1]> belong [B] [0] + path [I] [2] * belong [a] [1])
{
Fa [B] =;
Belong [a] [0] = belong [a] [0] + path [I] [2] * belong [B] [1];
Belong [a] [1] + = belong [B] [1];
}
Else
{
Fa [a] = B;
Belong [B] [0] = belong [B] [0] + path [I] [2] * belong [a] [1];
Belong [B] [1] + = belong [a] [1];
}
}
Printf ("% LLD \ n", belong [find (1)] [0]);
}
Return 0;
}