fork download
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. typedef long long ll;
  7. typedef long double ld;
  8. typedef pair<int, int> pii;
  9. typedef pair<ll, ll> pll;
  10. typedef vector<int> vi;
  11. typedef vector<ll> vl;
  12. typedef vector<pii> vii;
  13. typedef vector<pll> vll;
  14. #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  15. #define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
  16. #define all(x) (x).begin(),(x).end()
  17. #define pb push_back
  18. #define ff first
  19. #define ss second
  20. #define mp make_pair
  21. const int N = 5e3 + 1;
  22. const int K = 6;
  23. const int mod = 998244353;
  24.  
  25. int n, k;
  26. vi graf[N], c;
  27. int ile[1 << K], podd[N], dp[N][1 << K], pom[1 << K];
  28.  
  29. void dfs(int v, int ojc){
  30. podd[v] = 1;
  31. dp[v][0] = 1;
  32.  
  33. for(int u : graf[v]){
  34. if(u == ojc) continue;
  35.  
  36. dfs(u, v);
  37.  
  38. for(int mask = 0; mask < (1 << k); mask++) pom[mask] = 0;
  39.  
  40. for(int mask = 0; mask < (1 << k); mask++){
  41. for(int sub = mask;; sub = (sub - 1) & mask){
  42. int a = mask ^ sub;
  43. if(dp[v][a] && dp[u][sub]){
  44. pom[mask] = (pom[mask] + 1LL * dp[v][a] * dp[u][sub]) % mod;
  45.  
  46. for(int i = 1; i <= k; i++){
  47. int bit = 1 << (i - 1);
  48. if(mask & bit) continue;
  49.  
  50. int roz = podd[u] - ile[sub & (bit | (bit - 1))];
  51. if(bit > a && roz == c[i] && __builtin_popcount(sub >> i) == k - i){
  52. pom[mask | bit] = (pom[mask | bit] + 1LL * dp[v][a] * dp[u][sub]) % mod;
  53. }
  54.  
  55. if(bit > sub && roz == ile[bit]){
  56. pom[mask | bit] = (pom[mask | bit] + 1LL * dp[v][a] * dp[u][sub]) % mod;
  57. }
  58. }
  59. }
  60.  
  61. if(sub == 0) break;
  62. }
  63. }
  64.  
  65. podd[v] += podd[u];
  66.  
  67. for(int mask = 0; mask < (1 << k); mask++)
  68. dp[v][mask] = pom[mask];
  69. }
  70. }
  71.  
  72. int main(){
  73. ios_base::sync_with_stdio(0);
  74. cin.tie(0);
  75.  
  76. cin >> n;
  77.  
  78. for(int i = 1; i < n; i++){
  79. int a, b;
  80. cin >> a >> b;
  81. graf[a].push_back(b);
  82. graf[b].push_back(a);
  83. }
  84.  
  85. cin >> k;
  86.  
  87. c.resize(k + 1);
  88. c[0] = n;
  89.  
  90. for(int i = 1; i <= k; i++) cin >> c[i];
  91.  
  92. for(int mask = 0; mask < (1 << k); mask++){
  93. for(int i = 1; i <= k; i++){
  94. if(mask & (1 << (i - 1))) ile[mask] += c[i - 1] - c[i];
  95. }
  96. }
  97.  
  98. dfs(1, 0);
  99. cout << dp[1][(1 << k) - 1] << "\n";
  100.  
  101. return 0;
  102. }
Success #stdin #stdout 0s 5328KB
stdin
3
1 2
2 3
2
2 1
stdout
4