CF1252F.Regular Forestation

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

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 N−1N - 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.

造林是指种植大量树木以形成森林,通常是为了替代被砍伐的森林。奇怪的是,图论学家对如何制造森林有另一种想法,即通过砍掉一棵树!

一棵树是由 NN 个节点和 N−1N - 1 条边构成的连通图。设 uu 是树 UU 中的一个节点,其度数至少为 22(即在 UU 中直接与至少 22 个其他节点相连)。若从 UU 中移除 uu,我们将得到两个或更多互不连通(更小的)树,图论学家也将这些不连通的树组成的集合称为“森林”。本题中,我们将研究图论学家所定义的一种特殊的“造林”操作——即对一棵树进行单点割裂。

设 V(S)V(S) 表示树 SS 的节点集合,V(T)V(T) 表示树 TT 的节点集合。当且仅当存在一个双射函数 f:V(S)→V(T)f : V(S) \rightarrow V(T),使得对任意节点对 (si,sj)∈V(S)(s_i, s_j) \in V(S),sis_i 与 sjs_j 在 SS 中由一条边相连,当且仅当 f(si)f(s_i) 与 f(sj)f(s_j) 在 TT 中由一条边相连时,称树 SS 与树 TT 同构。注意:f(s)=tf(s) = t 表示 SS 中的节点 ss 对应于 TT 中的节点 tt。

我们称树 UU 中的一个节点 uu 为优质割点,当且仅当从 UU 中移除 uu 后,得到两个或更多互不连通的树,且这些不连通的树两两同构。

给定一棵树 UU,你的任务是判断 UU 中是否存在优质割点。若存在,则输出恰好移除一个优质割点所能得到的最大不连通树的数量。

例如,考虑如下含 1313 个节点的树:

该树中恰有一个优质割点,即节点 44。观察可知:移除节点 44 后,将得到三棵彼此同构的树(此处均为路径图),即 {5,1,7,13}\{5, 1, 7, 13\}、{8,2,11,6}\{8, 2, 11, 6\} 和 {3,12,9,10}\{3, 12, 9, 10\},图中分别记为 AA、BB 和 CC。

  • AA 与 BB 之间的同构映射 ff:f(5)=8f(5) = 8,f(1)=2f(1) = 2,f(7)=11f(7) = 11,f(13)=6f(13) = 6。
  • AA 与 CC 之间的同构映射 ff:f(5)=3f(5) = 3,f(1)=12f(1) = 12,f(7)=9f(7) = 9,f(13)=10f(13) = 10。
  • BB 与 CC 之间的同构映射 ff:f(8)=3f(8) = 3,f(2)=12f(2) = 12,f(11)=9f(11) = 9,f(6)=10f(6) = 10。

当然,这些树之间还存在其他可能的同构映射。

输入格式

Input begins with a line containting an integer: NN (3≤N≤40003 \le N \le 4000) representing the number of nodes in the given tree. The next N−1N - 1 lines each contains two integers: aia_i bib_i (1≤ai<bi≤N1 \le a_i \lt 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.

输入的第一行包含一个整数:NN(3≤N≤40003 \le N \le 4000),表示给定树中的节点数量。接下来的 N−1N - 1 行每行包含两个整数:aia_i bib_i(1≤ai<bi≤N1 \le a_i \lt b_i \le N),表示树中的一条边 (ai,bi)(a_i,b_i)。保证给定树中任意两个节点均可通过若干条边相互连通。

输出格式

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-1。

输入输出样例

  • 输入#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.

样例输入/输出 #1 的说明

这是题目描述中的示例。

输入解题思路,AI测评打分。不知道怎么写?

首页