hihocoder#1050: The longest road in the tree (the longest path algorithm in the tree two times BFS Find root node for maximum +bfs Mark route length +bfs not easy to time out, with Dfs do Tle)

Source: Internet
Author: User

#1050: The longest time limit in a tree:10000msSingle Point time limit:1000msMemory Limit:256MBDescribe

Last said, Little Ho got a binary tree toy, the toy is connected by small balls and sticks, and in the process of dismantling it, Little Ho found that he can not only piece together into a two-pronged tree! can also be pieced together into a multi-fork tree-well, is actually more ordinary trees.

But anyway, little Ho's favorite toy has been upgraded, so he is more admiringly (in fact, the ball and sticks have what fun is it =). The toy tree in Small Ho's hands is now pieced together by n balls and N-1 sticks, all of which are marked by small ho with different numbers, and these numbers are all out of 1. Within the range of N, each stick is connected to two different balls, and there are no more than two different paths between any two spheres that can reach each other. In a word, it's a pretty fun toy!

But little hi saw small ho this way, think he is so addicted to it is not a good thing, so he thought to find some more questions let him think about it-but to small hi's level, nature is extremely easy!

So this day after breakfast, small hi will be holding a tree toy playing with the joy of Small Ho Road: You say you play this thing every day, I asked you a question, see if you can know? ”

Bad Little Ho didn't want to refuse.

"Then you continue to play, a time to return home when I do not call on you ~" Little hi Serious way.

Eh No, no, no, no, you said you said, I'm listening. "The Little Ho, who has always been used to opening follow patterns, cries out," he said.

Small hi satisfied nodded, then said: "This is right, my question is very simple, that is-- you this tree which two nodes in the distance between the longest?" Of course, the distance here refers to the number of sticks that go through one node to another. ”。

"Ah?" Little Ho looked down at the toy tree in his hand and was puzzled.

Hint One: The road always has the vertex, the path is no exception! Input

Each test point (input file) has and has only one set of test data.

The first behavior of each set of test data is an integer n, meaning as described earlier.

Each group of test data of the line 2~n, each line describes a stick, wherein the I+1 Act Two integer Ai,bi, which represents the number of two balls connected by the root of the I stick.

For 20% of data, meet n<=10.

For 50% of data, meet n<=10^3.

For 100% of data, meet N<=10^5,1<=ai<=n, 1<=bi<=n

Little hi Tip: Those with array storage tree edge remember to open twice times the size Oh!

Output

For each set of test data, output an integer ans, which indicates the distance between the two nodes farthest from the given tree.

Sample input
81 21 31 44 53 66 77 8
Sample output
 6 
algorithm analysis: The longest road in a tree must be between two leaf nodes. So we do two bfs, random from a node for the first BFS,
find out from the point of the deepest leaf node, assuming V, and then from the V point for the second BFS, this time is to find the deepest leaf node, and on the
time is different: this time from our designated v node. Assuming that the deepest leaf node found is W, the path length between V and W is the longest of the
in this tree.
Code:
#include <iostream> #include <string> #include <stdio.h> #include <string.h> #include < stdlib.h> #include <ctype.h> #include <algorithm> #include <vector> #include <queue> #define N 100000+5using namespace Std;int n, tail; Using tail to mark the first time BFS found leaf node bool Vis[n];int fa[n];    The array used to record the path length vector<int>q[n];void bfs_root (int s) {int i, J;    int Len;    queue<int>p;    while (!p.empty ()) P.pop ();    P.push (s);    Vis[s]=true;        while (!p.empty ()) {int Dd=p.front ();        P.pop ();        Len=q[dd].size ();                for (i=0; i<len; i++) {if (vis[q[dd][i]]==false) {fa[q[dd][i]]=fa[dd]+1;                Vis[q[dd][i]]=true;            P.push (Q[dd][i]);    }}}//tail=1;    int ff=fa[1];        for (j=2; j<=n; J + +) {if (FA[J]&GT;FF) {ff=fa[j]; tail=j;    }}}void bfs_len_tree (int s) {int i, J;    int Len; queue<int>p;    while (!p.empty ()) P.pop ();    Vis[s]=true;    P.push (s);        while (!p.empty ()) {int Dd=p.front ();        P.pop ();        Len=q[dd].size ();                for (i=0; i<len; i++) {if (vis[q[dd][i]]==false) {fa[q[dd][i]]=fa[dd]+1;                Vis[q[dd][i]]=true;            P.push (Q[dd][i]);    }}} int ans=fa[s];    for (j=1; j<=n; J + +) {if (Fa[j]>ans) ans=fa[j]; } printf ("%d\n", ans);}    int main () {scanf ("%d", &n);    int I, J;    int u, v;    for (i=0; i<=n; i++) q[i].clear ();        for (i=0; i<n-1; i++) {scanf ("%d%d", &u, &v);        Q[u].push_back (v);    Q[v].push_back (U);    } memset (Vis, false, sizeof (VIS));    memset (FA, 0, sizeof (FA));    Bfs_root (1);    Memset (Vis, false, sizeof (VIS));    memset (FA, 0, sizeof (FA));    Bfs_len_tree (tail); return 0;}

hihocoder#1050: The longest road in the tree (the longest path algorithm in the tree two times BFS Find root node for maximum +bfs Mark route length +bfs not easy to time out, with Dfs do Tle)

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.