[BZOJ2427]软件安装
Problem
每个软件都要安装某些软件才能安装,而且都有体积和价值,求安装的价值最大值
Solution
对于每个环,我们可以知道必须全部一起取或者不取,因此我们先用Tarjan缩点
然后我们用一个树形DP就可以解决了
Notice
注意这颗树是如果一个节点没取,后面就都不能取了
Code
#include<cmath>
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
#define travel2(i, u) for (reg i = head2[u]; i; i = edge2[i].next)
const int INF = 1e9;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int f[105][1005];
int Time = 0, num1 = 0, num2 = 0, scc = 0, n, m;
stack<int> Stack;
struct node
{
int vet, next;
}edge[505], edge2[505];
int head[105], head2[105], dfn[105], low[105], in[105], belong[105], v[105], w[105], V[105], W[105], flag[105];
void addedge(int u, int v)
{
edge[++num1].vet = v;
edge[num1].next = head[u];
head[u] = num1;
}
void add(int u, int v)
{
in[v]++;
edge2[++num2].vet = v;
edge2[num2].next = head2[u];
head2[u] = num2;
}
void Tarjan(int u)
{
dfn[u] = low[u] = ++Time;
Stack.push(u);
flag[u] = 1;
travel(i, u)
{
int v = edge[i].vet;
if (!dfn[v])
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (flag[v]) low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
int t; scc++;
do
{
t = Stack.top();
Stack.pop();
belong[t] = scc;
flag[t] = 0;
V[scc] += v[t];
W[scc] += w[t];
}while (t != u);
}
}
void dp(int u)
{
travel2(i, u)
{
int v = edge2[i].vet;
dp(v);
per(j, m - V[u], 0)
rep(k, 0, j) f[u][j] = max(f[u][j], f[u][k] + f[v][j - k]);
}
per(j, m, V[u]) f[u][j] = f[u][j - V[u]] + W[u];
per(j, V[u] - 1, 0) f[u][j] = 0;
}
int sqz()
{
n = read(), m = read();
rep(i, 1, n) v[i] = read();
rep(i, 1, n) w[i] = read();
rep(i, 1, n) addedge(read(), i);
rep(i, 0, n)
if (!dfn[i]) Tarjan(i);
rep(u, 1, n)
travel(i, u)
{
int v = edge[i].vet;
if (belong[u] != belong[v]) add(belong[u], belong[v]);
}
rep(i, 1, scc) if (!in[i]) add(0, i);
dp(0);
printf("%d\n", f[0][m]);
return 0;
}
[BZOJ2427]软件安装的更多相关文章
- bzoj2427 软件安装! 树dp
软件安装 内存限制:128 MiB 时间限制:1000 ms 标准输入输出 题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一 些软 ...
- 【BZOJ2427】[HAOI2010]软件安装(动态规划,Tarjan)
[BZOJ2427][HAOI2010]软件安装(动态规划,Tarjan) 题面 BZOJ 洛谷 题解 看到这类题目就应该要意识到依赖关系显然是可以成环的. 注意到这样一个性质,依赖关系最多只有一个, ...
- 【BZOJ2427】[HAOI2010]软件安装 Tarjan+树形背包
[BZOJ2427][HAOI2010]软件安装 Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为 ...
- 【BZOJ-2427】软件安装 Tarjan + 树形01背包
2427: [HAOI2010]软件安装 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 960 Solved: 380[Submit][Status ...
- bzoj2427: [HAOI2010]软件安装
Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和 ...
- 【BZOJ2427】【HAOI2010】软件安装
无力吐槽…… 原题: 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最 ...
- [BZOJ2427][HAOI2010]软件安装(Tarjan+DP)
2427: [HAOI2010]软件安装 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1987 Solved: 791[Submit][Statu ...
- BZOJ2427:[HAOI2010]软件安装(树形DP,强连通分量)
Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和 ...
- BZOJ2427:[HAOI2010]软件安装——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2427 https://www.luogu.org/problemnew/show/P2515 现在 ...
随机推荐
- XML DOM 节点类型(Node Types)
节点类型 下面的表格列出了不同的 W3C 节点类型,以及它们可拥有的子元素: 节点类型 描述 子元素 Document 表示整个文档(DOM 树的根节点) Element (max. one) Pro ...
- shell脚本遍历当前目录下以数字命名的目录,并打印
#!/bin/bash single='' #定义以个位数为目录的集合double='' #定位十位数为目录的集合#按照需要可以根据实际情况再定义以百位数为目录的集合 for dir in `ls - ...
- 关于var、let、const的故事
对于一门编程语言来说,变量与常量是再正常不过的两种,JavaScript是一直解释型的弱类型语言. JavaScript中变量或者常量可以用var.let.const(后两者是ES6的新特性). 1. ...
- AIX更改用户组
1:先 修改 user: smit chuser 把id 号给改了.成功! 2:在 修改group 的时候报警告: smit chgroup : can not update the /etc/pas ...
- function(){}、var fun=function(){}和function fun(){}的区别
一.基本定义 1.函数声明:使用function声明函数,并指定函数名. function fun() { // ...... } 2.函数表达式:使用function声明函数,但未指定函数名,将匿名 ...
- centos7.3配置python2、3环境与配置各自pip
环境:CentOS-7-x86_64-Everything-1611 No.1 查看CentOS对Python的默认依赖 [root@cs ~]# ls /usr/bin/python* /usr/b ...
- Python RabbitMQ 权重设置
消费端recv设置 注:设置消费端处理完一条消息后再发另一条 channel.basic_qos(prefetch_count=1) 由于每一条机器的处理速度不同,所以我们这里就会对应,机 ...
- topcoder srm 590 div1 (max_flow_template)
problem1 link 对于每一个,找到其在目标串中的位置,判断能不能移动即可. problem2 link 如果最后的$limit$为$11=(1011)_{2}$,那么可以分别计算值为$(10 ...
- makefile中的patsubst函数有何作用?
答:这是个模式替换函数,格式为: $(patsubst <pattern>,<replacement>,<text>) 查找text中的单词,如果匹配pattern ...
- bind封装
原理:通过apply或者call方法来实现. (1)初始版本 Function.prototype.bind=function(obj,arg){ var arg=Array.prototype.sl ...