Coj 0349 Wzj's Travels (Fri)

Source: Internet
Author: User

Wzj's Travels (Fri)
Difficulty level: E; operating time limit: 3000ms; operating space limit: 262144KB; code length limit: 2000000B
Question Description

Wzj again to travel t^t=0. Fantasy Country is composed of N cities, because of the road renovation, only N-1 two-way road can be used, the article I bidirectional road from the UI connected to VI, the distance is WI. But this N-1 road is connected to the whole country, there is a road between every two cities!

Set D (A, A, b) to indicate the distance between city A and City C (a<b). Wzj want to know the value of the first K Big D (A, b) please tell him the result.

Input
The first is a n,k of two positive integers.
Next N-1 each behavior of three positive integer ui,vi,wi.
Output
Outputs a large d (a, A, b) value in front of each row.
Input example
5 10
1 2 1
1 3 2
2 4 3
2 5 4
Output example
7
7
6
5
4
4
3
3
2
1
Other Notes
1<=n<=50000,1<=k<=min (300000,n* (N-1)/2)
1<=ui,vi<=n
1<=wi<=1000

Puzzle: Point Sub-set a super piano Yes, the location of a point can be recorded with the L,r array ("Enough to reach" ...) ), then the problem becomes super piano ....

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 #defineCH for (int d=0;d<2;d++) if (Ch[d])Ten #defineLson x->ch[0],l,m One #defineRson X->ch[1],m+1,r A using namespacestd; - Const intmaxn=500000+Ten, maxnode=3000000+Ten, maxm=1000000+Ten, maxt=maxn* -, inf=-1u>>1; - structted{intX,Y,W;TED*NXT;} adj[maxm],*fch[maxn],*ms=adj; the voidAddintXintYintW) { -*ms= (Ted) {x,y,w,fch[x]};fch[x]=ms++; -*ms= (Ted) {y,x,w,fch[y]};fch[y]=ms++; -     return; + } - intCg,size,siz[maxn],f[maxn],l[maxt],r[maxt],t[maxt],ln,rn,tot;BOOLVIS[MAXN]; + voidFINDCG (intXintFA) { Asiz[x]=1;intmxs=0; at      for(ted*e=fch[x];e;e=e->NXT) { -         intv=e->y;if(v!=fa&&!Vis[v]) { -FINDCG (v,x); siz[x]+=siz[v];mxs=Max (mxs,siz[v]); -         } -}f[x]=max (Mxs,size-siz[x]);if(F[X]&LT;F[CG]) Cg=x;return; - } in voidDfsintXintFaintdis) { -siz[x]=1; l[++tot]=ln;r[tot]=rn;t[tot]=dis; to      for(ted*e=fch[x];e;e=e->NXT) { +         intv=e->y;if(V!=fa&&!vis[v]) DFS (v,x,dis+e->w), siz[x]+=Siz[v]; -}return; the } * voidSolveintx) { $vis[x]=true;BOOLflag=true;Panax Notoginseng      for(ted*e=fch[x];e;e=e->NXT) { -         intv=e->y;if(!Vis[v]) { the             if(flag) flag=false, ln=rn=++tot; +DFS (V,X,E-&GT;W); rn=tot; A         } the     } +      for(ted*e=fch[x];e;e=e->NXT) { -         intv=e->y;if(!Vis[v]) { $f[cg=0]=size=SIZ[V];FINDCG (v,x); solve (CG); $         } -}return; - } the structnode{ -node*ch[2];intP,v;node () {v=-inf;}Wuyi     voidUpdate () {v=-inf; ch{if(V<ch[d]->v) P=ch[d]->p,v=ch[d]->v;}return;} the}seg[maxnode],*nodecnt=seg,*root;intN,k,ql,qr,_p,_v; - voidBuild (Node*&x=root,intL=1,intR=tot) { Wux=nodecnt++;intM=l+r>>1;if(l==r) x->p=m,x->v=T[m]; -     ElseBuild (Lson), Build (Rson), x->update ();return; About } $ voidQuery (Node*x=root,intL=1,intR=tot) { -     if(ql<=l&&r<=qr) { -         if(_V&LT;X-&GT;V) _v=x->v,_p=x->p; -}Else{intM=l+r>>1; A         if(ql<=m) query (Lson);if(qr>M) query (Rson); +}return; the } - structinfo{intx,l,r,t;}; $ BOOL operator< (ConstInfo&a,ConstINFO&AMP;B) {returnt[a.t]+t[a.x]<t[b.t]+t[b.x];} thePriority_queue<info>Q; theInlineintRead () { the     intx=0, sig=1;CharCh=GetChar (); the      for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') sig=0; -      for(; isdigit (ch); Ch=getchar ()) x=Ten*x+ch-'0'; in     returnsig?x:-x; the } theInlinevoidWriteLong Longx) { About     if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; the     intlen=0;Long Longbuf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; the      for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; the } + voidinit () { -N=read (); K=read ();intx, y; the      for(intI=1; i<n;i++) X=read (), y=read (), add (X,y,read ());Bayif[cg=0]=SIZE=N;FINDCG (1,0); solve (CG); the build (); the      for(intI=1; i<=tot;i++)if(L[i]) { -ql=l[i];qr=r[i];_v=-Inf;query (); Q.push ((info) {i,l[i],r[i],_p}); -     } the      for(intI=1; i<=k;i++){ theInfo A=q.top (); Q.pop (); Write (t[a.x]+T[A.T]); ENT; the         if(A.L&LT;A.T) {ql=a.l;qr=a.t-1; _v=-inf;query (); Q.push ((info) {a.x,a.l,a.t-1, _p});} the         if(A.T&LT;A.R) {ql=a.t+1; Qr=a.r;_v=-inf;query (); Q.push ((info) {a.x,a.t+1, a.r,_p});} -     } the     return; the } the voidWork () {94     return; the } the voidprint () { the     return;98 } About intMain () {init (); work ();p rint ();return 0;}

Coj 0349 Wzj's Travels (Fri)

Related Keywords:

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.