#P16786. [蓝桥杯 2026 国 A] 安全路径
[蓝桥杯 2026 国 A] 安全路径
Problem Description
In a secure network, there are communication base stations. They are connected by bidirectional optical fibers and form a tree.
In this problem, base station is used as the root of the whole tree. For any base station , if the total number of base stations in the subtree rooted at is even, then base station is called a stable base station. The subtree here includes base station itself.
For two different base stations and , if all base stations on the simple path from to are stable base stations, then the ordered path is called a safe path.
Please compute the total number of safe paths in the whole tree.
Note that and are considered two different safe paths.
Input Format
The first line contains a positive integer , representing the number of base stations.
The next lines each contain two positive integers , indicating that there is a bidirectional optical fiber between base station and base station .
The input guarantees that the given base stations and 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 as the root:
- The subtree of base station contains base stations , with size ;
- The subtree of base station contains base stations , with size ;
- The subtree of base station contains all base stations.
Therefore, the stable base stations are .
There are safe paths in total: , , , , , .
All base stations on these paths are stable base stations, so they meet the requirement.
Constraints
For of the testdata, it is guaranteed that .
For all testdata, it is guaranteed that .
Translated by ChatGPT 5