NOI.ac #31 MST DP、Hash

题目传送门:http://noi.ac/problem/31

一道思路好题
考虑模拟$Kruskal$的加边方式,然后能够发现非最小生成树边只能在一个已经由边权更小的边连成的连通块中,而树边一定会让两个连通块合为一个,故考虑以连通块为切入点设计$DP$
设字符串$s_1s_2s_3...s_i,s_1 \geq s_2 \geq s_3 \geq ... \geq s_i$表示某一个图中各个连通块的大小(可以发现我们只关心连通块有多大,但不关心连通块内具体有哪些点,因为当所有连通块大小一一对应的时候,方案也是一一对应的),$f_{s_1s_2s_3...s_i}$为通过连边达到这个图的方案数,然后考虑状态的转移


考虑已经加入了边权从$1$至$a_{n-i}$的边,现在即将加入$a_{n-i}$至$a_{n-i+1}-1$的非树边与$a_{n-i+1}$的树边
考虑非树边的加入方案,对于一个图$s_1s_2s_3...s_i$,它的总边数为$\sum\limits_{x=1}^i\frac{s_i \times (s_i-1)}2$,已经加入了$a_{n-i}$条边,所以剩余$\sum\limits_{x=1}^i\frac{s_i \times (s_i-1)}2 - a_{n-i}$可以加入非树边,所以总共的非树边加入方案是$$P_{\sum\limits_{x=1}^i\frac{s_i \times (s_i-1)}2 - a_{n-i}}^{a_{n-i+1}-a_{n-i}-1}$$当然,如果图中非树边数量小于需要加入的非树边,方案就是0,无需转移

接下来考虑树边的加入方案,我们可以在任意两个连通块之中加入边$a_{n-i+1}$,于是枚举这两个连通块$s_x,s_y$,删除$s_x,s_y$、加入$s_x+s_y$并排序得到新的图$s_1's_2'...s_{i-1}'$,那么从$s_1s_2s_3...s_i$到$s_1's_2',...,s_{i-1}'$就有$$P_{\sum\limits_{x=1}^i\frac{s_i \times (s_i-1)}2 - a_{n-i}}^{a_{n-i+1}-a_{n-i}-1}$$种非树边加入方式与$$s_x \times s_y$$种树边加入方式,就有$$f_{s_1's_2'...s_{i-1}'} += f_{s_1s_2s_3...s_i} \times P_{\sum\limits_{x=1}^i\frac{s_i \times (s_i-1)}2 - a_{n-i}}^{a_{n-i+1}-a_{n-i}-1} \times s_x \times s_y$$的答案贡献


最后考虑$f_{s_1s_2s_3...s_i}$的存储方式,显然用$dfs$枚举可行字符串加上$Hash$是很不错的选择。还有一个问题:形如$s_1s_2s_3...s_i$的序列有多少个?可知有$N$个点时序列个数就是$N$的无序整数划分个数,$N=40$时的无序整数划分个数不超过$40000$,然后这道题就解决了
 
 1 #include<bits/stdc++.h>
 2 #define MOD 1000000007
 3 #define int long long
 4 using namespace std;
 5 
 6 map < string , int > m;
 7 int cntLSH , ans[40001] = {0 , 1} , a[41] , N;
 8 
 9 void forLSH(int num , int upNum , string s){
10     //枚举方案用于Hash
11     m.insert(make_pair(s + string(num , 1) , ++cntLSH));
12     for(int i = 2 ; i < upNum && i <= num ; i++)
13         for(int j = 1 ; j * i <= num ; j++)
14             forLSH(num - j * i , i , s + string(j , i));
15 }
16 
17 inline int P(int a , int b){
18 //计算排列
19     if(b > a)
20         return 0;
21     int times = 1;
22     for(int i = a - b + 1 ; i <= a ; i++)
23         times = times * i % MOD;
24     return times;
25 }
26 
27 inline void calc(string s){
28 //DP
29     int t = m.find(s)->second , num = s.size() , cnt = 0;
30     if(num == 1){
31         cout << ans[t] * P((N * (N - 1) >> 1) - a[N - 1] , (N * (N - 1) >> 1) - a[N - 1]) % MOD;
32         //不要忽视了最后几条边!
33         exit(0);
34     }
35     for(int i = 0 ; i < num ; i++)
36         cnt += s[i] * (s[i] - 1) >> 1;
37     int r = P(cnt - a[N - num] , a[N - num + 1] - a[N - num] - 1);
38     //非树边方案总数
39     for(int i = 0 ; i < num ; i++)
40         for(int j = i + 1 ; j < num ; j++){
41         //枚举连接的两个连通块
42                 string s1 = s;
43                 s1.erase(j , 1);
44                 s1.erase(i , 1);
45                 int q = lower_bound(s1.begin() , s1.end() , s[j] + s[i] , greater<char>()) - s1.begin();
46                 s1.insert(q , 1 , s[j] + s[i]);
47                 ans[m.find(s1)->second] = (ans[m.find(s1)->second] + ans[t] * r % MOD * s[j] * s[i]) % MOD;
48             }
49 }
50 
51 void dfs(int num , int upNum , string s){
52 //继续枚举可行方案进行DP
53     calc(s + string(num , 1));
54     for(int i = 2 ; i < upNum && i <= num ; i++)
55         for(int j = 1 ; j * i <= num ; j++)
56             dfs(num - j * i , i , s + string(j , i));
57 }
58 
59 main(){
60     cin >> N;
61     for(int i = 1 ; i < N ; i++)
62         cin >> a[i];
63     forLSH(N , N + 1 , "");
64     dfs(N , N + 1 , "");
65     return 0;
66 }

猜你喜欢

转载自www.cnblogs.com/Itst/p/9748347.html