题解 CF1172B 【Nauuo and Circle】

· · 题解

设 f_x 为以 x 为根的子树的方案数,d_x 为节点 x 的度数。另外为了方便起见,钦点 1 号点为根。

首先容易发现为了不出现相交的情况,各子树一定对应一个连续的区间。

于是得到如下转移方程:

f_u=(d_v+[u \neq 1])!\prod_{v \in son(u)} f_v

上面这个式子的含义比较显然:对于根节点,其 d_1 棵子树可以任意排列,对于非根节点,除了排列其所有子树之外,它自己的位置也要参与排序。

最终答案为 n f_1。

稍微化简一下上面的式子,就会发现答案为每个节点度数阶乘之积再乘 n。

#include <iostream>
#include <algorithm>
#define MOD 998244353
using namespace std;
int t[200005];
int main()
{
 int n;
 cin>>n;
 long long ans=n;
 for(int i=1;i<n;i++)
 {
  int u,v;
  cin>>u>>v;
  t[u]++,t[v]++;
  ans=ans*t[u]%MOD*t[v]%MOD;
 }
 cout<<ans<<endl;
 return 0;
}