CodeForces 120F|Spiders|树形DP|求树最长链

来源:互联网 发布:开源软件二次开发 编辑:程序博客网 时间:2024/06/16 20:38

给出n棵树,求这些树以某种方式连接在一起后的最长链长和。

令f[i]表示在i子树中能走到的最长步数。有f[i]=max{f[son[i]]+1}

那么对于每个点i,有答案为max j∈son[i] {f[son[i]-{j}]+f[j]+1}

#include <cstdio>#include <cstring>#include <algorithm>using std::max;const int N = 101, M = 2 * N;int far[N], len, to[M], next[M], head[N], cnt = 0;void add(int u, int v) {    next[++cnt] = head[u]; head[u] = cnt; to[cnt] = v;}void dfs(int u, int fa) {    for (int i = head[u]; i; i = next[i])        if (to[i] != fa) {            dfs(to[i], u);            len = max(len, far[u] + far[to[i]] + 1);            far[u] = max(far[u], far[to[i]] + 1);        }}int main() {    freopen("input.txt","r",stdin);    freopen("output.txt","w",stdout);    int t, n, i, u, v, ans = 0;    scanf("%d", &t);    while (t--) {        scanf("%d", &n);        cnt = 0;        memset(head, 0, sizeof head);        memset(far, 0, sizeof far);        for (i = 1; i < n; i++) {            scanf("%d%d", &u, &v);            add(u, v); add(v, u);        }        len = 0; dfs(1, 0); ans += len;    }    printf("%d", ans);    return 0;}

F. Spiders
time limit per test
1 second
memory limit per test
256 megabytes
input
input.txt
output
output.txt

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.

Input

The first input file line contains one integer n (1 ≤ n ≤ 100) — the number of spiders. Next nlines contain the descriptions of each spider: integer ni (2 ≤ ni ≤ 100) — the number of beads, then ni - 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 ni.

Output

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

Sample test(s)
input
13 1 2 2 3
output
2
input
23 1 2 1 34 1 2 2 3 2 4
output
4
input
25 1 2 2 3 3 4 3 57 3 4 1 2 2 4 4 6 2 7 6 5
output
7


0 0
原创粉丝点击