CF802L Send the Fool Further! (hard) 题解

21 篇文章 0 订阅

题面自己去看
原版题面:CF802L
洛谷中文题面:CF802L

状态设计

我们令 f [ i ] f[i] f[i] 表示从 i i i 号节点开始到达叶子节点的期望距离。
当然我们可以得出对于叶子节点有 f [ l e a f ] = 0 f[leaf] = 0 f[leaf]=0

暴力做法

显然对于每个节点 x x x 我们都有以下转移方程 ( 以下方程中会以下标和中括号两种形式表示数组或函数值以体现更清晰的层次 ) :
f [ x ] = ∑ y ∈ { { s o n x } ,    f a x } f [ y ] + d i s x ,    y d e g [ x ] f[x] = \sum_{y \in \{\{son_x\},\; fa_x\}}\frac{f[y]+dis_{x,\;y}}{deg[x]} f[x]=y{{sonx},fax}deg[x]f[y]+disx,y
通过观察可以发现这个式子中包含了未知量 f [ f a x ] f[fa_x] f[fax]。但每个点都构成这样的一个方程,于是我们有 n n n 个未知量和 n n n 个方程,所以我们想到用高斯消元来求解,最终 f [ 1 ] f[1] f[1] 就是答案。

钳制芝士

首先必须明确一点,对于任意一个节点 x x x 都有一个关于自己父亲的一次函数表达形式:
f [ x ] = k x × f [ f a x ] + b x f[x] = k_x \times f[fa_x] + b_x f[x]=kx×f[fax]+bx
至于为什么嘛,你自己画棵树自己手推两个你就知道了。

方程推导

我们注意到每个节点 x x x 的方程中只有一个未知量 ( 除去他自己 ) 也就是 f [ f a x ] f[fa_x] f[fax],于是我们有了以下推导 ( 这句话我乱胡的,看不懂可以直接看下面的推导 ):

首先我们将上面的式子中节点 x x x 的儿子部分和父亲部分分开:
f [ x ] = ∑ y ∈ { s o n x } f [ y ] + d i s x ,    y d e g [ x ] + f [ f a x ] + d i s f a x ,    x d e g [ x ] f[x] = \sum_{y \in \{son_x\}}\frac{f[y]+dis_{x,\;y}}{deg[x]} + \frac{f[fa_x]+dis_{fa_x,\;x}}{deg[x]} f[x]=y{sonx}deg[x]f[y]+disx,y+deg[x]f[fax]+disfax,x
= ∑ y ∈ { s o n x } ( f [ y ] + d i s x ,    y )    +    ( f [ f a x ] + d i s f a x ,    x ) d e g [ x ] =\frac{\sum_{y \in \{son_x\}} (f[y]+dis_{x,\;y}) \;+ \;(f[fa_x]+dis_{fa_x,\;x})}{deg[x]} =deg[x]y{sonx}(f[y]+disx,y)+(f[fax]+disfax,x)

然后我们移项,把 d e g [ x ] deg[x] deg[x] 乘到左边去:
f [ x ] × d e g [ x ] = ∑ y ∈ { s o n x } ( f [ y ] + d i s x ,    y )    +    ( f [ f a x ] + d i s f a x ,    x ) f[x] \times deg[x] = \sum_{y \in \{son_x\}} (f[y]+dis_{x,\;y}) \;+ \;(f[fa_x]+dis_{fa_x,\;x}) f[x]×deg[x]=y{sonx}(f[y]+disx,y)+(f[fax]+disfax,x)

然后我们将 f [ x ] = k x × f [ f a x ] + b x f[x] = k_x \times f[fa_x] + b_x f[x]=kx×f[fax]+bx 代入到上式的 f [ y ] f[y] f[y] 上去得到:
f [ x ] × d e g [ x ] = ∑ y ∈ { s o n x } ( k y ∗ f [ x ] + b y + d i s x ,    y )    +    ( f [ f a x ] + d i s f a x ,    x ) f[x] \times deg[x] = \sum_{y \in \{son_x\}} (k_y * f[x]+ b_y + dis_{x,\;y}) \;+ \;(f[fa_x]+dis_{fa_x,\;x}) f[x]×deg[x]=y{sonx}(kyf[x]+by+disx,y)+(f[fax]+disfax,x)

再将 S i g m a Sigma Sigma 中的东西拆开得:
f [ x ] × d e g [ x ] = ∑ y ∈ { s o n x } k y ∗ f [ x ] + ∑ y ∈ { s o n x } ( b y + d i s x ,    y )    +    ( f [ f a x ] + d i s f a x ,    x ) f[x] \times deg[x] = \sum_{y \in \{son_x\}} k_y * f[x ] + \sum_{y \in \{son_x\}} (b_y +dis_{x,\;y}) \;+ \;(f[fa_x]+dis_{fa_x,\;x}) f[x]×deg[x]=y{sonx}kyf[x]+y{sonx}(by+disx,y)+(f[fax]+disfax,x)

再把与 f [ x ] f[x] f[x] 有关的式子全部扔到左边去得:
f [ x ] × d e g [ x ] − ∑ y ∈ { s o n x } k y ∗ f [ x ] = ∑ y ∈ { s o n x } ( b y + d i s x ,    y )    +    ( f [ f a x ] + d i s f a x ,    x ) f[x] \times deg[x] - \sum_{y \in \{son_x\}} k_y * f[x ] = \sum_{y \in \{son_x\}} (b_y +dis_{x,\;y}) \;+ \;(f[fa_x]+dis_{fa_x,\;x}) f[x]×deg[x]y{sonx}kyf[x]=y{sonx}(by+disx,y)+(f[fax]+disfax,x)
   ⟹    f [ x ] × d e g [ x ] − f [ x ] ∗ ∑ y ∈ { s o n x } k y = ∑ y ∈ { s o n x } ( b y + d i s x ,    y )    +    ( f [ f a x ] + d i s f a x ,    x ) \implies f[x] \times deg[x] - f[x] * \sum_{y \in \{son_x\}} k_y = \sum_{y \in \{son_x\}} (b_y +dis_{x,\;y}) \;+ \;(f[fa_x]+dis_{fa_x,\;x}) f[x]×deg[x]f[x]y{sonx}ky=y{sonx}(by+disx,y)+(f[fax]+disfax,x)
   ⟹    f [ x ] × ( d e g [ x ] − ∑ y ∈ { s o n x } k y ) = ∑ y ∈ { s o n x } ( b y + d i s x ,    y )    +    ( f [ f a x ] + d i s f a x ,    x ) \implies f[x] \times (deg[x] - \sum_{y \in \{son_x\}} k_y) = \sum_{y \in \{son_x\}} (b_y +dis_{x,\;y}) \;+ \;(f[fa_x]+dis_{fa_x,\;x}) f[x]×(deg[x]y{sonx}ky)=y{sonx}(by+disx,y)+(f[fax]+disfax,x)

然后再移项把左边的 d e g [ x ] − ∑ y ∈ { s o n x } k y deg[x] - \sum_{y \in \{son_x\}} k_y deg[x]y{sonx}ky 除到右边去:
f [ x ] = ∑ y ∈ { s o n x } ( b y + d i s x ,    y )    +    ( f [ f a x ] + d i s f a x ,    x ) d e g [ x ] − ∑ y ∈ { s o n x } k y f[x] = \frac{\sum_{y \in \{son_x\}} (b_y +dis_{x,\;y}) \;+ \;(f[fa_x]+dis_{fa_x,\;x})}{deg[x] - \sum_{y \in \{son_x\}} k_y} f[x]=deg[x]y{sonx}kyy{sonx}(by+disx,y)+(f[fax]+disfax,x)

最后把式子整理成 f [ x ] = k x × f [ f a x ] + b x f[x] = k_x \times f[fa_x] + b_x f[x]=kx×f[fax]+bx 的形式:
f [ x ] = 1 d e g [ x ] − ∑ y ∈ { s o n x } k y × f [ f a x ] + ∑ y ∈ { s o n x } ( b y + d i s x ,    y )    +    d i s f a x ,    x d e g [ x ] − ∑ y ∈ { s o n x } k y f[x] = \frac{1}{deg[x] - \sum_{y \in \{son_x\}} k_y} \times f[fa_x] + \frac{\sum_{y \in \{son_x\}} (b_y +dis_{x,\;y}) \;+ \;dis_{fa_x,\;x}}{deg[x] - \sum_{y \in \{son_x\}} k_y} f[x]=deg[x]y{sonx}ky1×f[fax]+deg[x]y{sonx}kyy{sonx}(by+disx,y)+disfax,x

于是我们可以得到递推式:
k x = 1 d e g [ x ] − ∑ y ∈ { s o n x } k y k_x = \frac{1}{deg[x] - \sum_{y \in \{son_x\}} k_y} kx=deg[x]y{sonx}ky1
b x = ∑ y ∈ { s o n x } ( b y + d i s x ,    y )    +    d i s f a x ,    x d e g [ x ] − ∑ y ∈ { s o n x } k y b_x = \frac{\sum_{y \in \{son_x\}} (b_y +dis_{x,\;y}) \;+ \;dis_{fa_x,\;x}}{deg[x] - \sum_{y \in \{son_x\}} k_y} bx=deg[x]y{sonx}kyy{sonx}(by+disx,y)+disfax,x

最后由于 根节点 1 号节点没有父亲,也就是说 f [ 1 ] = b [ 1 ] f[1] = b[1] f[1]=b[1]。所以我们只需要在 dfs 时递归求解 k x k_x kx b x b_x bx 即可 ( b x b_x bx 的方程中含有 k y k_y ky ,所以两个都需要计算),而不需要单独计算 f x f_x fx

注意

原题中的点标号是从 0 开始的!!!(什么恶心东西)

dfs 部分关键代码

void dfs(int x, int fa)
{
	if(deg[x] == 1 && x != 1) {
		k[x] = b[x] = 0;
		return ;
	}
	
	int sumk = 0;
	for(int i = e.hd[x]; i; i = e.nxt[i]) {
		int y = e.to[i], z = e.w[i];
		if(y == fa) {
			b[x] = pls(b[x], z);
			continue;
		}
		
		dfs(y, x);
		b[x] = pls(b[x], b[y]);//这里是加同时取模操作
		b[x] = pls(b[x], z);
		sumk = pls(sumk, k[y]);
	}
	
	k[x] = qpow(dec(deg[x], sumk), MOD - 2);
	b[x] = mul(b[x], k[x]);//同理
}

完整代码

//省去头文件和快读

int n;
struct Edge {
	int hd[MAXN];
	int nxt[MAXN << 1], to[MAXN << 1];
	int w[MAXN << 1];
	int tot = 0;
	
	void Add(int x, int y, int z) {
		nxt[++tot] = hd[x];
		hd[x] = tot;
		to[tot] = y;
		w[tot] = z;
	}
}e;
int deg[MAXN];

//------------以下是一系列的取模操作------------
int pls(int x, int y)
{
	if(x + y >= MOD)
		return x + y - MOD;
	else
		return x + y;
}
int dec(int x, int y) 
{
	if(x - y < 0)
		return x - y + MOD;
	else
		return x - y;
}
int mul(int x, int y) 
{
	if(1ll * x * y >= 1ll * MOD)
		return 1ll * x * y % MOD;
	else
		return x * y;
}
int qpow(int x, int y)
{
	int val = 1;
	while(y) {
		if(y & 1)
			val = mul(val, x);
		
		x = mul(x, x);
		y >>= 1;
	}
	return val;
}
//------------以上是一系列的取模操作------------

int k[MAXN], b[MAXN];
void dfs(int x, int fa)
{
	if(deg[x] == 1 && x != 1) {
		k[x] = b[x] = 0;
		return ;
	}
	
	int sumk = 0;
	for(int i = e.hd[x]; i; i = e.nxt[i]) {
		int y = e.to[i], z = e.w[i];
		if(y == fa) {
			b[x] = pls(b[x], z);
			continue;
		}
		
		dfs(y, x);
		b[x] = pls(b[x], b[y]);
		b[x] = pls(b[x], z);
		sumk = pls(sumk, k[y]);
	}
	
	k[x] = qpow(dec(deg[x], sumk), MOD - 2);//注意要取逆元
	b[x] = mul(b[x], k[x]);
}

int main()
{
	n = inpt();
	for(int i = 1; i < n; i++) {
		int x = inpt() + 1, y = inpt() + 1, z = inpt();
		
		e.Add(x, y, z);
		e.Add(y, x, z);
		deg[x]++, deg[y]++;
	}//对于所有节点都可以式子化为f[x] = k[x] * f[fa] + b[x]
	
	dfs(1, 0);
	
	printf("%d", b[1]);//因为根节点没有父亲,所以 f[1] = b[1] 
	
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值