首页 > 其他分享 >CF735E Ostap and Tree

CF735E Ostap and Tree

时间:2022-11-03 18:26:00浏览次数:68  
标签:ch CF735E int void Tree Ostap template inline getchar

看到这题,有一个naive的DP做法, \(f[u][i][j]\) 表示 \(u\) 节点的子树内近的黑点距离为 \(i\), 距离最远的非合法点距离为 \(j\), 然后转移一下,貌似是能过的。
但我们可以再做一步小优化。
有一个很神奇的性质,就是当我们合并两棵非合法子树的时候,最深的非合法点并不会被消去,这个性质很好证明。
这告诉我们, 在非合法子树的转移中, \(j\) 的转移和 \(i\) 毫无关联。 那么,就可以把 \(i\), \(j\) 两维状态合并
\(f[u][x]\) 表示当 \(x \leq k\) 时则是本棵子树合法,并且子树内最近的染色点距离 \(u\) 为 \(x\), \(k < x \leq 2k\) 时,表示最近的非合法点的距离距 \(u\) 的距离为 \(x - k - 1\)。
复杂度仅有 \(O(nk^2)\) 非常优秀,轻松通过本题。

#include<bits/stdc++.h>
#define RG register
#define LL long long
#define U(x, y, z) for(RG int x = y; x <= z; ++x)
#define D(x, y, z) for(RG int x = y; x >= z; --x)
using namespace std;
void read(){}
template<typename _Tp, typename... _Tps>
void read(_Tp &x, _Tps &...Ar) {
	x = 0; char ch = getchar(); bool flg = 0;
	for (; !isdigit(ch); ch = getchar()) flg |= (ch == '-');
	for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
	if (flg) x = -x;
	read(Ar...);	
}
inline char Getchar(){ char ch; for (ch = getchar(); !isalpha(ch); ch = getchar()); return ch;}
template <typename T> inline void write(T n){ char ch[60]; bool f = 1; int cnt = 0; if (n < 0) f = 0, n = -n; do{ch[++cnt] = char(n % 10 + 48); n /= 10; }while(n); if (f == 0) putchar('-'); for (; cnt; cnt--) putchar(ch[cnt]);}
template <typename T> inline void writeln(T n){write(n); putchar('\n');}
template <typename T> inline void writesp(T n){write(n); putchar(' ');}
template <typename T> inline void chkmin(T &x, T y){x = x < y ? x : y;}
template <typename T> inline void chkmax(T &x, T y){x = x > y ? x : y;}
template <typename T> inline T Min(T x, T y){return x < y ? x : y;}
template <typename T> inline T Max(T x, T y){return x > y ? x : y;}
inline void readstr(string &s) { s = ""; static char c = getchar(); while (isspace(c)) c = getchar(); while (!isspace(c)) s = s + c, c = getchar();}
inline void FO(string s){freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout);}

const int N = 1e2 + 10, K = 45, mod = 1e9 + 7;

int n, k;
LL f[N][K], t[K];
vector<int> e[N];

inline void dfs(int u, int fa) {
	f[u][0] = 1, f[u][k + 1] = 1;
	for (auto v: e[u]) if (v ^ fa) {
		dfs(v, u);
		memset(t, 0, sizeof t);
		U(i, 0, 2 * k)
			U(j, 0, 2 * k) 
				(t[i + j <= 2 * k ? min(i, j + 1) : max(i, j + 1)]
				+= f[u][i] * f[v][j]) %= mod;
			memcpy(f[u], t, sizeof t);
	}
}

int main(){
	//FO("");
	read(n, k);
	U(i, 2, n) {
		int u, v;
		read(u, v);
		e[u].push_back(v);
		e[v].push_back(u);
	}
	dfs(1, 1);
	LL ans = 0;
	U(i, 0, k) (ans += f[1][i]) %= mod;
	writeln(ans);
	return 0;
}

标签:ch,CF735E,int,void,Tree,Ostap,template,inline,getchar
From: https://www.cnblogs.com/SouthernWay/p/16855387.html

相关文章

  • CF1405D Tree Tag(树的直径/博弈)
    #include<bits/stdc++.h>#defineN300005usingnamespacestd;intn,a,b,da,db;inthead[N],ver[2*N],Next[2*N],tot=0;intp1,p2,mxd=0;intdep......
  • C# 窗体传值,TreeView To TreeView
      usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.T......
  • JAVA++:HashMap无序?TreeMap有序?
    书上说HashMap是无序的,TreeMap是有序的(有序无序是针对key的),但是实际去敲的时候发现不是这样,有时HashMap是有序的,有时TreeMap是无序的。于是就做了以下测试来探究:......
  • 阿里TDM论文阅读《Learning Tree-based Deep Model for Recommender Systems》
    背景推荐本质上需要完成从全量商品库高效检索Topk相关商品,由于候选商品数量过于庞大,现在的推荐系统一般分为两个阶段:召回和排序。对于召回阶段,面临着从全量商品库里面,高效......
  • TreeMap,HashMap,LinkedHashMap区别
    TreeMap,HashMap,LinkedHashMap之间的区别和TreeSet,HashSet,LinkedHashSet之间的区别相似。TreeMap:内部排序,内部使用了红黑树排序HashMap:无序。LinkedHashMap:顺序存取,内部......
  • 决策树Decision Tree
    1.决策树概念:是一种非参数的有监督学习方法,他能从一系列有特征和标签的数据中总结出决策规则,并用树状图来呈现这些规则,以解决分类和回归问题。思想:决策树基于特征对数据......
  • Codeforces 1670 E. Hemose on the Tree
    题意给你个数p,n=2^p;有一棵树有n个节点,告诉你怎么连边;每个点有个权值,每条边也有个权值,权值需要自行分配,[1,2,3..n...2n-1],总共2n-1个权值;你需要选一个节点,使得他到所......
  • Tree 的
    二叉树 就是计划生育政策下,每个人只能最多生两个儿子的意思。 并且分为左子树和右子树。 二叉树又有二叉查找树,也就是二叉排序树。即,左节点都比自己小,右节点都比......
  • TreeSet实现类使用
    【1】存入Integer类型数据(底层用的是内部比较器)packagecom.msb.test09;importjava.util.TreeSet;/***@author:liu*日期:16:09:57*描述:IntelliJIDEA......
  • 机器学习 之 决策树(Decision Tree)文本算法的精确率
    目录​​0、推荐​​​​1、背景​​​​2、效果图​​​​3、本次实验整体流程​​​​4、这里用词向量,而不是TF-IDF预处理后的向量​​​​5、源代码​​​​6、知识点普......