# 【做题】ZJOI2017仙人掌——组合计数

$n \leq 5 \times 10^5, \ m \leq 10^6$

#### sol1

$dp_i$表示在结点$i$的子树中的答案。考虑如何转移。

• 不匹配。即$g_{n-1}$
• 匹配。那么枚举它和哪个元素匹配。即$(n-1)g_{n-2}$

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
#define fir first
#define sec second
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define rrp(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define gc() getchar()
template <typename tp>
x = 0; char tmp; bool key = 0;
for (tmp = gc() ; !isdigit(tmp) ; tmp = gc())
key = (tmp == '-');
for ( ; isdigit(tmp) ; tmp = gc())
x = (x << 3) + (x << 1) + (tmp ^ '0');
if (key) x = -x;
}

const int N = 500010, M = 1000010, MOD = 998244353;
int power(int a,int b) {
int ret = 1;
while (b) {
if (b & 1) ret = 1ll * ret * a % MOD;
a = 1ll * a * a % MOD;
b >>= 1;
}
return ret;
}
inline void Add(int& x,int y) {
x = x + y >= MOD ? x + y - MOD : x + y;
}
struct edge {
int la,b;
} con[M << 1];
int tot,fir[N],n,m,ans;
con[++tot] = (edge) {fir[from], to};
fir[from] = tot;
}
int dfn[N], low[N], col[N], sta[N], top, ecnt, ccnt;
pii edg[M];
void dfs(int pos,int fa) {
sta[low[pos] = dfn[pos] = ++ top] = pos;
for (int i = fir[pos] ; i ; i = con[i].la) {
if (con[i].b == fa) continue;
if (dfn[con[i].b]) low[pos] = min(low[pos], dfn[con[i].b]);
else {
dfs(con[i].b, pos);
low[pos] = min(low[pos], low[con[i].b]);
if (low[con[i].b] >= dfn[pos]) {
if (low[con[i].b] > dfn[pos])
edg[++ecnt] = pii(pos, con[i].b);
else ++ ccnt;
top = dfn[pos];
}
}
}
}
int dp[N], sdp[N], sz[N], jc[N], inv[N], vis[N];
void fsd(int pos,int fa) {
vis[pos] = 1;
dp[pos] = 1;
sz[pos] = 1;
int num = 0;
for (int i = fir[pos] ; i ; i = con[i].la) {
if (con[i].b == fa) continue;
fsd(con[i].b, pos);
sz[pos] += sz[con[i].b];
dp[pos] = 1ll * sdp[con[i].b] * dp[pos] % MOD;
++ num;
}
int tmp = 0, tmp1 = 0;
for (int k = 0, ipw2 = 1 ; k * 2 <= num ; ++ k) {
Add(tmp,1ll * jc[num] * inv[k] % MOD * inv[num - 2 * k] % MOD * ipw2 % MOD);
ipw2 = 1ll * ipw2 * (MOD + 1) / 2 % MOD;
}
-- num;
for (int k = 0, ipw2 = 1 ; k * 2 <= num ; ++ k) {
Add(tmp1,1ll * jc[num] * inv[k] % MOD * inv[num - 2 * k] % MOD * ipw2 % MOD);
ipw2 = 1ll * ipw2 * (MOD + 1) / 2 % MOD;
}
for (int i = fir[pos] ; i ; i = con[i].la) {
if (con[i].b == fa) continue;
Add(sdp[pos], 1ll * dp[pos] * power(sdp[con[i].b], MOD-2) % MOD * tmp1 % MOD * sdp[con[i].b] % MOD);
}
dp[pos] = 1ll * dp[pos] * tmp % MOD;
}
void init() {
memset(fir,0,sizeof(int) * (n + 5));
memset(dfn,0,sizeof(int) * (n + 5));
tot = ecnt = ccnt = top = 0;
ans = 1;
memset(dp,0,sizeof(int) * (n + 5));
memset(sdp,0,sizeof(int) * (n + 5));
memset(vis,0,sizeof(int) * (n + 5));
}
void solve() {
init();
jc[0] = 1;
rep (i, 1, n) jc[i] = 1ll * jc[i-1] * i % MOD;
inv[n] = power(jc[n], MOD - 2);
rrp (i, n-1, 0) inv[i] = 1ll * inv[i+1] * (i+1) % MOD;
for (int i = 1, x, y ; i <= m ; ++ i) {
}
rep (i, 1, n) if (!dfn[i]) dfs(i, 0);
if (n - 1 + ccnt != m) {
puts("0");
return;
}
tot = 0;
memset(fir,0,sizeof(int) * (n + 5));
rep (i, 1, ecnt) {
}
rep (i, 1, n) {
if (vis[i]) continue;
fsd(i, 0);
ans = 1ll * dp[i] * ans % MOD;
}
ans = (ans % MOD + MOD) % MOD;
printf("%d\n", ans);
}
int main() {
int T;
while (T --)
solve();
return 0;
}

#### sol2

• 让结点$i$负责向上连边。那么答案就是$\prod_{v \in child_i} sdp_v g_{|child_i|}$
• $i$的某个孩子内的结点向上连边。我们就枚举这是哪个孩子内的结点。即$|child_i| \times \prod_{v \in child_i} sdp_v g_{|child_i|-1}$

posted @ 2019-03-06 22:56 莫名其妙的aaa 阅读(...) 评论(...) 编辑 收藏