P3412 仓鼠找sugar II
根据期望的线性性质我们考虑对于每一条边进行计算贡献。
我们可以考虑先算方案数再除以总的点对。
根据期望的定义本质上就是平均数,那么对于一条边 u → v u \to v u→v 的贡献次数就是 ( n − s i z ( u ) ) × s i z ( u ) (n - siz(u)) \times siz(u) (n−siz(u))×siz(u)。
我们考虑一条边有两种情况:
- 向上
- 向下
我们钦定点 u u u 表示 f a ( u ) → u fa(u) \to u fa(u)→u 的边。
- 设 u p ( u ) up(u) up(u) 表示这条边从下走到上 u → f a ( u ) u \to fa(u) u→fa(u)。
- 设 d n ( u ) dn(u) dn(u) 表示这条边从下走到上 f a ( u ) → u fa(u) \to u fa(u)→u。
u p ( u ) up(u) up(u) 一共有两种情况:
- u → v → u → f a ( u ) u \to v \to u \to fa(u) u→v→u→fa(u)
- u → f a ( u ) u \to fa(u) u→fa(u)。
u p ( u ) = 1 d e g ( u ) + ∑ v ∈ s o n ( u ) u p ( v ) + u p ( u ) + 1 d e g ( u ) = d e g ( u ) + ∑ v ∈ s o n ( u ) u p ( v ) \begin{aligned} up( u) &= \frac{1}{deg(u)} + \sum_{v \in son(u)} \frac{up(v) + up(u) + 1}{deg(u)}\\ &= deg(u) + \sum_{v \in son(u)} up(v) \end{aligned} up(u)=deg(u)1+v∈son(u)∑deg(u)up(v)+up(u)+1=deg(u)+v∈son(u)∑up(v)
d n ( u ) dn(u) dn(u) 一共有 3 3 3 种情况:
- f a ( u ) → u fa(u) \to u fa(u)→u
- f a ( u ) → u ′ → f a ( u ) → u fa(u) \to u' \to fa(u) \to u fa(u)→u′→fa(u)→u
- f a ( u ) → f a ( f a ( u ) ) → u fa(u) \to fa(fa(u)) \to u fa(u)→fa(fa(u))→u
设
s
u
m
(
u
)
=
∑
v
∈
s
o
n
(
u
)
u
p
(
v
)
sum(u) = \sum_{v \in son(u)} up(v)
sum(u)=∑v∈son(u)up(v)。
d
n
(
u
)
=
1
d
e
g
(
f
a
(
u
)
)
+
∑
v
∈
s
o
n
(
f
a
(
u
)
)
,
v
≠
u
u
p
(
v
)
+
d
n
(
u
)
+
1
d
e
g
(
f
a
(
u
)
)
+
d
n
(
f
a
(
u
)
)
+
d
n
(
u
)
+
1
d
e
g
(
f
a
(
u
)
)
=
d
e
g
(
u
)
+
s
u
m
(
f
a
(
u
)
)
−
u
p
(
u
)
+
d
n
(
f
a
(
u
)
)
\begin{aligned} dn(u) &= \frac{1}{deg(fa(u))} + \sum_{v \in son(fa(u)), v \ne u} \frac{up(v) + dn(u) + 1}{deg(fa(u))} + \frac{dn(fa(u)) + dn(u) + 1}{deg(fa(u))} \\ &= deg(u) + sum(fa(u)) - up(u) + dn(fa(u)) \end{aligned}
dn(u)=deg(fa(u))1+v∈son(fa(u)),v=u∑deg(fa(u))up(v)+dn(u)+1+deg(fa(u))dn(fa(u))+dn(u)+1=deg(u)+sum(fa(u))−up(u)+dn(fa(u))
之后容易想到一条边的贡献就是向上和向下之和 a n s = s i z ( u ) × ( n − s i z ( u ) ) × ( u p ( u ) + d n ( u ) ) ans = siz(u) \times (n - siz(u)) \times (up(u) + dn(u)) ans=siz(u)×(n−siz(u))×(up(u)+dn(u))。
之后再 ÷ n 2 \div n^2 ÷n2 即可。
#include <bits/stdc++.h>
using namespace std;
//#define Fread
//#define Getmod
#ifdef Fread
char buf[1 << 21], *iS, *iT;
#define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
#define getchar gc
#endif // Fread
template <typename T>
void r1(T &x) {
x = 0;
char c(getchar());
int f(1);
for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48);
x *= f;
}
template <typename T,typename... Args> inline void r1(T& t, Args&... args) {
r1(t); r1(args...);
}
//#define int long long
const int maxn = 2e5 + 5;
const int maxm = maxn << 1;
const int mod = 998244353;
int head[maxn], cnt;
struct Edge {
int to, next;
}edg[maxn << 1];
void add(int u,int v) {
edg[++ cnt] = (Edge) {v, head[u]}, head[u] = cnt;
}
int n;
int up[maxn], dn[maxn], sum[maxn], deg[maxn], siz[maxn];
void dfs1(int p,int pre) {
siz[p] = 1;
for(int i = head[p];i;i = edg[i].next) {
int v = edg[i].to;
++ deg[p];
if(v == pre) continue;
dfs1(v, p);
sum[p] = (sum[p] + up[v]) % mod;
siz[p] += siz[v];
}
up[p] = (deg[p] + sum[p]) % mod;
}
int ksm(int x,int mi) {
int res(1);
while(mi) {
if(mi & 1) res = 1ll * res * x % mod;
mi >>= 1;
x = 1ll * x * x % mod;
}
return res;
}
int ans(0);
void dfs2(int p,int pre) {
if(p != 1) dn[p] = (deg[pre] + sum[pre] - up[p] + dn[pre] + mod) % mod;
ans += 1ll * siz[p] * (n - siz[p]) % mod * (up[p] + dn[p]) % mod;
ans %= mod;
for(int i = head[p];i;i = edg[i].next){
int to = edg[i].to; if(to == pre) continue;
dfs2(to, p);
}
}
signed main() {
// freopen("S.in", "r", stdin);
// freopen("S.out", "w", stdout);
int i, j;
r1(n);
for(i = 1; i < n; ++ i) {
int u, v;
r1(u, v), add(u, v), add(v, u);
}
dfs1(1, 0);
dfs2(1, 0);
// printf("ans = %d\n", ans);
ans = 1ll * ans * ksm(1ll * n * n % mod, mod - 2) % mod;
printf("%d\n", ans);
return 0;
}