Mudanjiang. 2014B (graph theory, diameter of tree)

Source: Internet
Author: User

B-building Fire StationsTime limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld &%llu SubmitStatusPracticeZOJ 3820Appoint Description:System Crawler (2015-08-15)

Description

Marjar University is a beautiful and peaceful place. There is N buildings and N -1 bidirectional roads in the campus. These buildings is connected by roads in such a-path that there is exactly one paths between any and buildings. By coincidence, the length of each road is 1 unit.

To ensure the campus security, Edward, the headmaster of Marjar University, plans to setup-fire stations in both differ ENT buildings So firefighters is able to arrive at the scene of the fire as soon as possible whenever fires occur. That means the longest distance between a building and its nearest fire station should is as short as possible.

As a clever and diligent student in Marjar University, you is asked to write a program to complete the plan. Please find out both proper buildings to setup the fire stations.

Input

There is multiple test cases. The first line of input contains an integer indicating the number of the T test cases. For each test case:

The first line contains an integer N (2 <= N <= 200000).

Nfor the next-1 lines, each line contains the integers Xi and Yi . That means there was a road connecting building and Xi building Yi (Indexes is 1-based).

Output

For each test case, output three integers. The first one is the minimal longest distance between a building and its nearest fire station. The next integers is the indexes of the buildings selected to build the fire stations.

If There is multiple solutions, any one'll be acceptable.

Sample Input

241 21 31 451 22 33 44 5

Sample Output

1 1 21) 2 4

1 //#pragma COMMENT (linker, "/stack:1024000000,1024000000")2 using namespacestd;3 Const intM = 2e5 +Ten, INF =111111111 ;4 structNode {5         intL, R;6         intDL, Dr;7 node () {}8Node (intLintRintDlintDR):9 L (L), R (R), DL (DL), Dr (DR) {}Ten }a[m]; One intID, MAXN; A intN; -vector<int>G[m]; - BOOLVis[m]; thevector<int>path; - intN; - intDFS (intOintu) { -A[u] = node (u, u,1,1) ; +          for(vector<int>::iterator v = g[u].begin (); V! = g[u].end (); V + +) { -                 if(*v = = O)Continue ; +                 intTMP =1+ DFS (U, *v); A                 if(tmp > A[U].DL) A[u] = node (a[*v]. L, A[u]. L, TMP, A[U].DL); at                 Else if(tmp > A[u].dr) a[u]. R = A[*v]. L, a[u].dr =tmp; -         } -         if(A[u].dl + a[u].dr > Maxn) maxn = A[u].dl + a[u].dr, id =u; -         returna[u].dl; - } -  in BOOLPATH (intOintu) { -          for(vector<int>::iterator v = g[u].begin (); V! = g[u].end (); V + +) { to                 if(*v = = O)Continue ; +                 if(PATH (U, *v)) {path.push_back (U);return true ;}  -         } the         if(U = = A[id]. R) {path.push_back (U);return true ;}  *         return false ; $ }Panax Notoginseng  - voidDFS (intOintU,intDepintDeep ) { the         if(Dep > Deep)return ; +Vis[u] =1 ; A          for(vector<int>::iterator v = g[u].begin (); V! = g[u].end (); V + +) { the                 if(*v = = O)Continue ; +DFS (U, *v, dep+1, deep); -         } $ } $  - BOOLJudge (intx) { -memset (Vis,0,sizeof(Vis)); theDFS (-1, Path[x],0, x); -DFS (-1, n-x-1= = x? path[x+1]: path[n-x-1] ,0, x);Wuyi         //printf ("x =%d\n", x); the         //if (x = = 1) for (int i = 1; I <= n; i + +) if (!vis[i]) printf ("Geng Shi%d\n", i); -          for(inti =1; I <= N; i + +)if(!vis[i])return false ; Wu         return true ; - } About  $ voidsolve () { -N =path.size (); -         intL =0, r = N/2 ; -         intRET =R; A         //printf ("L =%d, r =%d\n", l, R); +          while(L <=r) { the                 intMID = L + R >>1 ; -                 if(judge (mid)) { $RET =mid; theR = Mid-1 ; the                         //printf ("ret =%d\n", ret); the                 } the                 ElseL = mid +1 ; -         } inprintf ("%d%d%d\n", ret, Path[ret], ret = = n-ret-1? path[ret+1]: path[n-ret-1] ) ;  the } the  About intMain () { the         intT; thescanf ("%d", &T); the          while(T--) { +scanf ("%d", &n); - path.clear (); the                  for(inti =0; I <= N; i + +) g[i].clear ();Bayi                  for(inti =0; I < n-1; i + +) { the                         intu, v; thescanf ("%d%d", &u, &v); - G[u].push_back (v); - g[v].push_back (u); the                 } the  theMAXN =-1 ; theDFS (-1,1) ; -                 //printf ("Dia =%d, Lnode =%d, Rnode =%d\n", Maxn-1, A[id]. L, A[id]. R); thePATH (-1, A[id]. L); the                 //for (Vector<int>::iterator it = Path.begin (); It! = Path.end (); it + +) printf ("%d", *it); Puts (""); the solve ();94         } the         return 0 ; the}
View Code

Because the two possible points are definitely in the diameter of the tree (symmetrical two points), so we can answer the two points, the optimal solution must be within the 0~DIA/2.

In addition to find the diameter of the method can be changed from any point to any one of the maximum depth of the point V, at the beginning of the search from the V to the point of the most depth of u, then u,v is the tree diameter of two endpoints.

My approach is to:

For any one node, he has one of the deepest, and sub-deep sub-nodes l,r. So from any point u start, with U's two deepest sub-nodes to update, u.dl= v1.dl+1, u.dr = v2.dl + 1;

U.L = v1, U.R = v2 (Depth (V1) >= Depth (V2));

This way, you can find out how much dia is with DFS and know who the two endpoints of that diameter are.

The disadvantage is that when you print the DIA path, you need to write a dfs,hhhhh

Also note, Zoj Pit Dad's stack depth is very low, 20w burst, so want to DFS, go to CF hand it: Http://codeforces.com/gym/100554/problem/B

Mudanjiang. 2014B (graph theory, diameter of tree)

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.