【Luogu】P2515软件安装(树形DP)
这么水的题我一遍没A,而且前两次提交都只有十分。气死我了。本来我的博客拒收水题来着。
Tarjan缩点之后跑树形DP即可。
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cstdlib>
#include<queue>
#define maxn 300
#define maxm 600
using namespace std; inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} int w[maxn];
int c[maxn];
int dfn[maxn],ID;
int low[maxn];
int stack[maxn],top;
int col[maxn],cnt;
int colw[maxn],colc[maxn];
bool vis[maxn];
int f[maxn][maxm];
int indl[maxn];
int n,m; struct Pic{
struct Edge{
int next,to;
}edge[maxn*];
int head[maxn],num;
Pic(){num=;memset(head,,sizeof(head)); }
inline void add(int from,int to){
edge[++num]=(Edge){head[from],to};
head[from]=num;
}
void tarjan(int x){
vis[x]=;stack[++top]=x;
dfn[x]=low[x]=++ID;
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
if(!dfn[to]){
tarjan(to);
low[x]=min(low[x],low[to]);
}
else if(vis[to]) low[x]=min(low[x],dfn[to]);
}
if(dfn[x]==low[x]){
col[x]=++cnt;
vis[x]=; colw[cnt]=w[x]; colc[cnt]=c[x];
while(stack[top]!=x){
vis[stack[top]]=; col[stack[top]]=cnt;
colw[cnt]+=w[stack[top]]; colc[cnt]+=c[stack[top]];
top--;
}
top--;
}
}
void dfs(int x){
for(int i=m;i>=colw[x];--i) f[x][i]=colc[x];
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
dfs(to);
for(int j=m;j>=;--j)
for(int k=;j-k>=colw[x];++k) f[x][j]=max(f[x][j],f[x][j-k]+f[to][k]);
}
return;
}
}Old,New; int main(){
n=read();m=read();
for(int i=;i<=n;++i) w[i]=read();
for(int i=;i<=n;++i) c[i]=read();
for(int i=;i<=n;++i){
int x=read();
if(x) Old.add(x,i);
}
for(int i=;i<=n;++i)
if(!dfn[i]) Old.tarjan(i);
for(int i=;i<=n;++i)
for(int j=Old.head[i];j;j=Old.edge[j].next){
int to=Old.edge[j].to;
if(col[i]==col[to]) continue;
New.add(col[i],col[to]);
indl[col[to]]++;
}
for(int i=;i<=cnt;++i)
if(!indl[i]) New.add(,i);
New.dfs();
printf("%d",f[][m]);
return ;
}
【Luogu】P2515软件安装(树形DP)的更多相关文章
- BZOJ2427:[HAOI2010]软件安装(树形DP,强连通分量)
Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和 ...
- Libre 6009 「网络流 24 题」软件补丁 / Luogu 2761 软件安装问题 (最短路径,位运算)
Libre 6009 「网络流 24 题」软件补丁 / Luogu 2761 软件安装问题 (最短路径,位运算) Description T 公司发现其研制的一个软件中有 n 个错误,随即为该软件发放 ...
- [BZOJ2427][HAOI2010]软件安装(Tarjan+DP)
2427: [HAOI2010]软件安装 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1987 Solved: 791[Submit][Statu ...
- 【Luogu】P3565HOT-Hotels(树形DP)
题目链接 水了半个月之后Fd终于开始做题啦! 然后成功的发现自己什么都不会了 树形DP,既然是三个点两两距离相等那一定得有个中心点吧,枚举那个中心点,然后暴力DFS,根据乘法原理算. 乘法原理就是我一 ...
- [luogu 1270] “访问”美术馆 (树形dp)
传送门 Description 经过数月的精心准备,Peer Brelstet,一个出了名的盗画者,准备开始他的下一个行动.艺术馆的结构,每条走廊要么分叉为两条走廊,要么通向一个展览室.Peer知道每 ...
- BZOJ 4042 Luogu P4757 [CERC2014]Parades (树形DP、状压DP)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=4042 (Luogu) https://www.luogu.org/prob ...
- Luogu P1273 有线电视网(树形dp+背包)
P1273 有线电视网 题面 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部 ...
- Luogu P1273 有线电视网 树形DP
又重构了一下...当然当初的题一看就看懂了QAQ 设f[i][j]表示以i为根的子树,有j个客户的最大收益 方程:f[u][j+k]=max(f[u][j+k],f[u][j]+f[v][k]-w(u ...
- Luogu P1272 重建道路 树形DP
刚才瞅了半天自己当初写的,终于瞅出来了...QWQ 设f[i][j]表示以i为根的子树,包含j个节点所需砍掉的最小边数 那么可知f[u][1]=u的度: 方程:f[u][j]=min(f[u][j], ...
随机推荐
- Netweaver和CloudFoundry的服务器日志
Netweaver 事务码SMICM,Goto->HTTP Plug-In->Server Logs: CloudFoundry 假设我部署本地应用到CloudFoundry之后,应用的状 ...
- 11gR2 Agent 简介
目的:本文会对oracle 11gR2 集群件(Grid Infrastructure,以下简称GI) 新特性 agent进行介绍,包括 agent的功能,常见的agent介绍,以及基本的诊断方法. ...
- [视觉] 基于YoloV3的实时摄像头记牌器
基于YoloV3的实时摄像头记牌器 github:https://github.com/aoru45/cards_recognition_recorder_pytorch 最终效果 数据准备 数据获取 ...
- python3.6.2利用pyinstaller发布EXE
我的环境是Ubuntu 16.04,系统自带Python2和Python3 安装 pip3 install pyinstaller 发布exe pyinstaller -F helloworld.py ...
- EditPlus 3.7激活码注册码
EditPlus3.7激活教程以及EditPlus3.7激活码使用方法 EditPlus是一款功能齐全的文字编辑器,搭配其他的插件还可以实现很多的功能,还可以编辑和编译Java,调试程序等,主要用来打 ...
- DateTime与long互转
DateTime转long: public static long GetDateLong(object time) { DateTime epoc = TimeZone.CurrentTimeZon ...
- Python Web 架构
1. Django(全能型)2. Tornado3. BottlePython+Bottle+Sina SAE快速构建网站http://www.cnblogs.com/Xjng/p/3511983.h ...
- cocos2dx 使用XMLHttpRequest时回调status为0的问题
今天使用cocos连接http访问时,使用XMLHttpRequest在pc上反问时正常的返回了status=0,但是在android上去返回status是0,看了一下底层代码, 发现status只有 ...
- UICollectionView实现无限轮播
#import "KGNewsController.h"#import "KGNewsCell.h"#import "KGNews.h"#i ...
- VueX源码分析(2)
VueX源码分析(2) 剩余内容 /module /plugins helpers.js store.js helpers要从底部开始分析比较好.也即先从辅助函数开始再分析那4个map函数mapSta ...