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]++ 然 ...
随机推荐
- OC面向对象封装
面向对象语言的三大特性:封装.继承.多态 封装:不暴露自己类的内部的属性,提高自己的数据的安全性:就像一个接线盒一样,内部结构看不到,只有外部的接口提供给我们使用,这样既安全又美观:在代码方面就是结构 ...
- android重写view和viewgroup的区别
重写view: View类一般用于绘图操作,重写它的onDraw方法,但它不可以包含其他组件,没有addView(View view)方法. 重写viewgroup: ViewGroup是一个组件容器 ...
- wap开发使用jquery mobile之后页面不加载外部css样式文件/js文件
场景: wap开发,使用jquery mobile之后不会加载外部自定义的css文件了,需要手动刷新才会加载,查看外部自定义的js文件也是一样. 解决办法: 1.在page下面添加css样式,就不要写 ...
- Linux "ls -l"文件列表权限详解
ls Linux "ls -l"文件列表权限详解 1.使用 ls -l 命令 执行结果如下(/var/log) : drwxr-x--- root adm -- : apache2 ...
- CentOS7 安装 PostGIS方法(适合国内网络
安装Postgresql 9.4 yum install http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-redhat94-9.4-1. ...
- JavaScript高级---适配器模式
一.设计模式 javascript里面给我们提供了很多种设计模式: 工厂.桥.组合.门面.适配器.装饰者.享元.代理.观察者.命令.责任链 在前面我们实现了工厂模式和桥模式 工厂模式 : 核心:为了生 ...
- 多线程Java Socket编程示例(转)
这篇做为学习孙卫琴<<Java网络编程精解>>的学习笔记吧.其中采用Java 5的ExecutorService来进行线程池的方式实现多线程,模拟客户端多用户向同一服务器端发送 ...
- [转载]Spring Beans Auto-Wiring
Autowiring Modes You have learnt how to declare beans using the <bean> element and inject < ...
- Chp18: Hard
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. Solut ...
- Android ListView点击失效
item中存在 ImageButton 等可以点击的组件,这会抢先获得ListView的焦点. 从而导致item点击失效