Main idea:
Let's give you the complete graph of nn points and the tree TT of nn points, find out how many spanning trees there are, and satisfy that there are kk edges on the tree TT.
For any k ∈ [1,n − 1]k ∈ [1,n − 1], we have to answer the question.
Analysis:
The spanning tree counting can think of the matrix tree theorem, but it is obvious that the value of the matrix tree is the sum of the product of the edge weights, and it is impossible to count the number of coincident edges between the edge on the tree and the given tree.
Consider an edge as a polynomial. If the edge is on the original tree, the weight of the edge is seen as x1x1, otherwise as 11. So the total weight of a tree is xkxk, where kk is the number of edges that coincide with the original tree. Then we can sum these polynomials by matrix tree. The sum is a polynomial. The coefficient of xkxk is the same as that of the original tree.
We can't take polynomials into account, but we know that the degree of the final polynomials is n − 1n − 1. So we can take nn number instead of xx to find determinant, because it requires coefficient, so it's better to directly violence Gauss elimination interpolation. The complexity is O(n4)O(n4).
Code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define LL long long
const int maxn=105;
const LL mod=1e9+7;
using namespace std;
int n,x,y;
LL a[maxn][maxn],c[maxn][maxn],b[maxn];
LL power(LL x,LL y)
{
if (y==1) return x;
LL c=power(x,y/2);
c=(c*c)%mod;
if (y%2) c=(c*x)%mod;
return c;
}
LL det(int n)
{
LL ans=1;
for (int i=1;i<=n;i++)
{
LL inv=power(c[i][i],mod-2);
for (int j=i+1;j<=n;j++)
{
LL rate=c[j][i]*inv%mod;
for (int k=i;k<=n;k++)
{
c[j][k]=(c[j][k]+mod-rate*c[i][k]%mod)%mod;
}
}
ans=(ans*c[i][i])%mod;
if (!c[i][i]) break;
}
return ans;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
a[x][y]=1;
a[y][x]=1;
}
for (int k=1;k<=n;k++)
{
memset(c,0,sizeof(c));
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (i!=j)
{
if (a[i][j]) c[i][j]=mod-k,c[i][i]+=k;
else c[i][j]=mod-1,c[i][i]+=1;
}
}
}
b[k]=det(n-1);
}
for (int i=1;i<=n;i++)
{
c[i][1]=1;
for (int j=2;j<=n;j++) c[i][j]=(c[i][j-1]*i)%mod;
}
for (int i=1;i<=n;i++)
{
LL inv=power(c[i][i],mod-2);
for (int j=1;j<=n;j++)
{
if (i==j) continue;
LL rate=c[j][i]*inv%mod;
for (int k=i;k<=n;k++)
{
c[j][k]=(c[j][k]+mod-rate*c[i][k]%mod)%mod;
}
b[j]=(b[j]+mod-rate*b[i]%mod)%mod;
}
}
for (int i=1;i<=n;i++)
{
b[i]=b[i]*power(c[i][i],mod-2)%mod;
printf("%lld ",b[i]);
}
}