4851 -- 【模拟试题】攻略
Description
众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏。
今天他得到了一款新游戏《XX半岛》,这款游戏有n个场景(scene),某些场景可以通过不同的选择支到达其他场景。所有场景和选择支构成树状结构:开始游戏时在根节点(共通线),叶子节点为结局。每个场景有一个价值,现在桂马开启攻略之神模式,同时攻略k次该游戏,问他观赏到的场景的价值和最大是多少(同一场景观看多次是不能重复得到价值的)
“为什么你还没玩就知道每个场景的价值呢?”
“我已经看到结局了。”
Input
第一行两个正整数n,k
第二行n个正整数,表示每个场景的价值
以下n-1行,每行2个整数a,b,表示a场景有个选择支通向b场景(即a是b的父亲)
保证场景1为根节点
Output
输出一个整数表示答案
Sample Input
5 2
4 3 2 1 1
1 2
1 5
2 3
2 4
Sample Output
10
Hint
【数据范围与约定】
对于100%的数据,n<=200000,1<=场景价值<=2^31-1
首先这个题贪心是显然的,画几个图就知道了..
对于贪心,记录sum[i]为i节点到跟的点权和,每次都挑sum最大的累加即可。
那么问题来了:
选择一个节点,对于其他节点的sum有影响。
我们再进一步,选择一个节点,影响的是其子树上的节点的sum值。
我们把思路理一下,贪心选择最大sum,选择一个节点对其子树的sum有修改值a[i],不难看出,这是一个区间修改,单点询问的问题。
我们自然想到线段树。但是怎么得到一个节点的子树,并且把它们放到线段树上呢?
方法也是很简单的:DFS序,根据DFS序的性质,一个点出现两次,分别在其进入其子树、走出其子树时。
那么得到思路:求DFS序,在DFS序上用线段树维护sum值,对于一个节点子树的修改,只需要线段树区间修改:Add(l[i],r[i],balabala)。
其实这个sum可以直接记为maxx,顺便把最大值一起求了,很方便。
总结一下:
1.贪心显而易见。
2.DFS序线段树是第一次写,考试的时候写不怎么来,就直接上了个大暴力,这种写法的用处比较广泛,好好学习。
3.线段树的基本操作一定要熟练,力争在10分钟内写出来。
4.要敢去写,不然连机会都没有。
贴上代码,这次是个教训,好好反思。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
using namespace std;
typedef long long ll;
struct node
{
ll to,next;
}w[400005];
struct seg
{
ll l,r,maxx,lazy,idx;
}tree[400005]={0};
ll sum[400005]={0};
ll h[400005]={0},cnt=0,n,k,a[400005]={0},ans=0,fa[400005]={0};
ll low[400005]={0},high[400005]={0},f[400005]={0},_cnt=0;
bool vis[400005]={0};
void Addarc(ll x,ll y)
{
cnt++;w[cnt].to=y;w[cnt].next=h[x];h[x]=cnt;
}
void init()
{
scanf("%lld%lld",&n,&k);
for(ll i=1;i<=n;i++)scanf("%lld",a+i);
for(ll i=1;i<n;i++)
{
ll a,b;
scanf("%lld%lld",&a,&b);
Addarc(a,b);
fa[b]=a;
}
sum[1]=a[1];
}
void DFS(ll x)
{
_cnt++;
low[x]=_cnt;
f[_cnt]=x;
for(ll i=h[x];i;i=w[i].next)
{
ll to=w[i].to;
sum[to]=sum[x]+a[to];
DFS(to);
}
high[x]=_cnt;
}
void Pushdown(ll root)
{
if(tree[root].lazy==0)return;
ll l=L(root),r=R(root);
tree[l].maxx+=tree[root].lazy;
tree[l].lazy+=tree[root].lazy;
tree[r].maxx+=tree[root].lazy;
tree[r].lazy+=tree[root].lazy;
tree[root].lazy=0;
}
void Pushup(ll root)
{
ll l=L(root),r=R(root);
tree[root].maxx=0;
if(tree[l].maxx>tree[root].maxx)
{
tree[root].maxx=tree[l].maxx;
tree[root].idx=tree[l].idx;
}
if(tree[r].maxx>tree[root].maxx)
{
tree[root].maxx=tree[r].maxx;
tree[root].idx=tree[r].idx;
}
return;
}
void Build(ll l,ll r,ll root)
{
tree[root].l=l;tree[root].r=r;tree[root].maxx=0;tree[root].lazy=0;
if(l==r)
{
tree[root].idx=l;
tree[root].maxx=sum[f[l]];
return;
}
ll mid=(l+r)>>1;
Build(l,mid,L(root));
Build(mid+1,r,R(root));
Pushup(root);
}
void Add(ll root,ll l,ll r,ll data)
{
if(r<tree[root].l||tree[root].r<l)return;
if(l<=tree[root].l&&r>=tree[root].r)
{
tree[root].maxx+=data;
tree[root].lazy+=data;
return;
}
Pushdown(root);
Add(L(root),l,r,data);
Add(R(root),l,r,data);
Pushup(root);
}
void work()
{
DFS(1);
Build(1,n,1);
while(k--)
{
ans+=tree[1].maxx;
ll pos=f[tree[1].idx];
while(1)
{
if(pos==0||vis[pos])break;
vis[pos]=1;
Add(1,low[pos],high[pos],-a[pos]);
pos=fa[pos];
}
}
printf("%lld\n",ans);
}
int main()
{
init();
work();
return 0;
}