比较好的树形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. [搜片神器]使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)

    谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器:http://www.sosobta.com   大家可以给提点意见... 出售商业网站代码,万元起,非诚勿扰,谢谢. 联系h31h31 a ...

  2. Ubuntu下安装配置zsh和oh my zsh

    zsh优势:自动补全功能强大和很高的可配置性 1.查看当前系统装了哪些shell    cat /etc/shells 2.当前正在运行的是哪个版本的shell    echo $SHELL 3.安装 ...

  3. UML类图关系大全-转

    1.关联 双向关联: C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法. 在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描述设计模式内的类关系来 ...

  4. java文件操作(输出目录、查看磁盘符)

    问题描述:     java操作文件,所有硬盘中所有文件路径 问题解决:     (1)查看所有磁盘文件 注:     如上所示,使用接口 File.listRoots()可以返回所有磁盘文件,通过f ...

  5. JXL读取写入excel表格数据

    问题描述: 使用java的jxl包创建.写入excel表格数据 问题解决: (1)说明 (2)写入execel数据 注: 以上是写入数据需要调用的函数接口 注: 具体接口调用过程,如上所示 (3)读取 ...

  6. windows server 2008 R2 远程连接用户数修改

    设置windows server 2008 R2 远程连接用户数修改,三步搞定 1.运行(win+R)中输入tsconfig.msc 2.双击“限制每个用户只能进行一个会话”,取消这个选项负选框 3. ...

  7. GCD异步加载网络图片

    //image dispatch_queue_t network_queue; network_queue = dispatch_queue_create("com.myapp.networ ...

  8. Observer Pattern

    Motivation We can not talk about Object Oriented Programming without considering the state of the ob ...

  9. What is the difference between database table and database view?

    The database table has a physical existence in the database. A view is a virtual table, that is one ...

  10. hdu 2717 Catch That Cow(BFS,剪枝)

    题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...