QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#401372 | #6842. Popcount Words | lifan | WA | 3ms | 19640kb | C++14 | 2.3kb | 2024-04-28 16:20:42 | 2024-04-28 16:20:42 |
Judging History
answer
//
#include <bits/stdc++.h>
using namespace std;
#define maxn 4005
#define mod (int)(1e9 + 7)
int n, m;
int fst[maxn], cnt;
int c[maxn], d[maxn], e[maxn];
struct node
{
int tar, nxt;
}arr[maxn << 1];
void adds(int x, int y)
{
arr[++cnt].tar = y, arr[cnt].nxt = fst[x], fst[x] = cnt;
}
bool vis[maxn];
void clear()
{
memset(fst, 0, sizeof(fst));
memset(vis, 0, sizeof(vis));
cnt = 0;
}
int siz[maxn], klen;
long long dp[505][maxn], g[maxn];
void dfs(int x, int last)
{
memset(dp[x], 0xf3, sizeof(dp[x]));
siz[x] = 1;
for (int i = 0; i <= m; ++i) g[i] = dp[last][i];
for (int k = 1; k <= d[x]; k <<= 1)
{
int j = k;
if(k * 2 > d[x]) j = d[x] - k + 1;
for (int i = 1ll * j * e[x]; i <= m; ++i) dp[x][i] = max(dp[x][i], g[i - j * e[x]] + j * c[x]);
for (int i = 0; i <= m; ++i) g[i] = max(dp[x][i], g[i]);
}
for (int i = fst[x]; i; i = arr[i].nxt)
{
int j = arr[i].tar;
if(vis[j] || j == last) continue;
dfs(j, x);
siz[x] += siz[j];
for (int i = 1; i <= m; ++i) dp[x][i] = max(dp[j][i], dp[x][i]);
}
}
int pos, maxsiz, nowall;
int heavy(int x, int last)
{
int nows = 1, maxx = 0;
for (int i = fst[x]; i; i = arr[i].nxt)
{
int j = arr[i].tar;
if(vis[j] || j == last) continue;
int now = heavy(j, x);
nows += now, maxx = max(maxx, now);
}
maxx = max(maxx, nowall - nows);
if(maxsiz > maxx) maxsiz = maxx, pos = x;
return nows;
}
int num[maxn];
long long calc(int x)
{
vis[x] = true;
dfs(x, 0);
long long ans;
int bj;
ans = bj = 0;
for (int i = 1; i <= m; ++i) ans = max(ans, dp[x][i]);
vector<int> sons;
for (int i = fst[x]; i; i = arr[i].nxt) if(!vis[arr[i].tar]) sons.push_back(siz[arr[i].tar]);
for (int i = fst[x]; i; i = arr[i].nxt)
{
int j = arr[i].tar;
if(vis[j]) continue;
maxsiz = INT_MAX, nowall = sons[bj++], heavy(j, 0);
ans = max(calc(pos), ans);
}
return ans;
}
int main()
{
int t;
cin >> t;
while(t--)
{
scanf("%d %d", &n, &m);
clear();
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; ++i) scanf("%d", &c[i]);
for (int i = 1; i <= n; ++i) scanf("%d", &e[i]);
for (int i = 1; i <= n; ++i) scanf("%d", &d[i]);
for (int i = 1; i < n; ++i)
{
int x, y;
scanf("%d %d", &x, &y);
adds(x, y), adds(y, x);
}
maxsiz = INT_MAX, nowall = n, heavy(1, 0);
printf("%lld\n", calc(pos));
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 19640kb
input:
3 5 2 6 1 3 4 8 0 1 11 101 0011010
output:
0 0 0
result:
wrong answer 1st lines differ - expected: '6', found: '0'