首页 > 其他分享 >Luogu P2515 [HAOI2010]软件安装

Luogu P2515 [HAOI2010]软件安装

时间:2022-10-25 14:02:50浏览次数:96  
标签:fr P2515 Luogu ca int HAOI2010 low include sum


题目链接:​​传送门​

很明显,如果图中有一个环
那么这个环上的点必须都要选
那我们一开始就直接缩点
因为每个物品有价值有重量还有有重量限制
所以是很明显的树上背包
我们不确定一开始选哪一个点
所以建一个虚点
入度为0的点像这个虚点连边
然后做树上背包
细节蛮多
数组卡着开

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
struct node {int next, to;}e[A << 1];
int head[A], num;
void add(int fr, int to) {
e[++num].next = head[fr];
e[num].to = to;
head[fr] = num;
}
int dfn[A], low[A], n, m, bl[A], sta[A], top, kn, cnt; bool vis[A];
int w[A], v[A], sum[A], val[A], d[A], in[A], f[A][A * 5];
void tarjan(int fr) {
dfn[fr] = low[fr] = ++cnt; sta[++top] = fr; vis[fr] = 1;
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (!dfn[ca]) tarjan(ca), low[fr] = min(low[fr], low[ca]);
else if (vis[ca]) low[fr] = min(low[fr], dfn[ca]);
}
if (dfn[fr] == low[fr]) {
kn++; int p;
do {
p = sta[top--]; vis[p] = 0;
sum[kn] += w[p]; val[kn] += v[p];
bl[p] = kn;
} while (p != fr);
}
}
void dfs(int fr) {
for (int i = sum[fr]; i <= m; i++) f[fr][i] = val[fr];
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to; dfs(ca);
for (int j = m - sum[fr]; j >= 0; j--)
for (int k = 0; k <= j; k++)
f[fr][j + sum[fr]] = max(f[fr][j + sum[fr]], f[fr][j + sum[fr] - k] + f[ca][k]);
}
}

int main(int argc, char const *argv[]) {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> w[i];
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= n; i++) {
cin >> d[i];
if (!d[i]) continue;
add(d[i], i);
}
for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i);
memset(head, 0, sizeof head); num = 0;
for (int i = 1; i <= n; i++) if (bl[d[i]] != bl[i] and d[i]) add(bl[d[i]], bl[i]), in[bl[i]]++;
for (int i = 1; i <= kn; i++) if (!in[i]) add(0, i);
dfs(0); cout << f[0][m] << endl;
}


标签:fr,P2515,Luogu,ca,int,HAOI2010,low,include,sum
From: https://blog.51cto.com/lyle/5794671

相关文章

  • Luogu P4421 [COCI2017-2018#1] Lozinke
    题目链接:​​传送门​​一开始直接AC自动机每个串暴力跳fail显然会T,44分#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#i......
  • Luogu P3182 [HAOI2016]放棋子
    题目链接:​​传送门​​题目说了每行有一个障碍两个障碍不在同一行也不在同一列那障碍放哪里就没关系了矩阵都不用输入或者这样理解:交换矩阵的某两行对答案是没有影响......
  • Luogu P1772 [ZJOI2006]物流运输
    题目链接:​​传送门​​很麻烦也很难想的一道题数据很小大胆yy详细解释在代码里#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<co......
  • Luogu P4149 [IOI2011]Race
    题目链接:​​传送门​​#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#include<algorithm>#include<climits>#include<......
  • Luogu P2859 [USACO06FEB]摊位预订Stall Reservations
    题目链接:​​传送门​​很明显的要贪心从左右端点考虑先排序保证单调性每次往后看有没有能接上的单调性才保证了这个往后看的复杂度于是就很好写了/***@Date:2019......
  • Luogu P3488 [POI2009]LYZ-Ice Skates
    题目链接:​​传送门​​号脚的人可以穿大小的鞋设为号脚的人的数量假设选择区间就要满足把提出来即维护一个全局最大子段和就好了#include<iostream>#include<cstdi......
  • Luogu P4105 [HEOI2014]南园满地堆轻絮
    题目链接:​​传送门​​明显的二分简单的check我的没有longlong会炸掉50分#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<comple......
  • Luogu P3313 [SDOI2014]旅行
    题目链接:​​传送门​​动态开点+树剖的模板吧。都很熟的话就挺好写的特别注意在dfs序上修改#include<iostream>#include<cstdio>#include<cstring>#include<cstdli......
  • Luogu P2412 查单词
    题目链接:​​传送门​​做完这个题感觉我是个沙雕在越做越麻烦的道路上一去不复返我真傻,真的(会有大量冗余变量)#include<iostream>#include<cstdio>#include<cstring>......
  • 【luogu ARC106E】Medals(二分)(高维前缀和)
    Medals题目链接:luoguARC106E题目大意有n个第i个人的出现规律是对于所有2aik+1~2ai(k+1)的区间,2aik+1~2aik+ai会出现,另一部分则会不见。每个时间点你可以选择一......