CF1252F.Regular Forestation

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A forestation is an act of planting a bunch of trees to grow a forest, usually to replace a forest that had been cut down. Strangely enough, graph theorists have another idea on how to make a forest, i.e. by cutting down a tree!

A tree is a graph of NN nodes connected by N1N - 1 edges. Let uu be a node in a tree UU which degree is at least 22 (i.e. directly connected to at least 22 other nodes in UU ). If we remove uu from UU , then we will get two or more disconnected (smaller) trees, or also known as forest by graph theorists. In this problem, we are going to investigate a special forestation of a tree done by graph theorists.

Let V(S)V(S) be the set of nodes in a tree SS and V(T)V(T) be the set of nodes in a tree TT . Tree SS and tree TT are identical if there exists a bijection f:V(S)V(T)f : V(S) \rightarrow V(T) such that for all pairs of nodes (si,sj)(s_i, s_j) in V(S)V(S) , sis_i and sjs_j is connected by an edge in SS if and only if node f(si)f(s_i) and f(sj)f(s_j) is connected by an edge in TT . Note that f(s)=tf(s) = t implies node ss in SS corresponds to node tt in TT .

We call a node uu in a tree UU as a good cutting point if and only if the removal of uu from UU causes two or more disconnected trees, and all those disconnected trees are pairwise identical.

Given a tree UU , your task is to determine whether there exists a good cutting point in UU . If there is such a node, then you should output the maximum number of disconnected trees that can be obtained by removing exactly one good cutting point.

For example, consider the following tree of 1313 nodes.

There is exactly one good cutting point in this tree, i.e. node 44 . Observe that by removing node 44 , we will get three identical trees (in this case, line graphs), i.e. {5,1,7,13}\{5, 1, 7, 13\} , {8,2,11,6}\{8, 2, 11, 6\} , and {3,12,9,10}\{3, 12, 9, 10\} , which are denoted by AA , BB , and CC respectively in the figure.

  • The bijection function between AA and BB : f(5)=8f(5) = 8 , f(1)=2f(1) = 2 , f(7)=11f(7) = 11 , and f(13)=6f(13) = 6 .
  • The bijection function between AA and CC : f(5)=3f(5) = 3 , f(1)=12f(1) = 12 , f(7)=9f(7) = 9 , and f(13)=10f(13) = 10 .
  • The bijection function between BB and CC : f(8)=3f(8) = 3 , f(2)=12f(2) = 12 , f(11)=9f(11) = 9 , and f(6)=10f(6) = 10 .

Of course, there exists other bijection functions for those trees.

输入格式

Input begins with a line containting an integer: NN ( 3N40003 \le N \le 4000 ) representing the number of nodes in the given tree. The next N1N - 1 lines each contains two integers: aia_i bib_i ( 1ai<biN1 \le a_i < b_i \le N ) representing an edge (ai,bi)(a_i,b_i) in the given tree. It is guaranteed that any two nodes in the given tree are connected to each other by a sequence of edges.

输出格式

Output in a line an integer representing the maximum number of disconnected trees that can be obtained by removing exactly one good cutting point, or output -1 if there is no such good cutting point.

输入输出样例

  • 输入#1

    13
    1 5
    1 7
    2 4
    2 8
    2 11
    3 12
    4 7
    4 12
    6 11
    7 13
    9 10
    9 12
    

    输出#1

    3
    
  • 输入#2

    6
    1 2
    1 3
    2 4
    3 5
    3 6
    

    输出#2

    -1
    

说明/提示

Explanation for the sample input/output #1

This is the example from the problem description.

首页