Atcoder Unicyclic Graph Counting

Unicyclic Graph Counting

Count the number of trees with a reference numeral collar, the \ (I \) degrees points to \ (D_i \) , modulo large prime number.

\(N ≤ 300\)

answer

Hamster "miscellaneous title selected to speak."

Suppose we've got a ring of size \ (K \) . Here we consider a new Prüfer coding, do not delete the point on the ring, remove only the point of the tree. Then the tree point \ (U \) the number of occurrences in the encoding necessarily \ (D_u -. 1 \) , ring spot \ (V \) appears in a certain frequency and this encoding is \ (D_v - 2 \) , and Finally, a point sequence must be a point on the ring.

Can be found that when determining the shape of the ring, and on such a coding tree for sets of correspondence. The total length coding \ (N - K \) .

DP can be done directly on the record currently selected and how many points the last determined whether a sequence of points on the ring, the time complexity is \ (O (N ^ 2) \) .

CO int N=310;
int fac[N],ifac[N];
int d[N],f[N][N][2];

int main(){
	int n=read<int>();
	fac[0]=1;
	for(int i=1;i<=n;++i) fac[i]=mul(fac[i-1],i);
	ifac[n]=fpow(fac[n],mod-2);
	for(int i=n-1;i>=0;--i) ifac[i]=mul(ifac[i+1],i+1);
	for(int i=1;i<=n;++i) read(d[i]);
	f[0][0][0]=1;
	for(int i=0;i<n;++i)for(int j=0;j<=i;++j){
		f[i+1][j][0]=add(f[i+1][j][0],mul(f[i][j][0],ifac[d[i+1]-1]));
		f[i+1][j][1]=add(f[i+1][j][1],mul(f[i][j][1],ifac[d[i+1]-1]));
		if(d[i+1]<2) continue;
		f[i+1][j+1][0]=add(f[i+1][j+1][0],mul(f[i][j][0],ifac[d[i+1]-2]));
		f[i+1][j+1][1]=add(f[i+1][j+1][1],mul(f[i][j][1],ifac[d[i+1]-2]));
		if(d[i+1]<3) continue;
		f[i+1][j+1][1]=add(f[i+1][j+1][1],mul(f[i][j][0],ifac[d[i+1]-3]));
	}
	int ans=0;
	for(int j=3;j<n;++j)
		ans=add(ans,mul(f[n][j][1],mul(fac[n-j-1],mul(fac[j-1],i2))));
	if(n>=3) ans=add(ans,mul(f[n][n][0],mul(fac[n-1],i2)));
	printf("%d\n",ans);
	return 0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/12564091.html