

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
頂点の無向木 が与えられます。頂点には の番号が付いており、 番目の辺は頂点 と頂点 を結ぶ無向辺です。
グラフがアルカンであるとは以下の条件をともに満たしていることであると定義します。
- グラフは無向木である
- すべての頂点の次数が または であり、次数 の頂点が つ以上存在する
の部分グラフであってアルカンであるものが存在するか判定し、存在する場合はそのようなものの頂点数の最大値を求めてください。
制約
- 与えられるグラフは無向木
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
出力
の部分グラフであってアルカンであるものが存在する場合はそのようなものの頂点数の最大値を出力せよ。そうでない場合は と出力せよ。
入力例 1Copy
9 1 2 2 3 3 4 4 5 2 6 2 7 3 8 3 9
出力例 1Copy
8
頂点 と頂点 を結ぶ無向辺を辺 と表記します。
頂点 、辺 からなる部分グラフはアルカンです。
入力例 2Copy
7 1 2 1 3 2 4 2 5 3 6 3 7
出力例 2Copy
-1
入力例 3Copy
15 8 5 2 9 1 12 6 11 9 3 15 1 7 12 7 13 10 5 6 9 5 1 1 9 4 5 6 14
出力例 3Copy
11
Score : points
Problem Statement
You are given an undirected tree with vertices, numbered . The -th edge is an undirected edge connecting vertices and .
A graph is defined to be an alkane if and only if it satisfies the following conditions:
- The graph is an undirected tree.
- Every vertex has degree or , and there is at least one vertex of degree .
Determine whether there exists a subgraph of that is an alkane, and if so, find the maximum number of vertices in such a subgraph.
Constraints
- The given graph is an undirected tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If there exists a subgraph of that is an alkane, print the maximum number of vertices in such a subgraph. Otherwise, print .
Sample Input 1Copy
9 1 2 2 3 3 4 4 5 2 6 2 7 3 8 3 9
Sample Output 1Copy
8
Let denote an undirected edge between vertices and .
A subgraph consisting of vertices and edges is an alkane.
Sample Input 2Copy
7 1 2 1 3 2 4 2 5 3 6 3 7
Sample Output 2Copy
-1
Sample Input 3Copy
15 8 5 2 9 1 12 6 11 9 3 15 1 7 12 7 13 10 5 6 9 5 1 1 9 4 5 6 14
Sample Output 3Copy
11