Editorial
Fix a vertex as the center. Consider each direction incident to , and define the length of that direction as the maximum distance one can go from through that direction.
A triangular star of size centered at exists if and only if there are at least three directions whose lengths are at least . Therefore, the largest possible size of a triangular star centered at is the third largest value among the direction lengths of .
We compute this value for every vertex. Root the tree arbitrarily and perform two tree DP passes.
Let be the maximum distance one can reach by going downward into the subtree of . This can be computed in postorder.
Then let be the maximum distance one can reach from through the parent direction. When computing for a child of , use the longest direction from excluding the direction toward .
For each vertex, collect the lengths for every child , and also if is not the root. The third largest collected value is denoted by . Then vertex contributes one triangular star of each size .
Using a difference array, add to the range for every vertex , and then take prefix sums to obtain all answers.
The time complexity is , and the memory complexity is .