# 洛谷2515 [haoi2010]软件安装

## tarjan重构图后树形DP

Posted by yjjr's blog on October 31, 2018

3 10
5 5 6
2 3 4
0 1 1

5

# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e3+6;
int n,m,cnt,scc,tim=0,top=0,v[maxn],w[maxn],last[maxn],last2[maxn];
int sv[maxn],sw[maxn],dfn[maxn],low[maxn],bel[maxn],que[maxn],f[maxn][maxn],in[maxn];
struct edge{int to,next;}e[maxn],e2[maxn];
bool inque[maxn];
void insert(int u,int v){e[++cnt]=(edge){v,last[u]};last[u]=cnt;}
void insert2(int u,int v){in[v]=1;e2[++cnt]=(edge){v,last2[u]};last2[u]=cnt;}
void tarjan(int x){
int now=0;
low[x]=dfn[x]=++tim;que[++top]=x;inque[x]=1;
reg(x){
int v=e[i].to;
if(!dfn[v]){tarjan(v);low[x]=min(low[x],low[v]);}
else if(inque[v])low[x]=min(low[x],dfn[v]);
}
if(low[x]==dfn[x]){
scc++;
while(now!=x){
now=que[top--];inque[now]=0;
bel[now]=scc;
sv[scc]+=v[now];
sw[scc]+=w[now];
}
}
}
void rebuild(){
cnt=0;
rep(x,1,n)
reg(x){
int v=e[i].to;
if(bel[v]!=bel[x])insert2(bel[x],bel[v]);
}
}
void dp(int x){
#define reg2(x) for(int i=last2[x];i;i=e2[i].next)
reg2(x){
int v=e2[i].to;
dp(v);
dep(j,m-sw[x],0)
rep(k,0,j)f[x][j]=max(f[x][j],f[x][k]+f[v][j-k]);
}
dep(j,m,0){
if(j>=sw[x])f[x][j]=f[x][j-sw[x]]+sv[x];
else f[x][j]=0;
}
}
int main()
{