bzoj4042
比较好的树形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的更多相关文章
- 【BZOJ4042】【CERC2014】parades 状压DP
题目大意 给你一棵\(n\)个点的树和\(m\)条路径要求你找出最多的路径,使得这些路径不共边.特别的,每个点的度数\(\leq 10\). \(n\leq 1000,m\leq \frac{n(n- ...
- BZOJ4042 : [Cerc2014] parades
设f[x]为x子树里能选的最多的路径数,h[x]为x子树里往上走的点的集合,且不与x子树内的最优解冲突 首先f[x]=sum(f[son]) 若h[son]与x可以直接匹配,则匹配上,f[x]++ 然 ...
随机推荐
- centos nginx,php添加到Service
SHELL脚本: nginx vim /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx da ...
- SVN提交错误:working copy is not up-to-date解决方法
我在项目中删了2个jar,然后SVN提交,一直提交不成功 svn在提交时报错如下图: working copy is not up-to-date svn:commit failed(details ...
- soap消息机制 讲解
SOAP(Simple Object Access Protocol,简单对象访问协议)作为一种信息交互协议在分布式应用中非常广泛,如WebService.在使用.Net开发WebService时候, ...
- iframe标签用法详解(属性、透明、自适应高度)(总结)
<iframe src="http://www.jb51.net" width="200" height="500"> 脚本之家 ...
- 使用apt-fast 来加速你的Ubuntu 的apt
使用apt-fast 来加速你的Ubuntu 的apt sudo add-apt-repository ppa:apt-fast/stable sudo apt-get update sudo apt ...
- 用React.addons.TestUtils、Jasmine进行单元测试
一.用到的工具 1.React.addons.TestUtils 2.Jasmine 3.Browserify(处理jsx文件的require依赖关系) 4.Reactify(能和browserify ...
- VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)
原文地址: VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)-软件开发-鸡啄米 http://www.jizhuomi.com/software/139.html 上一讲中鸡 ...
- BSD历史
在UNIX问世之后(关于UNIX来历史这里不详述),UNIX拥有者AT&T公司以低廉甚至免费的许可将Unix源码授权给学术机构做研究或教学之用. 加州大学伯克利分校就是其中之一.在得到源码后, ...
- hibernate 联合主键生成机制(组合主键XML配置方式)
hibernate 联合主键生成机制(组合主键XML配置方式) 如果数据库中用多个字段而不仅仅是一个字段作为主键,也就是联合主键,这个时候就可以使用hibernate提供的联合主键生成策略. 具体 ...
- NSMutableString
/*可变字符串,注意NSMutableString是NSString子类*/ //注意虽然initWithCapacity分配字符串大小,但是不是绝对的不可以超过此范围,声明此变量对 性能有好处 NS ...