记录编号 |
577606 |
评测结果 |
AAAAAAAAAA |
题目名称 |
Spirits |
最终得分 |
100 |
用户昵称 |
yrtiop |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.000 s |
提交时间 |
2022-11-14 11:49:58 |
内存使用 |
0.00 MiB |
显示代码纯文本
// Problem: E - Bichrome Tree
// Contest: AtCoder - AtCoder Regular Contest 083
// URL: https://atcoder.jp/contests/arc083/tasks/arc083_c
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int maxm = 5e3 + 5;
std::vector<int> G[maxn];
int n,f[maxn],a[maxn];
bool dp[maxm],flag;
void dfs(int u,int fa) {
for(auto& v : G[u]) {
if(v == fa)continue ;
dfs(v , u);
}
if(!flag)return ;
memset(dp , false , sizeof(dp));
int res = 0;
dp[0] = true;
for(auto& v : G[u]) {
if(v == fa)continue ;
res += f[v] + a[v];
for(int i = a[u];~ i;-- i) {
if(i >= f[v]&&i >= a[v])
dp[i] = dp[i - f[v]] | dp[i - a[v]];
else if(i >= f[v])
dp[i] = dp[i - f[v]];
else if(i >= a[v])
dp[i] = dp[i - a[v]];
else dp[i] = false;
}
}
bool cur = false;
for(int i = a[u];~ i;-- i) {
if(dp[i]) {
f[u] = res - i;
cur = true;
break ;
}
}
flag &= cur;
return ;
}
void work() {
scanf("%d",&n);
for(int i = 1;i <= n;++ i)
G[i].clear();
for(int i = 2,x;i <= n;++ i) {
scanf("%d",&x);
G[x].pb(i);
}
for(int i = 1;i <= n;++ i)
scanf("%d",&a[i]);
flag = true;
dfs(1 , 0);
if(!flag)puts("IMPOSSIBLE");
else puts("POSSIBLE");
return ;
}
int main() {
freopen("spirits.in","r",stdin);
freopen("spirits.out","w",stdout);
int T;
scanf("%d",&T);
while(T --)work();
return 0;
}