Daejeon Urban Railway Line 2
InterviewTime limit1sMemory limit1024 MB
Count unordered pairs of intersections off the 1-to-N path whose tree path touches that path at least once.
- Level
Medium4 of 10
- Topics
- Tree, DFS, Combinatorics
- Solved
- No attempts yet
Problem
Daejeon has intersections and roads that form a tree. The intersections are numbered from to . Daejeon runs Urban Railway Line 1 along the path that connects intersection and intersection . Stations are installed at the intersections on this path, and rails are installed on the roads on it.
The city plans to run an additional Line 2. As with Line 1, Line 2 will have stations and rails along the path that connects intersection and intersection . Intersections and must be different and must not have Line 1 stations. Line 2 must include at least one Line 1 station so that riders can transfer to Line 1.
Count the number of ways to choose two intersections that satisfy these conditions. Two choices that differ only in order are considered the same.
Input
The first line contains an integer (). The next lines each contain and (; ), the endpoints of a road, separated by a space.
Output
Print the number of ways to choose two intersections that satisfy the conditions.