当前位置:网站首页>CF edu118f problem solving
CF edu118f problem solving
2022-06-22 11:42:00 【caoyang1123】
The question : Dye a tree and make an arrangement , The value that requires no son coloring in the whole tree is the father value -1, Find the number of solutions .
Obviously, it can be tolerated , Consider that there are at least k A son's dyeing value is a father's value -1, Examine the current k A son to his father's side , Find that these edges form some chains on the tree , The number of schemes contributed has wonderful properties : The internal size relationship of each chain is certain , That is, from the shallowest to the deepest -1, And each chain 、 The size relationship between the unselected points is uncertain , At this point, we find that the number of schemes is ( Chain number + No points selected ) The factorial , Because as long as we fix the size relationship between each other, we can only correspond to an arrangement of the original problem .
At this point, the contribution equivalent to inclusion and exclusion is only related to the one we choose k of , Immediate selection k The first contribution is (n - k)!, And a point on the tree x Choosing a son is a good idea deg(x) How to choose ,sigma deg(x) = 2n, Just divide and conquer fft that will do .
//#define LOCAL
#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define sc second
#define pb push_back
#define ll long long
#define trav(v,x) for(auto v:x)
#define all(x) (x).begin(), (x).end()
#define VI vector<int>
#define VLL vector<ll>
#define pll pair<ll, ll>
#define double long double
//#define int long long
using namespace std;
const int N = 1e6 + 100;
const int inf = 1e9;
//const ll inf = 1e18
const ll mod = 998244353;//1e9 + 7
#ifdef LOCAL
void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T)
{
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
ll qpow(ll x, ll y = mod - 2)
{
ll res = 1;
while(y)
{
if(y & 1)
res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
namespace Poly
{
int wh[N], cc, len;
void ntt(VLL &a, bool inv)
{
for(int i = 0; i < len; i++)
{
if(i < wh[i])
swap(a[i], a[wh[i]]);
}
for(int l = 2, md; l <= len; l <<= 1)
{
md = l >> 1;
ll tmp = qpow(3, (mod - 1) / l);
for(int i = 0; i < len; i += l)
{
ll mo = 1;
for(int j = 0; j < md; j++, mo = mo * tmp % mod)
{
ll ha = mo * a[i + j + md] % mod;
a[i + j + md] = (a[i + j] - ha + mod) % mod;
a[i + j] = (a[i + j] + ha) % mod;
}
}
}
if(inv)
{
ll tmp = qpow(len);
for(int i = 1; i < len / 2; i++)
swap(a[i], a[len - i]);
for(int i = 0; i < len; i++)
a[i] = a[i] * tmp % mod;
}
}
VLL mul(VLL x, VLL y)
{
int ed = x.size() + y.size() - 1;
cc = 0, len = 1;
while(len <= ed)
++cc, len <<= 1;
for(int i = 1; i < len; i++)
{
wh[i] = (wh[i >> 1] >> 1) | (i & 1) << (cc - 1);
}
x.resize(len);
y.resize(len);
ntt(x, 0);
ntt(y, 0);
for(int i = 0; i < len; i++)
{
x[i] = x[i] * y[i] % mod;
}
ntt(x, 1);
x.resize(ed);
return x;
}
}
ll fac[N];
int n, deg[N];
VI adj[N];
VLL buk[N];
void dfs(int x, int ff)
{
trav(v, adj[x])
{
if(v == ff)
continue;
dfs(v, x);
++deg[x];
}
buk[x].pb(1);
if(deg[x])
buk[x].pb(deg[x]);
//cerr << x << ' ' << deg[x] << ' ' << buk[x].size() << '\n';
}
VLL dac(int l, int r)
{
if(l == r)
return buk[l];
int mid = l + r >> 1;
VLL ls = dac(l, mid), rs = dac(mid + 1, r);
return Poly::mul(ls, rs);
}
void sol()
{
cin >> n;
fac[0] = 1;
for(int i = 1; i <= n; i++)
{
fac[i] = fac[i - 1] * i % mod;
}
for(int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(1, 0);
VLL g = dac(1, n);
ll ans = 0;
ll coef = 1;
for(int i = 0; i < g.size(); i++)
{
ll nw = coef * g[i] % mod * fac[n - i] % mod;
ans = (ans + nw) % mod;
coef = mod - coef;
}
cout << ans << '\n';
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
// int tt;
// cin >> tt;
// while(tt--)
sol();
}边栏推荐
- R语言使用自定义函数编写深度学习阶跃step激活函数、并可视化阶跃step激活函数
- 云端极简部署Svelte3聊天室
- 牛客挑战赛55D题解
- Bytearraystream case of IO
- Understanding of thread deadlock
- R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、使用lm函数对匹配后的样本构建线性回归模型、summary函数查看模型的汇总统计信息
- 牛客练习赛94D题解
- CF edu118F 题解
- The R language uses the matchit package for propensity matching analysis and match The data function constructs the matched sample set, and uses visual analysis to test the balance of all covariates i
- Popular understanding of TCP 3-time handshake
猜你喜欢

Development technology of NFT trading platform digital collection system

From prototype chain to inheritance, illustrate the context and recommend collection

Attack and defense drill | threat hunting practice case based on att & CK

2022年遵义市土地基准地价矢量数据(WGS84)

鉴权之cookie、session、JWT

Basic principles of the Internet

云端极简部署Svelte3聊天室

开源代码存在安全隐患:一个项目平均有49个漏洞

什么是同源???跨域错误???如何解决???

How many of the eight classic MySQL errors did you encounter?
随机推荐
牛客挑战赛55E题解
奋斗吧,程序员——第四十五章 柔情似水,佳期如梦
R语言使用read.table加载条件logistic回归分析的数据集(csv数据)、使用unique函数查看配对数据有多少组
xlrd. biffh. XLRDError: Excel xlsx file; Not supported solution
NFT交易平台数字藏品系统开发技术
R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、使用可视化分析检验倾向性评分匹配后样本中的所有协变量的平衡情况
Reader case of IO
二叉树的前序、中序、后序遍历的两种实现
"Dare not doubt the code, but have to doubt the code" a network request timeout analysis
什么是同源???跨域错误???如何解决???
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用summary函数获取模型汇总统计信息
【软工】 概论 & 过程和生命周期建模
Cookies and sessions for answers to common interview questions
R语言dplyr包mutate函数将dataframe中的两个数值变量相除创建新的数据列(Create a new variable)
成功案例 | 安超云助力兰州大学第二医院搭建新型IT基础设施平台 提升医疗信息资源利用率
牛客挑战赛57C题解
1.11 haas506 2.0开发教程-driver-RTC(仅支持2.2以上版本)
奋斗吧,程序员——第三十六章 落花人独立,微雨燕双飞
克鲁斯卡尔重构树
CISP textbook update: introduction to the new knowledge system of eight knowledge domains in 2019