题目
Luogu:P1352 https://www.luogu.com.cn/problem/P1352
题目描述
某大学有
n
n
n 个职员,编号为
1...
n
1...n
1...n。
他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司。
现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数 r i r_{i} ri,但是呢,如果某个职员的直接上司来参加舞会了,那么这个职员就无论如何也不肯来参加舞会了。
所以,请你编程计算,邀请哪些职员可以使快乐指数最大,求最大的快乐指数。
算法分析
每个职员有参加和不参加两种选择。以校长结点为根
r
o
o
t
root
root。
1. 设
f
[
x
]
[
0
]
f[x][0]
f[x][0]表示从以
x
x
x为根的子树中邀请一部分职员参加,但是
x
x
x不参加,此时的最大快乐值。此时,
x
x
x的儿子结点(也就是
x
x
x的下属),可以参加,或者不参加。
转移方程为:(
y
y
y是
x
x
x的儿子结点)
f
[
x
]
[
0
]
=
∑
m
a
x
(
f
[
y
]
[
0
]
,
f
[
y
]
[
1
]
)
f[x][0]=∑max(f[y][0],f[y][1])
f[x][0]=∑max(f[y][0],f[y][1])
2. 设
f
[
x
]
[
1
]
f[x][1]
f[x][1]表示从以
x
x
x为根的子树中邀请一部分职员参加,并且
x
x
x也参加舞会,此时的最大快乐值。此时,
x
x
x的儿子结点,只能选择不参加。
转移方程为:(
y
y
y是
x
x
x的儿子结点)
f
[
x
]
[
1
]
=
a
[
x
]
+
∑
f
[
y
]
[
0
]
f[x][1]=a[x]+∑f[y][0]
f[x][1]=a[x]+∑f[y][0]
3. 目标就非常的明确了,即
m
a
x
(
f
[
r
o
o
t
]
[
0
]
,
f
[
r
o
o
t
]
[
1
]
)
max(f[root][0],f[root][1])
max(f[root][0],f[root][1])
具体代码见下
Code
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define rg register
using namespace std;
inline int sread()
{
int x=0,f=1;char c=getchar();
while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
return f*x;
}
const int maxn=100000;
int n,cnt;
int h[maxn];
int a[maxn];
struct node{
int next;
int to;
int val;
}edg[10000000];
void add(int u,int v)
{
++cnt;
edg[cnt].next=h[u];
edg[cnt].to=v;
h[u]=cnt;
}
int f[8000][5];
int maxx(int a,int b)
{
return a>b?a:b;
}
void dfs(int u,int fa)
{
f[u][0]=0;
f[u][1]=a[u];
for(int i=h[u];i;i=edg[i].next)
{
int v=edg[i].to;
if(v==fa) continue;
dfs(v,u);
f[u][0]+=maxx(f[v][0],f[v][1]);
f[u][1]+=f[v][0];
}
}
int ans;
int pre[1000000];
int main()
{
n=sread();
for(rg int i=1;i<=n;++i)
{
a[i]=sread();
}
int x,y;
for(rg int i=1;i<n;++i)
{
x=sread();y=sread();
pre[y]=x;
add(x,y);
add(y,x);
}
int p;
for(rg int i=1;i<=n;++i)
{
if(pre[i]==0) p=i;
}
dfs(p,0);
ans=maxx(f[p][1],f[p][0]);
printf("%d\n",ans);
return 0;
}
总结与反思
1. 二维树形DP 用第二维度表示某种状态。
2. 开数组要合理 如此题中
f
f
f 数组第二维度只有0和1 没必要开那么大。
3. DP要注意分析阶段和目标。
4. 还需要提高码力。
2021.07.31 再刷
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#define rg register
using namespace std;
typedef long long ll;
int sread()
{
int x=0,f=1;char c=getchar();
while(c>'9'||c<'0') {if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
return f*x;
}
const int maxn=6010;
int n,cnt;
int h[maxn];
struct node{
int next;
int to_boss;
int val_happy;
}tr[maxn*2];
void add(int u,int v)
{
++cnt;
tr[cnt].next=h[u];
tr[cnt].to_boss=v;
h[u]=cnt;
}
int pre[maxn];
int f[maxn][2];
void _search(int u,int fa)
{
f[u][0]=0;//u不参加
f[u][1]=tr[u].val_happy;
for(rg int i=h[u];i;i=tr[i].next)
{
int v=tr[i].to_boss;
if(v==fa) continue;
_search(v,u);
f[u][0]+=max(f[v][0],f[v][1]);
f[u][1]+=f[v][0];
}
}
int main()
{
n=sread();
for(rg int i=1;i<=n;++i) tr[i].val_happy=sread();
int x=0,y=0;
for(rg int i=1;i<=n-1;++i)
{
x=sread(); y=sread();
pre[y]=x;
add(x,y); add(y,x);
}
int p=0;
for(rg int i=1;i<=n;++i)
{
if(pre[i]==0) p=i;
}
_search(p,0);
int ans=0;
ans=max(f[p][0],f[p][1]);
cout<<ans<<endl;
return 0;
}