atcoder#AGC033F. [AGC033F] Adding Edges
[AGC033F] Adding Edges
Score : points
Problem Statement
You are given a tree with vertices and an undirected graph with vertices and edges. The vertices of each graph are numbered to . The -th of the edges in connects Vertex and Vertex , and the -th of the edges in connects Vertex and Vertex .
Consider adding edges to by repeatedly performing the following operation:
- Choose three integers , and such that has an edge connecting Vertex and and an edge connecting Vertex and but not an edge connecting Vertex and . If there is a simple path in that contains all three of Vertex and in some order, add an edge in connecting Vertex and .
Print the number of edges in when no more edge can be added. It can be shown that this number does not depend on the choices made in the operation.
Constraints
- does not contain multiple edges.
- is a tree.
Input
Input is given from Standard Input in the following format:
Output
Print the final number of edges in .
5 3
1 2
1 3
3 4
1 5
5 4
2 5
1 5
6
We can have at most six edges in by adding edges as follows:
- Let and add an edge connecting Vertex and .
- Let and add an edge connecting Vertex and .
- Let and add an edge connecting Vertex and .
7 5
1 5
1 4
1 7
1 2
2 6
6 3
2 5
1 3
1 6
4 6
4 7
11
13 11
6 13
1 2
5 1
8 4
9 7
12 2
10 11
1 9
13 7
13 11
8 10
3 8
4 13
8 12
4 7
2 3
5 11
1 4
2 11
8 10
3 5
6 9
4 10
27