

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
頂点からなる木 と 頂点 辺からなる無向グラフ が与えられます。 それぞれの各頂点には から の番号が割り振られています。 の 本の辺のうち、 本目の辺は頂点 と頂点 を繋いでいます。 の 本の辺のうち、 本目の辺は頂点 と頂点 を繋いでいます。
に対して以下の操作を繰り返し行うことで、 に辺を追加することを考えます。
- つの整数 ,, であって、 の頂点 間と 間に辺があり、 間に辺がないようなものを選ぶ。 のある単純パス上に頂点 ,, が何らかの順序ですべて含まれるとき、 の頂点 間に辺を追加する。
これ以上辺を追加することができなくなったとき、 の辺の数はいくつになるか求めてください。 この数はどのように操作を行っても変わらないことが示せます。
制約
- は多重辺を含まない
- は木である
入力
入力は以下の形式で標準入力から与えられる。
出力
最終的な の辺の数を出力せよ。
入力例 1Copy
5 3 1 2 1 3 3 4 1 5 5 4 2 5 1 5
出力例 1Copy
6
以下の順で辺を追加することで 本まで辺を増やすことができます。
- とし、頂点 と頂点 の間に辺を追加する。
- とし、頂点 と頂点 の間に辺を追加する。
- とし、頂点 と頂点 の間に辺を追加する。
入力例 2Copy
7 5 1 5 1 4 1 7 1 2 2 6 6 3 2 5 1 3 1 6 4 6 4 7
出力例 2Copy
11
入力例 3Copy
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
出力例 3Copy
27
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 .
Sample Input 1Copy
5 3 1 2 1 3 3 4 1 5 5 4 2 5 1 5
Sample Output 1Copy
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 .
Sample Input 2Copy
7 5 1 5 1 4 1 7 1 2 2 6 6 3 2 5 1 3 1 6 4 6 4 7
Sample Output 2Copy
11
Sample Input 3Copy
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
Sample Output 3Copy
27