Solution -「HDU #6566」The Hanged Man
\(\mathcal{Description}\)
Link.
给定一棵含 \(n\) 个点的树,每个结点有两个权值 \(a\) 和 \(b\)。对于 \(k\in[1,m]\),分别求
\]
其中 \(S\) 是树上的一个独立点集。
测试数据组数 \(\le20\),\(n\le50\),\(m\le5\times10^3\)。
\(\mathcal{Solution}\)
一个 naive 的 DP 方法是,令 \(f(u,i,0/1)\) 表示 \(u\) 子树内,入 \(S\) 的点 \(a\) 值之和为 \(i\),且 \(u\) 入/未入集时最大的 \(b\) 值之和及其方案数。这种做法慢在需要 \(\mathcal O(m^2)\) 合并两棵子树的 DP 信息。我们尝试转化成一种仅需要单点更新的 DP。
考虑点分树,因为结点 \(u\) 在原树上的邻接点必然是点分树上 \(u\) 的祖先或子树内的孩子,所以可以在点分树上以 DFN 的顺序 DP,令状态 \(f(i,s,j)\) 表示考虑了 DFN 在 \([1,i]\) 之间的结点,设 \(u\) 是 DFN 为 \(i\) 的结点,那么 \(s\) 为 \(u\) 的祖先中被选结点的深度集合,\(j\) 为 \(a\) 值之和,状态表示最大值及方案数。
以上,每次暴力插入一个结点更新 DP 数组,就能做到 \(\mathcal O(Tnm2^{\log n})=\mathcal O(Tn^2m)\) 了。
(我写 T 了,也许是代码问题,抱歉 qwq。
\(\mathcal{Code}\)
会 T 掉的可怜代码。
/* Clearink */
#include <cstdio>
#define rep( i, l, r ) for ( int i = l, repEnd##i = r; i <= repEnd##i; ++i )
#define per( i, r, l ) for ( int i = r, repEnd##i = l; i >= repEnd##i; --i )
typedef long long LL;
inline void chkmax( int& a, const int b ) { a < b && ( a = b, 0 ); }
const int MAXN = 100, MAXM = 5e3;
int n, m, a[MAXN + 5], b[MAXN + 5];
int siz[MAXN + 5], wgt[MAXN + 5], ban[MAXN + 5], dep[MAXN + 5];
bool vis[MAXN + 5];
struct Graph {
int ecnt, head[MAXN + 5], to[MAXN * 2 + 5], nxt[MAXN * 2 + 5];
inline void operator () ( const int u, const int v ) {
to[++ecnt] = v, nxt[ecnt] = head[u], head[u] = ecnt;
}
inline void clear( const int n ) {
ecnt = 0;
rep ( i, 1, n ) head[i] = 0;
}
} srcT, divT;
#define adj( T, u, v ) \
for ( int i = T.head[u], v; v = T.to[i], i; i = T.nxt[i] )
struct Atom {
int val; LL cnt;
Atom(): val( 0 ), cnt( 0 ) {}
Atom( const int v, const LL c ): val( v ), cnt( c ) {}
inline Atom operator + ( const int x ) {
return cnt ? Atom( val + x, cnt ) : Atom();
}
inline Atom& operator += ( const Atom& u ) {
if ( val == u.val ) cnt += u.cnt;
if ( val < u.val ) *this = u;
return *this;
}
} f[MAXN * 4 + 5][MAXM + 5];
inline void findG( const int u, const int fa, const int all, int& rt ) {
siz[u] = 1, wgt[u] = 0;
adj ( srcT, u, v ) if ( !vis[v] && v != fa ) {
findG( v, u, all, rt ), siz[u] += siz[v];
chkmax( wgt[u], siz[v] );
}
chkmax( wgt[u], all - siz[u] );
if ( !rt || wgt[u] < wgt[rt] ) rt = u;
}
inline void build( const int u ) {
vis[u] = true;
adj ( srcT, u, v ) if ( !vis[v] ) {
int rt = 0; findG( v, 0, siz[v], rt );
divT( u, rt ), dep[rt] = dep[u] + 1;
build( rt );
}
}
inline void solve( const int u, int& lasd ) {
int d = dep[u];
rep ( i, 1 << d, ( 1 << lasd << 1 ) - 1 ) {
Atom *cf = f[i & ( ( 1 << d ) - 1 )], *lf = f[i];
rep ( j, 0, m ) cf[j] += lf[j], lf[j] = Atom();
}
rep ( i, 0, ( 1 << d ) - 1 ) {
if ( i & ban[u] ) continue;
Atom *cf = f[i | 1 << d], *lf = f[i];
rep ( j, 0, m - a[u] ) cf[j + a[u]] += lf[j] + b[u];
}
lasd = d;
adj ( divT, u, v ) solve( v, lasd );
}
int main() {
int T; scanf( "%d", &T );
rep ( cas, 1, T ) {
scanf( "%d %d", &n, &m );
srcT.clear( n ), divT.clear( n );
rep ( i, 1, n ) vis[i] = false;
// f[][] is cleared when outputing;
// ban[] is cleared when calculating.
rep ( i, 1, n ) scanf( "%d %d", &a[i], &b[i] );
for ( int i = 2, u, v; i <= n; ++i ) {
scanf( "%d %d", &u, &v );
srcT( u, v ), srcT( v, u );
}
int rt = 0; findG( 1, 0, n, rt );
build( rt );
rep ( u, 1, n ) {
ban[u] = 0;
adj ( srcT, u, v ) ban[u] |= ( dep[v] < dep[u] ) << dep[v];
}
f[0][0].cnt = 1;
int lasd = 0; solve( rt, lasd );
printf( "Case %d:\n", cas );
rep ( i, 1, m ) {
Atom ans;
rep ( j, 0, ( 1 << lasd << 1 ) - 1 ) {
ans += f[j][i], f[j][i] = Atom();
}
printf( "%lld%c", ans.cnt, i ^ m ? ' ' : '\n' );
}
}
return 0;
}
Solution -「HDU #6566」The Hanged Man的更多相关文章
- Solution -「HDU 6875」Yajilin
\(\mathcal{Description}\) Link.(HDU 裂开了先放个私链 awa.) 在一个 \(n\times n\) 的方格图中,格子 \((i,j)\) 有权值 \(w_ ...
- Solution -「HDU 5498」Tree
\(\mathcal{Description}\) link. 给定一个 \(n\) 个结点 \(m\) 条边的无向图,\(q\) 次操作每次随机选出一条边.问 \(q\) 条边去重后构成生成 ...
- Solution -「HDU 6643」Ridiculous Netizens
\(\mathcal{Description}\) Link. 给定一棵含有 \(n\) 个结点的树,点 \(u\) 有点权 \(w_u\),求树上非空连通块的数量,使得连通块内点权积 \(\ ...
- Solution -「HDU 1788」CRT again
\(\mathcal{Description}\) Link. 解同余方程组: \[x\equiv m_i-a\pmod{m_i} \] 其中 \(i=1,2,\dots,n\). \ ...
- Solution -「ARC 104E」Random LIS
\(\mathcal{Description}\) Link. 给定整数序列 \(\{a_n\}\),对于整数序列 \(\{b_n\}\),\(b_i\) 在 \([1,a_i]\) 中等概率 ...
- Solution -「HDU」Professor Ben
Description 有 \(Q\) 个询问.每次给定一个正整数 \(n\),求它的所有因数的质因数个数的和. Solution 就讲中间的一个 Trick. 我们定义正整数 \(x\) 有 \(f ...
- Solution -「CTS 2019」「洛谷 P5404」氪金手游
\(\mathcal{Description}\) Link. 有 \(n\) 张卡牌,第 \(i\) 张的权值 \(w_i\in\{1,2,3\}\),且取值为 \(k\) 的概率正比于 \ ...
- Solution -「BZOJ 3812」主旋律
\(\mathcal{Description}\) Link. 给定含 \(n\) 个点 \(m\) 条边的简单有向图 \(G=(V,E)\),求 \(H=(V,E'\subseteq E)\ ...
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
随机推荐
- Apache Ant: If 和 Unless
目录 If And Unless If And Unless 从 Ant 1.9.1 起,可以在所有的任务和嵌套的元素上以特别的命名空间添加 if 和 unless 属性. In order to u ...
- java调用redis的多种方式与心得
心得: /** * 心得: * 1.连接方式主要有:直连同步,直连事务,直连管道,直连管道事务,分布式直连同步,分布式直连管道, * 分布式连接池同步,分布式连接池管道:普通连接池同步,普通连接池管道 ...
- leetcode 33. 搜索旋转排序数组 及 81. 搜索旋转排序数组 II
33. 搜索旋转排序数组 问题描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定 ...
- Ubuntu下使用VS Code创建Spring Boot工程
目的 我们将在Ubuntu桌面系统下,使用VS Code(Visual Studio Code)编辑器从零开始创建一个Spring Boot工程,并实现一个简单的RESTful风格接口.使用这套流程的 ...
- 【刷题-PAT】A1135 Is It A Red-Black Tree (30 分)
1135 Is It A Red-Black Tree (30 分) There is a kind of balanced binary search tree named red-black tr ...
- 返回值ModelAndView
- Tomcat-如何在IDEA启动部署web模板
IDEA部署工程到Tomcat上运行 1,建议修改web工程对应的Tomcat运行实例名称 2,将需要部署的web工程添加到Tomcat运行实例中,添加或删除 Application context: ...
- AOP-底层原理(JDK动态代理实现)
AOP(JDK动态代理) 1,使用JDK动态代理,使用Proxy类里面的方法创建代理对象 (1)调用 newProxyInstance 方法 方法有三个参数 第一参数,类加载器 第二参数,增强方法所在 ...
- linux下怎么挂载U盘
一般来说linux系统会自动挂载u盘,如果是图形界面,你一插上u盘会自动弹出来文件夹. 如果是命令行界面 使用命令 fdisk -l (root下执行) 显示如下 一般u盘都是fat32格式 ...
- javascript 判断对像是否相等
在Javascript中相等运算包括"==","==="全等,两者不同之处,不必多数,本篇文章我们将来讲述如何判断两个对象是否相等? 你可能会认为,如果两个对象 ...