POJ 3162 Walking Race (diameter of tree + monotone queue)

Source: Internet
Author: User

The main topic: to a tree, find out from each node can go to the longest distance (each node can only pass one time), the distance is sorted into an array to get dis[1],dis[2],dis[3]......dis[n], in the series of dis to find the longest interval, The difference between the maximum and minimum values in the interval does not exceed M.

Idea: First find the diameter of the tree two endpoints, then the other nodes in the tree can walk the maximum distance must be to the two points in one of the. So three times BFS can find out the furthest distance, then the furthest distance to find out then use two monotone queue to maintain the maximum and minimum values.

/************************************************************************* > File Name:poj_3162.cpp > Author:howe_young > Mail: [email protected] > Created time:2015 Year September 05 Saturday 14:00 47 sec ************************************************************************/#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#include<queue>using namespaceStd;typedefLong LongLL;Const intMAXN = 1e6 +Ten;inttot, HEAD[MAXN];structEdge {intto, Next, W;} EDGE[MAXN*2];typedef pair<int,int>PII;intDIS1[MAXN], DIS2[MAXN], DIS[MAXN];voidinit () {tot=0; memset (Head,-1,sizeof(head));}voidAddedge (intUintVintW) {edge[tot].to=v; EDGE[TOT].W=W; Edge[tot].next=Head[u]; Head[u]= tot++;}intPos, Maxx;BOOLVIS[MAXN];voidBFsintUint*dist)//search the distance from the U point to each point and save it in the Dist{Maxx=0; Queue<pii>Q; memset (Vis,false,sizeof(VIS));    PII cur, NEX; Cur.first= u; Cur.second =0; Vis[u]=true; Dist[u]=0;    Q.push (cur);  while(!P.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 +EDGE[I].W; DIST[V]=Nex.second; if(Maxx <Nex.second) {Maxx=Nex.second; POS= V;//one end of the diameter is stored in the POS} q.push (NEX); }    }}intQ_MAX[MAXN], Q_MIN[MAXN];intN, M;intSolve ()//maintain maximum and minimum values with two monotone queues respectively{    intAns =0; intfront1, Front2, Tail1, Tail2, I, J; Front1= Front2 =1; Tail1 = TAIL2 =0;  for(j =1, i =1; I <= N; i++)    {         while(Tail1 >= front1 && Dis[q_max[tail1]] <= dis[i]) tail1--; q_max[++TAIL1] =i;  while(Tail2 >= front2 && Dis[q_min[tail2]] >= dis[i]) tail2--; q_min[++TAIL2] =i; if(Dis[q_max[front1]]-dis[q_min[front2]] >m) {ans= Max (ans, I-j);  while(Dis[q_max[front1]]-dis[q_min[front2]] >m) {J= Min (Q_max[front1], Q_min[front2]) +1;  while(Tail1 >= front1 && Q_max[front1] < J) front1++;  while(Tail2 >= front2 && Q_min[front2] < J) front2++; }}} ans= Max (ans, I-j); returnans;}intMain () { while(~SCANF ("%d%d", &n, &m)) {intV, W;        Init ();  for(inti =1; I < n; i++) {scanf ("%d%d", &v, &W); Addedge (i+1, V, W); Addedge (V, I+1, W); } BFS (1, DIS1);        BFS (POS, DIS1);        BFS (POS, DIS2);  for(inti =1; I <= N; i++) Dis[i]=Max (Dis1[i], dis2[i]); printf ("%d\n", Solve ()); }    return 0;}

POJ 3162 Walking Race (diameter of tree + monotone queue)

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.