比较好的树形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. 【BZOJ】【3669】【NOI2014】魔法森林

    LCT动态维护MST LCT动态维护MST 我们可以枚举a,然后找从1到n的一条路径使得:这条路径上的b的最大值最小.这个路径肯定在MST上……所以枚举一遍所有的边,动态维护一个关于b值的MST即可. ...

  2. 宏基5750G 不能用内置无线网卡上网

    宏基5750G 不能用内置无线网卡上网 具体体现在: 1.--> 搜索不到无线网络: 2.点击“打开网络和共享中心”-->没有“管理无线网络”一项.如果正常的话应该是这样的: 3.“我的电 ...

  3. hibernate 多对多

    HibernateHibernate多对多关联映射通常别拆分成两个多对一关联映射1. 下面的HostBean.UserBean.UserHostBean,UserHostBean是两个表之间的关联表, ...

  4. Unity3d 接入 移动MM支付SDK(2.3) 全攻略

    原地址:http://blog.csdn.net/dingxiaowei2013/article/details/26842177 先将例程运行起来 下载例程(csdn积分不够上传不了,只能用百度网盘 ...

  5. java reflect 例子

    public static void main(String[] args) { Student stu1 = new Student(); stu1.setId(1); stu1.setName(& ...

  6. grunt下cssmin的配置参数

    每个目标的具体设置,需要参考该模板的文档minify目标的参数具体含义如下: expand:如果设为true,就表示下面文件名的占位符(即*号)都要扩展成具体的文件名. cwd:需要处理的文件(inp ...

  7. 黑马程序员-- .net基础加强8之委托,事件,程序集

    ---------------------- ASP.Net+Android+IOS开发..Net培训.期待与您交流! ---------------------- 一.委托 ============ ...

  8. http://www.cnblogs.com/huangcong/archive/2010/06/14/1757957.html

    http://www.cnblogs.com/huangcong/archive/2010/06/14/1757957.html http://www.cnblogs.com/langtianya/a ...

  9. [转]python -m SimpleHTTPServer

    本文转自:http://www.cnblogs.com/congbo/archive/2012/11/15/2769704.html 如果你急需一个简单的Web Server,但你又不想去下载并安装那 ...

  10. Ubuntu 装JDK

    我是按照这篇文章安装jdk的: http://www.cnblogs.com/bluestorm/archive/2012/05/10/2493592.html   先去 Oracle下载Linux下 ...