QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#87145 | #4406. 独立集问题 | Appleblue17 | 0 | 129ms | 62720kb | C++14 | 959b | 2023-03-11 18:22:25 | 2023-03-11 18:22:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const long long N=4e5+5;
long long n,a[N];
vector <long long> G[N];
long long f[N],g[N],h[N];
void dfs(long long u,long long fa){
for(long long v: G[u]){
if(v==fa) continue;
dfs(v,u);
}
long long x,y;
x=0;
for(long long v: G[u]){
if(v==fa) continue;
x+=g[v];
}
f[u]=x;
x=a[fa]-a[u],y=-(a[fa]-a[u]);
for(long long v: G[u]){
if(v==fa) continue;
x=max(x+a[v]+f[v],x+g[v]);
y=max(y-a[v]+f[v],y+g[v]);
}
h[u]=max(x,y);
x=0,y=0;
for(long long v: G[u]){
if(v==fa) continue;
y=max(y+g[v],x+h[v]);
x+=g[v];
}
g[u]=max(x,y);
// cout<<u<<": "<<f[u]<<" "<<g[u]<<" "<<h[u]<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(long long i=1;i<=n;i++) cin>>a[i];
for(long long i=2;i<=n;i++){
long long fa; cin>>fa;
G[i].push_back(fa),G[fa].push_back(i);
}
dfs(1,0);
cout<<max({f[1]+a[1],g[1],h[1]});
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 76ms
memory: 62720kb
input:
351493 -641495974 -401868912 -864400429 35718599 -579296290 767835327 149511992 -246228754 649472235 893048442 -292675192 -788090312 -578172296 -62289673 196890164 -120059197 -452848333 -216788131 -604555771 -240241020 376984486 -407661514 -107574590 -461679558 -470560314 -583391402 -991686079 76956...
output:
149164330479910
result:
wrong answer 1st numbers differ - expected: '175477002777567', found: '149164330479910'
Subtask #2:
score: 0
Wrong Answer
Test #21:
score: 0
Wrong Answer
time: 129ms
memory: 37988kb
input:
351493 915594742 688454502 456077914 208864399 625937959 102483811 538999077 529481828 400375958 387560315 83710527 83424975 330114353 684812426 68052931 28849220 295907801 535129167 967891325 124069427 644256858 757666812 558755455 178666038 177054898 795236216 848264282 669310447 328353372 3681163...
output:
94687901290051
result:
wrong answer 1st numbers differ - expected: '175766699890054', found: '94687901290051'
Subtask #3:
score: 0
Wrong Answer
Test #41:
score: 0
Wrong Answer
time: 1ms
memory: 13584kb
input:
7 247522519 398923496 -976223527 806215585 -937536479 -130552271 90576461 1 2 1 2 5 5
output:
2961953029
result:
wrong answer 1st numbers differ - expected: '3587550338', found: '2961953029'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%