| Wzj's Travels (ii) |
| Difficulty level: D; operating time limit: 3000ms; operating space limit: 51200KB; code length limit: 2000000B |
| Question Description |
After a few days, Wzj again came to the fantasy country travel. 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! Wzj knows a lot of sights here, so he wants to start from the city to the city T, travel all the cities along the way. Wzj think of exercise, but do not want to be too tired, so the city s to the city T distance must be between L to R. In addition the WZJ stipulates that s must be less than T, can you help him to count how many of the cities (s,t) meet the requirements? |
| Input |
The first behavior is three positive integers of n,l,r. Next N-1 each behavior of three positive integer ui,vi,wi. |
| Output |
| How much output meets the requirements of the city (S,t). |
| Input example |
5 3 6 1 2 3 2 3 3 3 4 2 4 5 1 |
| Output example |
| 6 |
| Other Notes |
Cities to (2,3), (1,3), (2,4), (2,5), (3,5) meet the requirements, a total of 6 1<=n<=100000 1<=l<=r<=10^9 1<=ui,vi<=n 1<=wi<=1000 |
The puzzle: Will write a point of treatment ...
First of the complement set conversion, not the same subtree = total-in the same subtree, then is the path two paragraphs in the same subtree, Dfs again sort foot dafa to seek prefixes, turn into intervals, and then no.
Just a little bit ... (Escape ...
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 using namespacestd;Ten Const intmaxn=100000+Ten, inf=-1u>>1; One structted{intX,Y,W;TED*NXT;} adj[maxn<<1],*fch[maxn],*ms=adj; A voidAddintWintYintx) { -*ms= (Ted) {x,y,w,fch[x]};fch[x]=ms++; -*ms= (Ted) {y,x,w,fch[y]};fch[y]=ms++; the return; - } - BOOLVIS[MAXN]; - intl,r,cg=1, siz[maxn],f[maxn],n,size; + voidFINDCG (intXintFA) { - intmxs=-inf;siz[x]=1; + for(ted*e=fch[x];e;e=e->NXT) { A intv=e->y;if(v!=fa&&!Vis[v]) { atFINDCG (v,x); siz[x]+=Siz[v]; -mxs=Max (mxs,siz[v]); - } -}f[x]=max (mxs,size-siz[x]); - if(F[X]<F[CG]) Cg=x;return; - } in Long Longans; - inta[maxn],dep[maxn],cnt; to voidDfsintXintFaintDist) { +a[cnt++]=Dist; - for(ted*e=fch[x];e;e=e->NXT) { the intv=e->y;if(V!=fa&&!vis[v]) DFS (v,x,dist+e->W); *}return; $ }Panax Notoginseng Long LongCalintXint Base){ -Cnt=0;d FS (x,0,Base);Long Longres=0; theSort (a,a+CNT); + intL=0, r=cnt-1; while(L<r)if(A[l]+a[r]>r) r--;Elseres+=r-l++; AL=0; r=cnt-1; while(L<r)if(a[l]+a[r]>=l) r--;Elseres-=r-l++; the returnRes; + } - voidSolveintx) { $vis[x]=true; ans+=cal (x,0); $ for(ted*e=fch[x];e;e=e->NXT) { - intv=e->y;if(!Vis[v]) { -Ans-=cal (v,e->W); thef[cg=0]=INF;SIZE=SIZ[V];FINDCG (V,0); - solve (CG);Wuyi } the}return; - } WuInlineintRead () { - intx=0, sig=1;CharCh=GetChar (); About while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} $ while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); - returnx*=Sig; - } -InlinevoidWriteLong Longx) { A if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; + 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; - } $ voidinit () { theN=read (); L=read (); R=read (); the for(intI=1; i<n;i++) Add (read (), read (), read ()); thef[cg=0]=INF;SIZE=N;FINDCG (1,0); the solve (CG); write (ans); - return; in } the voidWork () { the return; About } the voidprint () { the return; the } + intMain () { -Init (); work ();p rint ();return 0; the}
Travel of Coj 0346 Wzj (ii)