# 「WC 2019」数树

## 「WC 2019」数树

### Task0

#### code

namespace task0{
map<pair<int, int>, int> mp;
int fa[N];
inline int ask(int x){ return x == fa[x] ? x : fa[x] = ask(fa[x]); }
inline void solve(){
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = 1, x, y; i <= 2 * (n - 1); i++)
read(x), read(y), mp[make_pair(min(x, y), max(x, y))]++;;
for(map<pair<int, int>, int>::iterator it = mp.begin(); it != mp.end(); it++) if(it->second == 2)
if(ask(it->first.first) != ask(it->first.second)) fa[ask(it->first.first)] = ask(it->first.second);
int tot = 0;
for(int i = 1; i <= n; i++) if(fa[i] == i) tot++;
cout << Pow(Y, tot) << endl;
}
}

### Task1

$z^k=(z-1+1)^k=\sum_{i=1}^k {k\choose i }(z-1)^i$

$n^{m-2}\prod a_i$

$n^{-2}\sum_{m=1}^n(z-1)^{n-m}n^m\prod_{\sum_{i=1}^m a_i=n,a_i\geq1} a_i$

#### code

namespace task1{
int dp[N][2], Z;
vector<int> g[N];
inline void dfs(int u, int fa){
dp[u][0] = 1, dp[u][1] = n;
for(int i = 0; i < (int) g[u].size(); i++){
int v = g[u][i];
if(v == fa) continue;
dfs(v, u);
dp[u][1] = (1ll * dp[v][1] * dp[u][1] % P + 1ll * (Z - 1) * (1ll * dp[v][1] * dp[u][0] % P + 1ll * dp[v][0] * dp[u][1] % P) % P) % P;
dp[u][0] = (1ll * dp[v][1] * dp[u][0] % P + 1ll * dp[v][0] * dp[u][0] % P * (Z - 1) % P) % P;
}
}
inline void solve(){
Z = Pow(Y);
for(int i = 1, x, y; i < n; i++){
read(x), read(y);
g[x].push_back(y), g[y].push_back(x);
}
dfs(1, 0);
cout << 1ll * dp[1][1] * Pow(Y, n) % P * Pow(n, P - 3) % P << endl;
}
}

### Task 2

$\sum_{m=1}^n(z-1)^{n-m}\sum_{\sum_{i=1}^{m}a_i=n,a_i\geq1}\dfrac{n!}{m!\prod a_i!} \prod a_i^{a_i-2}\left(n^{m-2}\prod a_i\right)^2 \\ =(z-1)^nn^{-4}\sum_{m=1}^n\sum_{\sum_{i=1}^ma_i=n,a_i\geq1}\dfrac{n!}{m!\prod a_i!}\prod(z-1)^{-1}n^2a_i^{a_i} \\$

$f(x) = \sum_{i=1}^{\infty}\dfrac{(z-1)^{-1}n^2i^{i}}{i!}x^i$

$(z-1)^nn^{-4}n![x^n]\exp\left(\sum_{i=1}^{\infty}\dfrac{(z-1)^{-1}n^2i^{i}}{i!}x^i\right)$

#### code

namespace task2{
int js[N], inv[N], ans[N], f[N];
inline void solve(){
if(Y == 1) return (void) (cout << 1ll * Pow(n, n - 2) * Pow(n, n - 2) % P << endl);
poly::init();
js[0] = inv[0] = 1;
for(int i = 1; i <= n; i++)
js[i] = 1ll * js[i-1] * i % P, inv[i] = Pow(js[i], P - 2);
int Z = Pow(Y, P - 2), c = 1ll * Pow(Z - 1, n) * Pow(n, P - 5) % P * js[n] % P;
int c2 = 1ll * n * n % P * Pow(Z - 1, P - 2) % P;
for(int i = 1; i <= n; i++) f[i] = 1ll * c2 * Pow(i, i) % P * inv[i] % P;
poly::getexp(f, ans, n + 1);
cout << 1ll * ans[n] * c % P * Pow(Y, n) % P;
}
}   
posted @ 2019-04-17 18:13  Joyemang33  阅读(...)  评论(...编辑  收藏