HDU 4607 Park Visit (diameter of tree)

Source: Internet
Author: User

Topic: Given a tree, so that the minimum cost of access to the K points in turn, each edge of the weight of 1.

Thought: If can not go back, then this path is certainly the smallest, it depends on the given k, but how to determine the length of the walk, in fact, this is the diameter of the tree, also known as the longest simple path. After finding the diameter, just compare it with K to determine how many steps to take. Set the diameter of Maxx, if Maxx + 1== K words, it is not necessary to come back to walk the longest road, so when K is less than or equal to Maxx + 1 when the k-1, when K is greater than Maxx + 1, in addition to walk the road without coming back, certainly also to go those used back, The remaining number to come back is k-maxx-1, these all go two times, so multiply two on the line.

#include <cstdio>#include<cstring>#include<algorithm>#include<queue>using namespaceStd;typedef pair<int,int>PII;Const intMAXN =220000;inttot, HEAD[MAXN];structEdge {intto, next;} EDGE[MAXN];BOOLVIS[MAXN];voidinit () {tot=0; memset (Head,-1,sizeof(head));}voidAddedge (intUintv) {edge[tot].to=v; Edge[tot].next=Head[u]; Head[u]= tot++;}intMaxx, POS;voidBFsintP//wide search to find the farthest point from P{Maxx=0; memset (Vis,false,sizeof(VIS)); Queue<pii>Q;    PII cur, NEX; Cur.first= P; Cur.second =0; VIS[P]=true;    Q.push (cur);  while(!Q.empty ()) {cur=Q.front ();        Q.pop ();  for(inti = Head[cur.first]; I! =-1; i =Edge[i].next) {            intv =edge[i].to; if(Vis[v])Continue; VIS[V]=true; Nex.first= V; Nex.second = Cur.second +1; if(Maxx <Nex.second) {Maxx= Nex.second;//The farthest distance is saved in the Maxx.pos = v;//The farthest point is saved in Pos} q.push (NEX); }    }}intMain () {intT, N, M; scanf ("%d", &T);  while(t--) {init (); intu, v; scanf ("%d%d", &n, &m);  for(inti =1; I < n; i++) {scanf ("%d%d", &u, &v);            Addedge (U, v);        Addedge (V, u); } BFS (1);        BFS (POS); intK;  for(inti =0; I < m; i++) {scanf ("%d", &k); ifK1<=Maxx) printf ("%d\n"K1); Elseprintf ("%d\n", Maxx + (K-maxx-1) *2); }    }        return 0;} 

HDU 4607 Park Visit (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.