【JZOJ5081】【GDSOI2017第三轮模拟】Travel Plan 背包问题+双指针+树的dfs序
题面


100
注意到ban的只会是一个子树,所以我们把原树转化为dfs序列。
然后题目就转化为,询问一段ban的区间,之后的背包问题。
比赛的时候,我想到这里,于是就开始想区间合并,于是搞了线段树合并,遂无果,爆零。
由于ban的是一段区间,所以肯定是将前缀和后缀合并。
我们预处理出前缀背包,和后缀背包。
然后合并两个背包就可以了。
具体的合并,Two pointers。
还要卡常。
Code
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
#define ui unsigned int
#define fo(i,x,y) for(int i=x;i<=y;i++)
#define fd(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
const char* fin="plan.in";
const char* fout="plan.out";
const int maxn=1007,maxm=maxn*2,maxv=50007,maxt=2000;
int n,m,fi[maxn],la[maxm],ne[maxm],tot,mx,pre[maxn],suf[maxn],A,B;
int dfn[maxn],low[maxn],num,va[maxn],co[maxn],a[maxv],b[maxv];
ui f[maxn][maxv],g[maxn][maxv],inf;
void add_line(int a,int b){
tot++;
ne[tot]=fi[a];
la[tot]=b;
fi[a]=tot;
}
void dfs(int v,int from){
dfn[v]=++num;
for(int k=fi[v];k;k=ne[k])
if (la[k]!=from)
dfs(la[k],v);
low[v]=num;
}
void dp(ui *f,ui *g,int v){
fd(i,mx,0){
if (g[i]==inf) continue;
f[i+va[v]]=(f[i+va[v]]<g[i]+co[v]?f[i+va[v]]:g[i]+co[v]);
f[i]=(f[i]<g[i]?f[i]:g[i]);
mx=(mx>i+va[v]?mx:i+va[v]);
}
fd(i,mx-1,0) f[i]=(f[i+1]<f[i]?f[i+1]:f[i]);
}
int main(){
freopen(fin,"r",stdin);
freopen(fout,"w",stdout);
scanf("%d",&n);
fo(i,1,n-1){
int j,k;
scanf("%d%d",&j,&k);
add_line(j,k);
add_line(k,j);
}
dfs(1,0);
fo(i,1,n) scanf("%d%d",&va[dfn[i]],&co[dfn[i]]);
inf=-1;
memset(f,255,sizeof f);
memset(g,255,sizeof g);
f[0][0]=0;
g[n+1][0]=0;
mx=pre[0]=0;
fo(i,1,n) dp(f[i],f[i-1],i),pre[i]=mx;
mx=suf[n+1]=0;
fd(i,n,1) dp(g[i],g[i+1],i),suf[i]=mx;
scanf("%d",&m);
fo(i,1,m){
int x;
ui lim;
scanf("%d%ud",&x,&lim);
A=dfn[x]-1;B=low[x]+1;
int k=suf[B],ans=0;
fo(j,0,pre[A]){
if (f[A][j]==inf) continue;
while (k>=0 && (g[B][k]==inf || f[A][j]+g[B][k]>lim))
k--;
if (k<0) break;
ans=max(j+k,ans);
}
printf("%d\n",ans);
}
return 0;
}
【JZOJ5081】【GDSOI2017第三轮模拟】Travel Plan 背包问题+双指针+树的dfs序的更多相关文章
- GDSOI2017第三轮模拟4.21 总结
1 第一题看着就觉得猎奇,于是就想着打暴力就跑. 但是很严重的问题就是... \(D\)和\(B\)打反了,都不知道当时在干什么??? 原本可以拿35. 2 第二题看着就觉得套路,于是想着今天就攻这题 ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- 【BZOJ3252】攻略 DFS序+线段树(模拟费用流)
[BZOJ3252]攻略 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛> ...
- PAT1030 Travel Plan (30)---DFS
(一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- 1030 Travel Plan (30 分)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT_A1030#Travel Plan
Source: PAT A1030 Travel Plan (30 分) Description: A traveler's map gives the distances between citie ...
- PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
随机推荐
- 基于baseline、svd和stochastic gradient descent的个性化推荐系统
文章主要介绍的是koren 08年发的论文[1], 2.3部分内容(其余部分会陆续补充上来).koren论文中用到netflix 数据集, 过于大, 在普通的pc机上运行时间很长很长.考虑到写文章目 ...
- 启动zuul时候报错:The bean 'proxyRequestHelper', defined in class path resource [org/springframework/cloud/netflix/zuul
启动zuul时候报错:The bean 'proxyRequestHelper', defined in class path resource [org/springframework/cloud/ ...
- python基础-递归
1.递归调用:在一个函数调用的过程中,直接或间接又调用了自身,就是递归调用 2.递归必备的两个阶段:1.递推 2.回溯 总结:#总结递归的使用: 1. 必须有一个明确的结束条件2. 每次进入更深一层 ...
- Leetcode965. Univalued Binary Tree单值二叉树
如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树. 只有给定的树是单值二叉树时,才返回 true:否则返回 false. 示例 1: 输入:[1,1,1,1,1,null,1] 输出:tr ...
- csp-s模拟测试52平均数,序列题解
题面:https://www.cnblogs.com/Juve/articles/11602244.html 平均数: 第k个平均数不好求,我们考虑二分,转化成平均数小于x的有几个 虑把序列中的每个数 ...
- 关于mysql8授权的问题,mysql萌新小白采坑记录
记录本人第一次使用mysql时踩的坑,因为我从官网下载最新的版本8.0.15msi版本的,直接下一步下一步安装完成之后,本地访问正常,然后服务器安装访问也正常.然后本地连接服务器上的mysql时报错. ...
- MyBatis配置文件(三)--typeAliases别名
因为类的全限定名一般包括包名,显得很长,在使用过程中不是很方便,所以MyBatis中允许我们使用一种简写的方式来代替全限定名,这就是别名.这就相当于我们在玩微信的时候,有些人的昵称很长很难记,怎么办? ...
- APT甲级——A1069 The Black Hole of Numbers
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- PAT甲级——A1013 Battle Over Cities
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- httpclient遇到java.net.URISyntaxException: Illegal character in scheme name at index 0:
正准备按照大佬的解决办法处理, 看会一条回复,说url有空格 检查了一下,还真是有空格 去除url中的空格,问题解除