[HAOI2010]软件安装
嘟嘟嘟
这题乍一看挺水的,似乎和选课一样,但其实不太一样,因为,他有环。
但这也并没有多难,我先说正解,然后分享一下我奇特的错误算法。
正解很好想,因为环中的点是不必须选的(没错,只有环中的点),因此用tarjan缩点,然后重新建图,跑树形dp就行了。
我的奇特想法是啥咧?我不知咋想的,认为只要和环相连的点都必须选,也就是说,整棵奇环外向树都必须选……
按照这个思路,我把联通分量分为了两种,一是树,二是奇环树。对于树,我们树形dp;对于奇环树,直接缩成一个物品。
那么实际上就变成了背包了。只不过对于树来说是一个泛化物品,因此先\(O(n ^ 3)\)枚举分配体积合并,然后再背包就行了。
这种听带劲的错误算法竟然还能得40分……
不过话说回来,这算法不仅不对,复杂度还更高……身败名裂啊。
代码两个都发!
正解:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 105;
const int maxm = 505;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m;
int w[maxn], val[maxn], d[maxn];
struct Edge
{
int nxt, to;
}e[maxn];
int head[maxn], ecnt = -1;
In void addEdge(int x, int y)
{
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
}
int dfn[maxn], low[maxn], cnt = 0;
bool in[maxn];
int st[maxn], top = 0;
int col[maxn], ccol = 0, C[maxn], V[maxn];
In void tarjan(int now)
{
dfn[now] = low[now] = ++cnt;
st[++top] = now; in[now] = 1;
for(int i = head[now], v; ~i; i = e[i].nxt)
{
if(!dfn[v = e[i].to])
{
tarjan(v);
low[now] = min(low[now], low[v]);
}
else if(in[v]) low[now] = min(low[now], dfn[v]);
}
if(low[now] == dfn[now])
{
int x; ++ccol;
do
{
x = st[top--]; in[x] = 0;
col[x] = ccol;
C[ccol] += w[x], V[ccol] += val[x];
}while(x ^ now);
}
}
int du[maxn];
Edge e2[maxn];
int head2[maxn], ecnt2 = -1;
In void addEdge2(int x, int y)
{
e2[++ecnt2] = (Edge){head2[x], y};
head2[x] = ecnt2;
}
In void buildGraph(int now)
{
for(int i = head[now]; ~i; i = e[i].nxt)
{
int u = col[now], v = col[e[i].to];
if(u ^ v) addEdge2(u, v), ++du[v];
}
}
int dp[maxn][maxm];
In void dfs(int now, int _f)
{
for(int i = C[now]; i <= m; ++i) dp[now][i] = V[now];
for(int i = head2[now], v; ~i; i = e2[i].nxt)
{
if((v = e2[i].to) == _f) continue;
dfs(v, now);
for(int j = m - C[now]; j >= 0; --j)
for(int k = 0; k <= j; ++k)
dp[now][j + C[now]] = max(dp[now][j + C[now]], dp[v][k] + dp[now][j + C[now] - k]);
}
}
int main()
{
Mem(head, -1); Mem(head2, -1);
n = read(), m = read();
for(int i = 1; i <= n; ++i) w[i] = read();
for(int i = 1; i <= n; ++i) val[i] = read();
for(int i = 1; i <= n; ++i)
{
d[i] = read();
if(d[i]) addEdge(d[i], i);
}
for(int i = 1; i <= n; ++i) if(!dfn[i]) tarjan(i);
for(int i = 1; i <= n; ++i) buildGraph(i);
for(int i = 1; i <= ccol; ++i) if(!du[i]) addEdge2(0, i);
dfs(0, 0);
write(dp[0][m]), enter;
return 0;
}
WA的: ```c++ #include
int n, m, w[maxn], val[maxn], du[maxn];
struct Edge
{
int nxt, to;
}e[maxn];
int head[maxn], ecnt = -1;
In void addEdge(int x, int y)
{
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
}
bool vis[maxn];
int dp[maxn][maxm], f[maxn][maxm], cnt1 = 0;
In void dfs1(int now, int _f)
{
vis[now] = 1;
for(int i = w[now]; i <= m; ++i) dp[now][i] = val[now];
for(int i = head[now], v; ~i; i = e[i].nxt)
{
if((v = e[i].to) == _f) continue;
dfs1(v, now);
for(int j = m - w[now]; j >= 0; --j)
for(int k = 0; k <= j; ++k)
dp[now][j + w[now]] = max(dp[now][j + w[now]], dp[v][k] + dp[now][j + w[now] - k]);
}
}
In void solve(int now)
{
dfs1(now, 0); ++cnt1;
memcpy(f[cnt1], dp[now], sizeof(dp[now]));
}
struct Node
{
int cost, sum;
}t[maxn];
int cnt2 = 0, sum = 0, cost = 0;
In void dfs2(int now)
{
vis[now] = 1;
sum += val[now], cost += w[now];
for(int i = head[now], v; ~i; i = e[i].nxt)
if(!vis[v = e[i].to]) dfs2(v);
}
int dp2[maxn][maxm];
int main()
{
//freopen("5.in", "r", stdin);
//freopen("ha.out", "w", stdout);
Mem(head, -1);
n = read(), m = read();
for(int i = 1; i <= n; ++i) w[i] = read();
for(int i = 1; i <= n; ++i) val[i] = read();
for(int i = 1; i <= n; ++i)
{
int x = read();
if(x) addEdge(x, i), ++du[i];
}
for(int i = 1; i <= n; ++i) if(!du[i]) solve(i);
for(int i = 1; i <= n; ++i)
if(!vis[i])
{
sum = cost = 0;
dfs2(i);
t[++cnt2] = (Node){cost, sum};
}
for(int i = 1; i <= cnt1; ++i) //合并泛化物品
for(int j = 0; j <= m; ++j)
{
dp2[i][j] = dp2[i - 1][j];
for(int k = 0; k <= j; ++k)
dp2[i][j] = max(dp2[i][j], dp2[i - 1][j - k] + f[i][k]);
}
for(int i = 1; i <= cnt2; ++i)
for(int j = 0; j <= m; ++j)
{
dp2[i + cnt1][j] = dp2[i + cnt1 - 1][j];
if(j - t[i].cost >= 0)
dp2[i + cnt1][j] = max(dp2[i + cnt1][j], dp2[i + cnt1 - 1][j - t[i].cost] + t[i].sum);
}
write(dp2[cnt1 + cnt2][m]), enter;
return 0;
}