#P16786. [蓝桥杯 2026 国 A] 安全路径

    ID: 19127 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度普及 上传者: 标签>图论连通块2026蓝桥杯国赛

[蓝桥杯 2026 国 A] 安全路径

Problem Description

In a secure network, there are nn communication base stations. They are connected by n−1n-1 bidirectional optical fibers and form a tree.

In this problem, base station 11 is used as the root of the whole tree. For any base station xx, if the total number of base stations in the subtree rooted at xx is even, then base station xx is called a stable base station. The subtree here includes base station xx itself.

For two different base stations xx and yy, if all base stations on the simple path from xx to yy are stable base stations, then the ordered path x→yx \to y is called a safe path.

Please compute the total number of safe paths in the whole tree.

Note that x→yx \to y and y→xy \to x are considered two different safe paths.

Input Format

The first line contains a positive integer nn, representing the number of base stations.

The next n−1n-1 lines each contain two positive integers u,vu, v, indicating that there is a bidirectional optical fiber between base station uu and base station vv.

The input guarantees that the given nn base stations and n−1n-1 optical fibers form a tree.

Output Format

Output one line containing an integer, representing the total number of safe paths.

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

Hint

Sample Explanation

When taking base station 11 as the root:

  • The subtree of base station 22 contains base stations 2,42,4, with size 22;
  • The subtree of base station 33 contains base stations 3,53,5, with size 22;
  • The subtree of base station 11 contains all 66 base stations.

Therefore, the stable base stations are 1,2,31,2,3.

There are 66 safe paths in total: 1→21 \to 2, 1→31 \to 3, 2→12 \to 1, 3→13 \to 1, 2→32 \to 3, 3→23 \to 2.

All base stations on these paths are stable base stations, so they meet the requirement.

Constraints

For 40%40\% of the testdata, it is guaranteed that n≤500n \le 500.

For all testdata, it is guaranteed that 1≤n≤5000001 \le n \le 500000.

Translated by ChatGPT 5