11package class183 ;
22
3- // 所有路径的魔力和,java版
3+ // 所有合法路径的魔力和,java版
4+ // 一共有n个节点,给定n-1条边,每条边有边权,所有节点组成一棵树
5+ // 给定两个整数l、r,对于任意两个不同节点u、v,考虑它们之间的简单路径
6+ // 如果路径上边的数量在[l, r]范围内,则这条路径是合法路径
7+ // 路径的魔力值 = 路径上所有边权的最大值,打印所有合法路径的魔力和
8+ // 注意,u到v和v到u视为两条不同的路径,均要计入答案
9+ // 1 <= n、边权 <= 10^5
410// 测试链接 : https://www.luogu.com.cn/problem/P5351
511// 提交以下的code,提交时请把类名改成"Main",可以通过所有测试用例
612
1218public class Code07_Maschera1 {
1319
1420 public static int MAXN = 100001 ;
15- public static int n , minEdge , maxEdge ;
21+ public static int n , l , r ;
1622
1723 public static int [] head = new int [MAXN ];
1824 public static int [] nxt = new int [MAXN << 1 ];
@@ -29,15 +35,12 @@ public class Code07_Maschera1 {
2935 public static int [] curMaxv = new int [MAXN ];
3036 public static int [] curEdge = new int [MAXN ];
3137 public static int cntc ;
32-
3338 public static int [] allMaxv = new int [MAXN ];
3439 public static int [] allEdge = new int [MAXN ];
3540 public static int cnta ;
3641
3742 public static int [] tree = new int [MAXN ];
3843
39- public static long ans ;
40-
4144 // 讲解118,递归函数改成迭代所需要的栈
4245 public static int [][] stack = new int [MAXN ][5 ];
4346 public static int stacksize , u , f , maxv , edge , e ;
@@ -88,7 +91,7 @@ public static int lowbit(int i) {
8891 }
8992
9093 public static void add (int i , int v ) {
91- while (i <= maxEdge ) {
94+ while (i <= r ) {
9295 tree [i ] += v ;
9396 i += lowbit (i );
9497 }
@@ -158,7 +161,7 @@ public static void getCentroid2(int cur, int fa) {
158161
159162 // 收集信息递归版,java会爆栈,C++可以通过
160163 public static void dfs1 (int u , int fa , int maxv , int edge ) {
161- if (edge > maxEdge ) {
164+ if (edge > r ) {
162165 return ;
163166 }
164167 curMaxv [++cntc ] = maxv ;
@@ -178,7 +181,7 @@ public static void dfs2(int cur, int fa, int pmaxv, int pedge) {
178181 while (stacksize > 0 ) {
179182 pop ();
180183 if (e == -1 ) {
181- if (edge > maxEdge ) {
184+ if (edge > r ) {
182185 continue ;
183186 }
184187 curMaxv [++cntc ] = maxv ;
@@ -197,7 +200,8 @@ public static void dfs2(int cur, int fa, int pmaxv, int pedge) {
197200 }
198201 }
199202
200- public static void calc (int u ) {
203+ public static long calc (int u ) {
204+ long ans = 0 ;
201205 cnta = 0 ;
202206 for (int e = head [u ]; e > 0 ; e = nxt [e ]) {
203207 int v = to [e ];
@@ -207,9 +211,7 @@ public static void calc(int u) {
207211 dfs2 (v , u , weight [e ], 1 );
208212 sort (curMaxv , curEdge , 1 , cntc );
209213 for (int i = 1 ; i <= cntc ; i ++) {
210- int l = minEdge - curEdge [i ] - 1 ;
211- int r = maxEdge - curEdge [i ];
212- ans -= 1L * curMaxv [i ] * (sum (r ) - sum (l ));
214+ ans -= 1L * curMaxv [i ] * (sum (r - curEdge [i ]) - sum (l - curEdge [i ] - 1 ));
213215 add (curEdge [i ], 1 );
214216 }
215217 for (int i = 1 ; i <= cntc ; i ++) {
@@ -223,42 +225,42 @@ public static void calc(int u) {
223225 }
224226 sort (allMaxv , allEdge , 1 , cnta );
225227 for (int i = 1 ; i <= cnta ; i ++) {
226- int l = minEdge - allEdge [i ] - 1 ;
227- int r = maxEdge - allEdge [i ];
228- ans += 1L * allMaxv [i ] * (sum (r ) - sum (l ));
228+ ans += 1L * allMaxv [i ] * (sum (r - allEdge [i ]) - sum (l - allEdge [i ] - 1 ));
229229 add (allEdge [i ], 1 );
230230 }
231231 for (int i = 1 ; i <= cnta ; i ++) {
232232 add (allEdge [i ], -1 );
233233 }
234234 for (int i = 1 ; i <= cnta ; i ++) {
235- if (allEdge [i ] >= minEdge ) {
235+ if (allEdge [i ] >= l ) {
236236 ans += allMaxv [i ];
237237 }
238238 }
239+ return ans ;
239240 }
240241
241- public static void solve (int u ) {
242- calc (u );
242+ public static long solve (int u ) {
243243 vis [u ] = true ;
244+ long ans = calc (u );
244245 for (int e = head [u ]; e > 0 ; e = nxt [e ]) {
245246 int v = to [e ];
246247 if (!vis [v ]) {
247248 total = siz [v ];
248249 centroid = 0 ;
249250 // getCentroid1(v, 0);
250251 getCentroid2 (v , 0 );
251- solve (centroid );
252+ ans += solve (centroid );
252253 }
253254 }
255+ return ans ;
254256 }
255257
256258 public static void main (String [] args ) throws Exception {
257259 FastReader in = new FastReader (System .in );
258260 PrintWriter out = new PrintWriter (new OutputStreamWriter (System .out ));
259261 n = in .nextInt ();
260- minEdge = in .nextInt ();
261- maxEdge = in .nextInt ();
262+ l = in .nextInt ();
263+ r = in .nextInt ();
262264 for (int i = 1 , u , v , w ; i < n ; i ++) {
263265 u = in .nextInt ();
264266 v = in .nextInt ();
@@ -270,7 +272,7 @@ public static void main(String[] args) throws Exception {
270272 centroid = 0 ;
271273 // getCentroid1(1, 0);
272274 getCentroid2 (1 , 0 );
273- solve (centroid );
275+ long ans = solve (centroid );
274276 out .println (ans << 1 );
275277 out .flush ();
276278 out .close ();
0 commit comments