CF120F.Spiders

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

One day mum asked Petya to sort his toys and get rid of some of them. Petya found a whole box of toy spiders. They were quite dear to him and the boy didn't want to throw them away. Petya conjured a cunning plan: he will glue all the spiders together and attach them to the ceiling. Besides, Petya knows that the lower the spiders will hang, the more mum is going to like it and then she won't throw his favourite toys away. Help Petya carry out the plan.

A spider consists of k beads tied together by k - 1 threads. Each thread connects two different beads, at that any pair of beads that make up a spider is either directly connected by a thread, or is connected via some chain of threads and beads.

Petya may glue spiders together directly gluing their beads. The length of each thread equals 1. The sizes of the beads can be neglected. That's why we can consider that gluing spiders happens by identifying some of the beads (see the picture). Besides, the construction resulting from the gluing process should also represent a spider, that is, it should have the given features.

After Petya glues all spiders together, he measures the length of the resulting toy. The distance between a pair of beads is identified as the total length of the threads that connect these two beads. The length of the resulting construction is the largest distance between all pairs of beads. Petya wants to make the spider whose length is as much as possible.

The picture two shows two spiders from the second sample. We can glue to the bead number 2 of the first spider the bead number 1 of the second spider. The threads in the spiders that form the sequence of threads of maximum lengths are highlighted on the picture.

一天,妈妈让佩佳整理玩具,并扔掉其中一些。佩佳发现了一整盒玩具蜘蛛。这些蜘蛛他十分珍爱,因此不愿将它们丢弃。佩佳想出了一个狡黠的计划:他要把所有蜘蛛粘在一起,然后把它们挂在天花板上。此外,佩佳知道,蜘蛛悬挂得越低,妈妈就越喜欢,从而就不会扔掉他最心爱的玩具了。请帮助佩佳实现这一计划。

一只蜘蛛由 kk 颗珠子通过 k−1k-1 根线段连接而成。每根线段连接两颗不同的珠子,且蜘蛛中任意两颗珠子之间,要么由一根线段直接相连,要么通过若干线段与珠子构成的路径间接相连。

佩佳可以通过直接粘合珠子的方式将多个蜘蛛粘合在一起。每根线段的长度均为 1,珠子的尺寸可忽略不计。因此,我们可以将蜘蛛的粘合理解为对某些珠子进行“识别”(即视为同一颗珠子)(参见图示)。此外,粘合后得到的整体结构也必须仍是一只蜘蛛,即需满足上述定义的所有性质。

佩佳将所有蜘蛛粘合完毕后,会测量最终玩具的“长度”。一对珠子之间的距离定义为连接这两颗珠子的所有线段的总长度。整个构造的长度则定义为所有珠子对之间距离的最大值。佩佳希望最终得到的蜘蛛具有尽可能大的长度。

图二展示了第二个样例中的两只蜘蛛。我们可以将第一只蜘蛛的编号为 2 的珠子与第二只蜘蛛的编号为 1 的珠子粘合在一起。图中高亮显示了构成最长路径的那些线段。

输入格式

The first input file line contains one integer n (1 ≤ n ≤ 100) — the number of spiders. Next n lines contain the descriptions of each spider: integer n__i (2 ≤ n__i ≤ 100) — the number of beads, then n__i - 1 pairs of numbers denoting the numbers of the beads connected by threads. The beads that make up each spider are numbered from 1 to n__i.

第一行输入文件包含一个整数 nn(1≤n≤1001 \leq n \leq 100)——蜘蛛的数量。接下来的 nn 行描述每只蜘蛛:一个整数 nin_i(2≤ni≤1002 \leq n_i \leq 100)——珠子的数量,然后是 ni−1n_i - 1 对数字,表示由丝线连接的珠子编号。每只蜘蛛所含的珠子编号从 11 到 nin_i。

输出格式

Print a single number — the length of the required construction.

输出一个整数——所需构造的长度。

输入输出样例

  • 输入#1

    1
    3 1 2 2 3

    输出#1

    2
  • 输入#2

    2
    3 1 2 1 3
    4 1 2 2 3 2 4

    输出#2

    4
  • 输入#3

    2
    5 1 2 2 3 3 4 3 5
    7 3 4 1 2 2 4 4 6 2 7 6 5

    输出#3

    7

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

首页