917D - Stranger Trees - CodeForces Solution


dp math matrices trees *2600

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<fstream>
#include<vector>
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define per(i,b,a) for(int i = (b); i >= (a); i--)
#define N 105
#define ll long long
#define mod 1000000007
using namespace std;

int n, k, p[N];
vector<int> son[N];
ll C[N][N], dp[N][N][2], pd[N][2], g[N];

ll qpow(ll a, int b){
ll ret = 1;
for(; b; b >>= 1){ if(b&1) (ret *= a) %= mod; (a *= a) %= mod; }
return ret;
}

void dfs(int x, int fa){
dp[x][1][0] = dp[x][1][1] = 1;
for(int y : son[x]) if(y != fa){
dfs(y, x);
rep(i,1,n) rep(j,1,n) rep(a,0,1) rep(b,0,1){
ll v = dp[x][i][a] * dp[y][j][b] % mod;
if(b) (pd[i+j][a] += v * n) %= mod;
if(a+b < 2) (pd[i+j-1][a+b] += v) %= mod;
}
rep(i,1,n) rep(p,0,1) dp[x][i][p] = pd[i][p], pd[i][p] = 0;
}
}

int main(){
cin>>n;
int u, v;
rep(i,2,n) cin>>u>>v, son[u].push_back(v), son[v].push_back(u);

C[0][0] = 1;
rep(i,1,n){
C[i][0] = 1;
rep(j,1,i) C[i][j] = (C[i-1][j] + C[i-1][j-1]) % mod;
}
dfs(1, 0);
rep(i,1,n){
g[i] = dp[1][i][1] * qpow(n, mod-2) % mod;
rep(j,1,i-1) (g[i] += mod - C[n-i+j][j] * g[i-j] % mod) %= mod;
}
per(i,n,1) cout<< g[i] <<" \n"[i == 1];
return 0;
}


Comments

Submit
0 Comments
More Questions

1738C - Even Number Addicts
1064B - Equations of Mathematical Magic
384A - Coder
1738B - Prefix Sum Addicts
1352D - Alice Bob and Candies
1316D - Nash Matrix
1548B - Integers Have Friends
348A - Mafia
315B - Sereja and Array
204A - Little Elephant and Interval
385B - Bear and Strings
114C - Grammar Lessons
1427A - Avoiding Zero
583A - Asphalting Roads
1358B - Maria Breaks the Self-isolation
828A - Restaurant Tables
1735A - Working Week
1735D - Meta-set
1735B - Tea with Tangerines
1735C - Phase Shift
1321C - Remove Adjacent
281B - Nearest Fraction
1043A - Elections
1598C - Delete Two Elements
1400C - Binary String Reconstruction
1734D - Slime Escape
1499A - Domino on Windowsill
991A - If at first you don't succeed
1196C - Robot Breakout
373A - Collecting Beats is Fun