There was a civil war between two factions in Skyrim, a province of the Empire on the continent of Tamriel. The Stormcloaks, led by Ulfric Stormcloak, are made up of Skyrim's native Nord race. Their goal is an independent Skyrim free from Imperial interference. The Imperial Legion, led by General Tullius, is the military of the Empire that opposes the Stormcloaks and seeks to reunite and pacify the province.
The current target of General Tullius is to defend Whiterun City. Near by this city there are
N towers under the Empire's control. There are
N−1 roads link these tower, so solders can move from any tower to another one through these roads.
In military affairs, tactical depth means the longest path between two towers of all. Larger the tactical depth is, more stable these towers are.
According to the message sent by spies, General Tullius believe that Stormcloaks is planning to attack one of these roads, and his towers would be divided into two parts. However, Tullius does not know which one, so he supposes the possibility that Stormcloaks attack these roads are the same. Now, General Tullius ask for your help, to calculate the expectation of tactical depth after this attack.
To avoid the issue of precision, you need to calculate
expectationoftacticaldepth×(N−1).
2 3 2 1 2 3 2 5 5 2 1 7 3 1 7 4 2 5 5 2 6
7 63
题意:给你一棵树,树的每条边都有权值,每次等概率地切掉一条边,求所有切掉一条边后情况的直径和。
思路:和network那题一样,是树形dp,这题主要是分类讨论,有5种情况。假设现在访问的点是x,连接x的边对应的子节点是y,那么切去x-y这条边后,两部分的较大直径只可能是:1.以y为根的子树的直径。2.a,b节点是x的子节点,且lca(a,b)是x,找到满足前面条件的最长a-x-b路径。3.a是x的子节点,满足前面条件的最大的以a为根的子树。4.设x的父亲是h,从父亲节点h转移过来的树的直径(这里的直径不包括x这棵子树)5.从父亲节点过来,经过x,并且到达x子树下的最远点,这条路径的距离。
这里要开6个数组,分别是dp[x](以x为根的子树的直径),dist1[x],dist1id[x],dist2[x],dist2id[x],dist3[x],dist3id[x](x节点到其子节点的第一大,第二大,第三大距离及对应的x孩子节点的编号),dpzishu1id[x],dpzishu2id[x](表示x的子树中,不包括x节点,直径最大和次大的直径和孩子节点的编号)。
#include
#include
#include
#include
#include
#include
#include
#include