首页 > 其他分享 >CF917D Stranger Trees

CF917D Stranger Trees

时间:2023-02-10 21:35:08浏览次数:42  
标签:连通 ifac Stranger int text 划分 Trees CF917D 条边

\(\text{Solution}\)

\(\text{have gained so many tricks。。。}\)
第一步:设恰好重合 \(i\) 条边的答案为 \(f(i)\),至少重合 \(i\) 条边的答案为 \(g(i)\)
那么

\[g(i)=\sum_{j=i}^{n-1}\binom{j}{i}f(j) \]

二项式反演得

\[f(i)=\sum_{j=i}^{n-1}(-1)^{j-i}\binom{j}{i}g(j) \]

于是乎只需求 \(g(i)\)

第二步:转化求 \(g(i)\) 问题:选出 \(i\) 条边相当于把原图分成 \(n-i\) 个连通块,再连边使得所有连通块连通
有结论 \(g(i)=n^{n-i-2}\prod_{j=1}^i s_j\),借助 \(\text{Prufer}\) 即可证明
于是问题相当于求一棵树划分成 \(i\) 个连通块,每种划分方式的贡献为所有连通块大小之积,求所有划分方式的贡献之和

第三步:这个问题可以树形 \(dp\) 解决
设 \(h_{u,i,j}\) 表示以 \(u\) 为根的子树已经划分出 \(i\) 个连通块,\(u\) 所在连通块大小为 \(j\) 且此连通块贡献还未算(即贡献还未乘上 \(j\)) 的答案
可以 \(O(n^3)\) 简单转移
事实上可以做到更优,考虑到一个连通块的贡献相当于这个连通块选出一个数的方案数
于是设 \(h_{u,i,0/1}\) 表示以 \(u\) 为根的子树已经划分出 \(i\) 个连通块,\(u\) 所在连通块还未/已经选出一个数的方案数
那么可以 \(O(n^2)\) 转移

\(\text{Code}\)

#include <bits/stdc++.h>
#define IN inline
#define eb emplace_back
using namespace std;

typedef long long LL;
const int N = 105, P = 1e9 + 7;
int n, g[N][N][2], G[N], sz[N], pw[N], fac[N], ifac[N], tg[N][2];
vector<int> E[N];

IN int C(int n, int m){return (LL)fac[n] * ifac[n - m] % P * ifac[m] % P;}
void dfs(int x, int fa) {
	g[x][1][0] = g[x][1][1] = sz[x] = 1;
	for(auto v : E[x]) if (v ^ fa) {
		dfs(v, x);
		for(int i = 1; i <= sz[x]; i++) {
			for(int j = 1; j <= sz[v]; j++) {
				tg[i + j][0] = (tg[i + j][0] + (LL)g[x][i][0] * g[v][j][1] % P) % P;
				tg[i + j][1] = (tg[i + j][1] + (LL)g[x][i][1] * g[v][j][1] % P) % P;
				tg[i + j - 1][0] = (tg[i + j - 1][0] + (LL)g[x][i][0] * g[v][j][0] % P) % P;
				tg[i + j - 1][1] = (tg[i + j - 1][1] + (LL)g[x][i][1] * g[v][j][0] % P + (LL)g[x][i][0] * g[v][j][1] % P) % P;
			}
		}
		sz[x] += sz[v];
		for(int i = 1; i <= sz[x]; i++) g[x][i][0] = tg[i][0], g[x][i][1] = tg[i][1], tg[i][0] = tg[i][1] = 0;
	}
}

int main() {
	scanf("%d", &n);
	for(int i = 1, u, v; i < n; i++) scanf("%d%d", &u, &v), E[u].eb(v), E[v].eb(u);
	dfs(1, 0), fac[0] = ifac[0] = ifac[1] = pw[0] = 1;
	for(int i = 1; i <= n; i++) fac[i] = (LL)fac[i - 1] * i % P, pw[i] = (LL)pw[i - 1] * n % P;
	for(int i = 2; i <= n; i++) ifac[i] = (LL)ifac[P % i] * (P - P / i) % P;
	for(int i = 2; i <= n; i++) ifac[i] = (LL)ifac[i] * ifac[i - 1] % P;
	for(int i = 0; i < n - 1; i++) G[i] = (LL)pw[n - i - 2] * g[1][n - i][1] % P;
	G[n - 1] = 1;
	for(int i = 0; i < n; i++) {
		int ans = 0;
		for(int j = i; j < n; j++) ans = (ans + (LL)((j - i & 1) ? P - 1 : 1) * C(j, i) % P * G[j] % P) % P;
		printf("%d ", ans);
	}
}

标签:连通,ifac,Stranger,int,text,划分,Trees,CF917D,条边
From: https://www.cnblogs.com/leiyuanze/p/17110330.html

相关文章

  • Lala Land and Apple Trees
    Description:AmrlivesinLalaLand.LalaLandisaverybeautifulcountrythatislocatedonacoordinateline.LalaLandisfamouswithitsappletreesgrowing......
  • Converting Boolean-Logic Decision Trees to Finite State Machines 如何将布尔表达
    ConvertingBoolean-LogicDecisionTreestoFiniteStateMachinesforsimpler,high-performancedetectionofcybersecurityevents将布尔逻辑决策树转换......
  • Educational Codeforces Round 126 (Rated for Div. 2) C. Water the Trees (从答案方
    题目https://codeforces.com/contest/1661/problem/C代码#include<bits/stdc++.h>#definedebug1(a)cout<<#a<<'='<<a<<endl;#definedebug2(a,b)cout<<#a<<"......
  • treemap/treeset 相关 1438
    1438. LongestContinuousSubarrayWithAbsoluteDiffLessThanorEqualtoLimitMedium2790115AddtoListShareGivenanarrayofintegers nums andani......
  • JDK 1.8 TreeSet 源码分析
       /**   *TreeSet的特点:无序 唯一需要比较器自定义<>中的内容需要实现comparable的接口推荐外部实现:多态,自定义多种规则   *底层实现逻辑:二叉红黑......
  • 「AGC018F」Two Trees
    题目点这里看题目。给定两棵树\(A,B\),两棵树均包含\(n\)个结点。结点编号均从\(1\simn\)。现在需要给每个编号分配一个权值,使得两棵树上的任意子树内,所有的结点编......
  • 【luogu CF1707D】Partial Virtual Trees(容斥)(DP)
    PartialVirtualTrees题目链接:luoguCF1707D题目大意给你一棵以1为根的数,问你对于每个长度,有多少个点集序列,第一个点集是全部点,最后一个点集只有1号点,且中间每个点......
  • 总结HashSet和TreeSet的去重
    HashSet的去重添加的对象需要重写hashCode()和equals()方法,其中hashCode()方法,应该是根据自定义类对象的成员属性值计算得来,equals()方法,应该是比较自定义类对象的成员属......
  • java中的TreeSet的add()方法解析
    TreeSet的add()方法解析【添加和去重】1publicclassHomeWork05{2publicstaticvoidmain(String[]args){3//TreeSet最好是同一类型。4......
  • LeetCode-Java-872. Leaf-Similar Trees
    题目Consideralltheleavesofabinarytree.Fromlefttorightorder,thevaluesofthoseleavesformaleafvaluesequence.假装有图Forexample,inthegiven......