luogu#P5904. [POI 2014] HOT-Hotels 加强版

    ID: 4906 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2014POI(波兰)O2优化树形 DP树链剖分前缀和

[POI 2014] HOT-Hotels 加强版

Background

Same as [POI2014]HOT-Hotels, but the Constraints are increased to 1n1051 \le n \le 10^5.

Source: BZOJ4543.

Problem Description

Given a tree with nn nodes, find how many triples of nodes (i,j,k)(i,j,k) satisfy that the distances between every pair among i,j,ki,j,k are all equal.

(i,j,k)(i,j,k) and (i,k,j)(i,k,j) are considered the same triple.

Input Format

The first line contains an integer nn.

The next n1n-1 lines each contain two integers a,ba,b, meaning there is an edge between aa and bb.

Output Format

Output one integer in one line, representing the number of all valid triples of nodes.

7
1 2
5 7
2 5
2 3
5 6
4 5

5

Hint

For 100%100\% of the testdata, 1n105,1abn1 \le n \le 10^5, 1 \le a \le b \le n.

Translated by ChatGPT 5