5196. 【NOIP2017提高組模擬7.3】B (Standard IO)
阿新 • • 發佈:2018-08-16
solution spa bsp NPU fill ron 統計 class qsort
Description
給出一棵樹,求出最小的k,使得,且在樹中存在路徑P,使得k>=S且k<=E。(k為路徑P的邊的權值和)Input
第一行給出N,S,E。N代表樹的點數,S,E如題目描述一致。 下面N-1行給出這棵樹的相鄰兩個節點的邊及其權值W。Output
輸出共一行一個整數,表示答案。若無解輸出-1。Solution
可以說是裸的點分。
細節:若A為重心,B、C兩個點不能在同一子樹上來統計路徑權值。
代碼
1 type 2 arr=record 3 y,w,next:longint;4 end; 5 var 6 min:int64; 7 n,s,e,nm,dt,xxx:longint; 8 a:array [0..200001] of arr; 9 ls,size,v,d,deep,vd:array [0..100001] of longint; 10 procedure add(x,y,z:longint); 11 begin 12 inc(nm); 13 a[nm].y:=y; a[nm].w:=z; 14 a[nm].next:=ls[x]; ls[x]:=nm; 15 end; 16 17procedure init; 18 var 19 i,x,y,z:longint; 20 begin 21 readln(n,s,e); 22 fillchar(ls,sizeof(ls),0); 23 fillchar(v,sizeof(v),0); 24 nm:=0; 25 for i:=1 to n-1 do 26 begin 27 readln(x,y,z); 28 add(x,y,z); add(y,x,z); 29 end; 30 min:=maxlongint*2333; 31 end; 32 33 procedure dfs1(x,last:longint); 34 var 35 i:longint; 36 begin 37 size[x]:=1; i:=ls[x]; 38 while i<>0 do 39 begin 40 if (v[a[i].y]=1) or (a[i].y=last) then 41 begin 42 i:=a[i].next; 43 continue; 44 end; 45 dfs1(a[i].y,x); 46 size[x]:=size[x]+size[a[i].y]; 47 i:=a[i].next; 48 end; 49 end; 50 51 function dfs2(x,last,tot:longint):longint; 52 var 53 i:longint; 54 begin 55 i:=ls[x]; 56 while i<>0 do 57 begin 58 if (v[a[i].y]=1) or (a[i].y=last) or (size[a[i].y]*2<tot) then 59 begin 60 i:=a[i].next; 61 continue; 62 end; 63 exit(dfs2(a[i].y,x,tot)); 64 i:=a[i].next; 65 end; 66 exit(x); 67 end; 68 69 procedure gd(x,last,fa:longint); 70 var 71 i:longint; 72 begin 73 inc(dt); 74 d[dt]:=deep[x]; 75 vd[dt]:=fa; 76 i:=ls[x]; 77 while i<>0 do 78 begin 79 if (a[i].y=last) or (v[a[i].y]=1) then 80 begin 81 i:=a[i].next; 82 continue; 83 end; 84 deep[a[i].y]:=deep[x]+a[i].w; 85 if x=xxx then fa:=a[i].y; 86 gd(a[i].y,x,fa); 87 i:=a[i].next; 88 end; 89 end; 90 91 procedure qsort(l,r:longint); 92 var 93 mid,i,j,k:longint; 94 begin 95 if l>r then exit; 96 i:=l; j:=r; 97 mid:=d[(l+r) div 2]; 98 repeat 99 while d[i]<mid do inc(i); 100 while d[j]>mid do dec(j); 101 if i<=j then 102 begin 103 k:=d[i]; d[i]:=d[j]; d[j]:=k; 104 k:=vd[i]; vd[i]:=vd[j]; vd[j]:=k; 105 inc(i); dec(j); 106 end; 107 until i>j; 108 qsort(i,r); 109 qsort(l,j); 110 end; 111 112 procedure js(x,cz:longint); 113 var 114 i,j:longint; 115 begin 116 dt:=0; deep[x]:=cz; 117 xxx:=x; 118 gd(x,0,x); 119 qsort(1,dt); 120 j:=dt; i:=1; 121 while i<j do 122 if d[i]+d[j]<=e then 123 begin 124 if d[i]+d[j]>=s then 125 begin 126 if (d[i]+d[j]<min) and (vd[i]<>vd[j]) then 127 min:=d[i]+d[j]; 128 if vd[i]=vd[j] then inc(i) 129 else dec(j); 130 end else inc(i); 131 end else dec(j); 132 end; 133 134 procedure main(x:longint); 135 var 136 i:longint; 137 begin 138 js(x,0); 139 v[x]:=1; i:=ls[x]; 140 while i<>0 do 141 begin 142 if v[a[i].y]=1 then 143 begin 144 i:=a[i].next; 145 continue; 146 end; 147 dfs1(a[i].y,0); 148 main(dfs2(a[i].y,0,size[a[i].y])); 149 i:=a[i].next; 150 end; 151 end; 152 153 begin 154 init; 155 dfs1(1,0); 156 main(dfs2(1,0,n)); 157 if min=maxlongint*2333 then min:=-1; 158 write(min); 159 end.
5196. 【NOIP2017提高組模擬7.3】B (Standard IO)