CF486D Valid Sets(CF486D Valid Sets)

CF486D Valid Sets

题意:

给出一棵有点权的树,求这棵树的满足极差 \(\leqslant k\) 连通子图的个数 ( 点数 \(n \leqslant 2000\) ) 。

解法:

由于点数 \(n \leqslant 2000\) ,所以我们考虑构造一个 \(O(n^2)\) 的算法:

遍历 \(i = 1 .. . n\) , 以 \(i\) 为根 \(dp\) 。

令 \(f_i\) 表示包含点 \(i\) 的极差 \(\leqslant k\) 的连通子图个数。那么转移方程就为: \(f_u += f_u \cdot f_v\) 。

那么如何不重不漏地遍历 \(f_v\) 呢。我们指定 \(a_i\) 为这个集合里最大的点权,所以我们遍历的时候只需要遍历点权在 \(a_i – k\) ~ \(a_i\) 的点就行了。而如果 \(a_v = a_i\) , 我们就只在编号 \(i > v\) 时遍历 \(v\) 。

时间复杂度:

\(O(n^2)\) 。

Code

————————

CF486D Valid Sets

< strong > meaning of the question: < / strong >

Give a tree with point weight and find the number of connected subgraphs (points \ (n \ leqslant 2000 \)) that satisfy the range \ (\ leqslant K \).

< strong > solution: < / strong >

Due to the number of points \ (n \ leqslant 2000 \), we consider constructing an algorithm of \ (O (n ^ 2) \):

Traverse \ (I = 1.. n \) and take \ (I \) as the root \ (DP \).

Let \ (f_i \) represent the number of connected subgraphs containing range \ (\ leqslant K \) of point \ (I \). Then the transfer equation is: \ (f_ + = f_ \ cdot f_v \).

So how to traverse \ (f_v \) without repetition and leakage. We specify \ (a_i \) as the largest point weight in this set, so we only need to traverse the points with the point weight in \ (a_i – K \) ~ \ (a_i \). If \ (a_v = a_i \), we only traverse \ (V \) when numbering \ (I & gt; V \).

< strong > time complexity: < / strong >

\(O(n^2)\) 。

Code