#abc312g. G - Avoid Straight Line

G - Avoid Straight Line

Score : 550550 points

问题描述

给定一棵包含 NN 个顶点的树。顶点编号从 11NN,第 ii 条边连接顶点 AiA_i 和顶点 BiB_i

求满足以下条件的整数元组 (i,j,k)(i,j,k) 的数量:

  • 1i<j<kN1 \leq i < j < k \leq N
  • 所给定的树中不存在一条简单路径,该路径包含了顶点 iijjkk

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a tree with NN vertices. The vertices are numbered from 11 through NN, and the ii-th edge connects vertex AiA_i and vertex BiB_i.
Find the number of tuples of integers (i,j,k)(i,j,k) such that:

  • 1i<j<kN1 \leq i < j < k \leq N; and
  • the given tree does not contain a simple path that contains all of vertices ii, jj, and kk.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1Ai,BiN1 \leq A_i, B_i \leq N
  • The given graph is a tree.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 B1B_1

\vdots

AN1A_{N-1} BN1B_{N-1}

Output

Print the answer.

Sample Input 1

5
1 2
2 3
2 4
1 5

Sample Output 1

2

Two tuples satisfy the conditions: (i,j,k)=(1,3,4),(3,4,5)(i,j,k) = (1,3,4),(3,4,5).

Sample Input 2

6
1 2
2 3
3 4
4 5
5 6

Sample Output 2

0

Sample Input 3

12
1 6
3 4
10 4
5 9
3 1
2 3
7 2
2 12
1 5
6 8
4 11

Sample Output 3

91

update @ 2024/3/10 08:54:15