Codeforces 120F (Longest distance on tree dp-tree)

Source: Internet
Author: User

Problem Description:

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 would glue all the spiders together and attach them to the ceiling. Besides, Petya knows that the lower the spiders would hang, the more mum was going to like it and then she won ' t throw him f Avourite toys away. Help Petya carry out the plan.

A spider consists of k beads tied together by k-1 threads. Each thread connects the different beads, at which any pair of beads the make up a spider are 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 this gluing spiders happens by identifying some of the beads. Besides, the construction resulting from the gluing process should also represent a spider, which is, it should has the GI Ven 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 the 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, shows, and spiders from the second sample. We can glue to the Bead Number 2 of the first spider The Bead Number 1 of the The second spider. The threads in the Spiders so form the sequence of threads of maximum lengths is highlighted on the picture.

Input

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 ni (2≤ni≤100)-the number of beads, then ni-1 pairs o F numbers denoting the numbers of the beads connected by threads. The beads that make up each spider is numbered from 1 to NI.

Output

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

Example

Input

1
3 1 2 2 3
Output
2
Input
2
3 1 2 1 3
4 1 2 2 3 2 4
Output
4
Input
2
5 1 2 2 3 3 4 3 5
7 3 4 1 2 2 4 4 6 2 7 6 5
Output
7
Topic test Instructions: Give us n trees, let us figure out the longest distance in each tree tree, the answer is their and.

Topic Analysis: Tree DP solves the longest distance problem on the tree, starting from the root node, recursively to the leaf node, and then returning the results of the leaf nodes to update the root node. Ans=max (ans,dp[father]+dp[son]+1); this subtrees the longest distance of the tree. Update Node Dp[father]=max (dp[father],dp[son]+1);

The code is as follows:

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <vector

> Using namespace std;
const int maxn=105;
Vector<int> VEC[MAXN];
int DP[MAXN];
        void Dfs (int x,int pre) {for (int i=0;i<vec[x].size (); i++) {if (vec[x][i]==pre) continue;
        DFS (VEC[X][I],X);
        Dp[0]=max (dp[0],dp[x]+dp[vec[x][i]]+1);
    Dp[x]=max (dp[x],dp[vec[x][i]]+1);
} return;
    } int main () {freopen ("Input.txt", "R", stdin);
    Freopen ("Output.txt", "w", stdout);
    int n,m,ans=0;
    scanf ("%d", &n);
        for (int i=1;i<=n;i++) {scanf ("%d", &m);
            for (int j=1;j<m;j++) {int A, B;
            scanf ("%d%d", &a,&b);
            Vec[a].push_back (b);
        Vec[b].push_back (a);
        } memset (Dp,0,sizeof (DP));
        DFS (1,0);
        for (int j=1;j<=m;j++) vec[j].clear ();
    ANS+=DP[0];
    } printf ("%d\n", ans);
return 0; }
























Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.