Description
Set T = (V, E, W ) is a communication acyclic and directed graph (also known as non-root of the tree), with the right edge of each positive integers, we call tree network T (treenetwork), wherein V, E denote the set of nodes and edges, W represents the length of each side of the set, and let T n-nodes.
Path: Tree Network any two nodes a, b there is only one simple path, represented by D (a, b) in a, b is the length of the endpoints of the path, which is the length of each side of the path and. We call D (a, b) is the distance between a, b two nodes. A path from point v to the point P for the distance to the nearest node on the P: d (v, P) = min {d (v, u), u is the junction point on the path P}.
The diameter of the tree network: tree network path is called the longest diameter of the tree network. For a given tree network T, diameters are not necessarily unique, but it can be proved: the midpoint of each diameter (not necessarily happens to be a node on the side of the interior of a possible bar) is unique, we call this point as the center of the tree network.
Eccentricity ECC (F): the tree network T are the farthest away from the path F of the node F to the path distance, namely: ECC (F) = max { d (v, F), v∈V}.
Task: For a given tree network T = (V, E, W ) and a non-negative integer s, seeking a path F, which is (are nodes of the tree network path ends in) diameter on a certain segment of the path , its length does not exceed s (may be equal to s), that the eccentricity ECC (F) minimum. We call this the path for the network tree T = (V, E, W ) of the core (Core). If necessary, F can be reduced to a junction. Generally, in the above definitions, the core is not necessarily only one, but only a minimal eccentricity.
The following figure shows an example of the tree network. FIG, AB and AC are two diameter, length was 20. W is the center point of a tree network, the length of the side is 5 EF. If you specify nuclear s = 11, the web path tree DEFG (path may be taken as the DEF), 8 eccentricity. If you specify nuclear s = 0 (or s = 1, s = 2) , the tree is a network node F, the eccentricity 12.
Input
第1行,两个正整数n和s,中间用一个空格隔开。其中n为树网结点的个数,s为树网的核的长度的上界。设结点编号依次为1, 2, ..., n。 从第2行到第n行,每行给出3个用空格隔开的正整数,依次表示每一条边的两个端点编号和长度。例如,“2 4 7”表示连接结点2与4的边的长度为7。 所给的数据都是正确的,不必检验。
Output
只有一个非负整数,为指定意义下的最小偏心距。
Sample Input 1
【输入1】
5 2
1 2 5
2 3 2
2 4 4
2 5 3
【输入2】
8 6
1 3 2
2 3 2
3 4 6
4 5 3
4 6 4
4 7 2
7 8 3
Sample Output 1
【输出1】
5
【输出1】
5
Hint
此题解来自洛谷@柒葉灬 大佬:
思路:在直径上尺取,计算每条路径的偏心距.
-
方法1:可以选开始的一条,直接暴力计算每个点到这条点的贡献,之后进行递推,可以发现,只要两端点的改变会使得子树每个节点变化,用什么数据结构可以支持(在线修改+查询最大值)呢?——线段树!!!代码就不出示了......100多行,反正也没人看。
-
方法2:可以证明一条路径的2个端点最长距离是到直径两个端点,可以用反证法证明,所以只要处理非直径上其他点的贡献即可,预处理。
整理发现,在一个线段上尺取,每一个点有一个贡献,问这些点中最大值是多少?在与两端点贡献比较,单调队列√
然而我们探索并没有结束,我们仔细观察,我们找的最小值肯定要么是两端点到直径端点的贡献,要么是直径上的点的贡献,那么能不能直接取最小呢?答案是可以,可以证明一条路径上,其他点的贡献<两端点到直径端点的贡献(用反证法可以证明)。
比如说染色的是直径,红色的是选的路径,显然,绿色点的贡献都小于路径俩端点的贡献,证明over!
即就算加上其他点的贡献,对这条路径的答案也不会有一点影响,所以单调队列就可以去掉了直接取max就行了。
(我依据此思路的代码实现:)
#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int maxn = 305, maxm = 605; int n, s; int fir[maxn], ne[maxm], to[maxm], w[maxm], np=0; void add(int x,int y,int z){ ne[++np] = fir[x]; fir[x] = np; to[np] = y; w[np] = z; } int dist[maxn],fa[maxn]; int mark[maxn]; void dfs(int u,int f,int d,int &o){ dist[u] = d; fa[u] = f; if(dist[o]<d) o = u; for(int i=fir[u];i;i=ne[i]){ int v = to[i]; if(v == f || mark[v]) continue; dfs(v, u, d+w[i], o); } } int main(){ scanf("%d%d", &n, &s); for(int i=1, x, y, z;i<n;++i) { scanf("%d%d%d", &x, &y, &z); add(x, y, z); add(y, x, z); } int d1=0,d2=0; dfs(1,0,0,d1); dfs(d1,0,0,d2); int ans=0x3f3f3f3f; for(int i=d2,j=d2; i; i=fa[i]) {//尺取的路径为[i,j] while(dist[j]-dist[i]>s) j=fa[j]; ans = min(ans, max(dist[d2]-dist[j], dist[i]) );//直接找两端点到i,j的距离 } for(int i=d2; i; i=fa[i]) mark[i] = 1; for(int i=d2,tmp=0; i; i=fa[i]) dist[i] = 0, dfs(i,fa[i],0,tmp); for(int i=1; i<=n; i++) ans = max(ans, dist[i]); printf("%d",ans); return 0; }