比较好的树形dp,涉及到树上路径的题目,我们往往考虑对路径分类

当我们考虑以x为根的子树,有这样几类路径

1. 起点终点都在子树内

2. 一个点延伸到子树外

对于要选择另一个点在子树外的路径,要建立在不破坏儿子子树内的路径基础上

因为破坏会破坏多条,而只能多选择一条,不合适

因此我们考虑树dp,设f[x]为路径起点终点在子树内能选择的最多路径数目,d[x]表示还没用过的另一个点在x子树外的路径的集合

我们知道,对于x的每个孩子d,d[y]中最多只会有1条路径被选中,因此我们可以用状压dp解决

 type node=record
po,next:longint;
end; var a:array[..,..] of longint;
w:array[..,..] of boolean;
e:array[..] of node;
g,f,q,b,s,p:array[..] of longint;
len,i,tt,n,m,x,y:longint; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure add(x,y:longint);
begin
inc(len);
e[len].po:=y;
e[len].next:=p[x];
p[x]:=len;
end; function check(x,y:longint):boolean;
var i,j:longint;
begin
for i:= to s[x] do
for j:= to s[y] do
if w[a[x,i],a[y,j]] then exit(true);
exit(false);
end; procedure dfs(x,fa:longint);
var st,mx,i,y,t,j,k:longint;
begin
f[x]:=;
s[x]:=;
a[x,s[x]]:=x;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if y<>fa then
dfs(y,x);
i:=e[i].next;
end;
i:=p[x];
t:=;
while i<> do
begin
y:=e[i].po;
if y<>fa then
begin
f[x]:=f[x]+f[y];
if check(x,y) then inc(f[x])
else begin
inc(t);
q[t]:=y;
end;
end;
i:=e[i].next;
end;
k:=;
for i:= to t- do
for j:=i+ to t do
if check(q[i],q[j]) then
begin
inc(k);
b[k]:=( shl (i-)) or ( shl (j-));
end;
st:=;
mx:=;
for i:= to shl t- do
begin
g[i]:=;
for j:= to k do
if b[j] and i=b[j] then
g[i]:=max(g[i],g[i xor b[j]]+);
if g[i]>mx then
begin
mx:=g[i];
st:=;
end;
if g[i]=mx then st:=st or (( shl t-) xor i);
end;
f[x]:=f[x]+mx;
for i:= to t do
if ( shl (i-)) and st> then
begin
y:=q[i];
for j:= to s[y] do
begin
inc(s[x]);
a[x,s[x]]:=a[y,j];
end;
end;
end; begin
readln(tt);
while tt> do
begin
dec(tt);
len:=;
fillchar(p,sizeof(p),);
readln(n);
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
readln(m);
fillchar(w,sizeof(w),false);
for i:= to m do
begin
readln(x,y);
w[x,y]:=true;
w[y,x]:=true;
end;
dfs(,);
writeln(f[]);
end;
end.

bzoj4042的更多相关文章

  1. 【BZOJ4042】【CERC2014】parades 状压DP

    题目大意 给你一棵\(n\)个点的树和\(m\)条路径要求你找出最多的路径,使得这些路径不共边.特别的,每个点的度数\(\leq 10\). \(n\leq 1000,m\leq \frac{n(n- ...

  2. BZOJ4042 : [Cerc2014] parades

    设f[x]为x子树里能选的最多的路径数,h[x]为x子树里往上走的点的集合,且不与x子树内的最优解冲突 首先f[x]=sum(f[son]) 若h[son]与x可以直接匹配,则匹配上,f[x]++ 然 ...

随机推荐

  1. HTML的标签-W3School读后总结

    学习前端知识有一段时间了,前两天想做个博客园的皮肤的静态页面.虽然做完了,但是有很多不如意的地方,反思一下,还是基础不够好,所以现在把html再过一遍.(这个是Xmind生成的图片)

  2. mysql之sql语句导入与导出讲解

    导出SQL:mysqldump -u root -p 数据库名 [表名1 表名2] > 输出地址其中表名可选 本机测试实例:

  3. rsync 文件校验及同步原理及rsync server配置

    参考:http://rsync.samba.org/how-rsync-works.html 我们关注的是其发送与接收校验文件的算法,这里附上原文和我老婆(^_^)的翻译: The Sender Th ...

  4. Asp.net MVC 自定义路由在IIS7以上,提示Page Not Found 解决方法

    受限确保自定义路由在开发服务器上Ok! 然后在web.config的<webserver>节点下增加如下配置就好了.   1: <system.webServer> 2: &l ...

  5. 如何有效地记录 Java SQL 日志?

    在常规项目的开发中可能最容易出问题的地方就在于对数据库的处理了,在大部分的环境下,我们对数据库的操作都是使用流行的框架,比如 Hibernate . MyBatis 等.由于各种原因,我们有时会想知道 ...

  6. How does database indexing work?

    When data is stored on disk based storage devices, it is stored as blocks of data. These blocks are ...

  7. auto_ptr的设计动机

    auto_ptr的设计动机 C++标准程序库提供的auto_ptr是一种智能型指针(smart pointer),帮助程序员防止“被异常抛出时发生资源泄露”. 函数的操作经常依以下模式进行: 1.获取 ...

  8. POJ 2568/ZOJ 1965 Decode the Tree

    题意:在树中,每次删去节点值最小的叶子结点. 每删去一个点,就给出与这相连的点的值,直到最后只剩下一个根结点,给这N-1个数,重新建立这个树. 思路: 给出的节点号按次序存入到数组a中,将未给出的数存 ...

  9. node.js 安装、图文详解

    网上的教程很多,有些模糊不清,有些版本太旧,有些是.exe安装,本文讲述windows系统下简单nodejs .msi环境配置.最新版是Current version: v0.10.26.官网下载地址 ...

  10. java基础知识回顾之javaIO类--RandomAccessFile类

    java.io 类 RandomAccessFile java.lang.Object java.io.RandomAccessFile1.该类不是IO流中的子类.2.该类既能读又能写.3.该对象内部 ...