QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#625668#6538. Lonely KingCryingWA 1ms5792kbC++141.3kb2024-10-09 20:20:272024-10-09 20:20:31

Judging History

你现在查看的是最新测评结果

  • [2024-10-09 20:20:31]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5792kb
  • [2024-10-09 20:20:27]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2010,INF = 1e18;
template<typename T>void tomin(T& x,T y){x = min(x,y);}
template<typename T>void tomax(T& x,T y){x = max(x,y);}
//
int n,a[N],tag[N]; ll sz[N];
vector<int> e[N];

vector<int> sub[N];
ll dp[N][N];

void dfs1(int u){
    sz[u] = a[u]; 
    for(auto v : e[u])dfs1(v),sz[u] += sz[v];
}
void dfs2(int u){
    for(auto v : e[u])dfs2(v);

    if(tag[u]){
        dp[u][0] = dp[u][u] = 0;
        sub[u].push_back(u);
        return;
    }
    dp[u][0] = 0;
    for(auto v : e[u]){
        ll w = dp[v][0] + 1ll*a[u]*sz[v];
        for(auto x : sub[v]){
            dp[u][x] = dp[u][0] + dp[v][x];
            tomin(w,dp[v][x] + 1ll*a[u]*a[x]);
        }
        dp[u][0] += w;
        for(auto x : sub[u])dp[u][x] += dp[v][0] + 1ll*a[u]*sz[v];
        for(auto x : sub[v])sub[u].push_back(x);
    }
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    cin>>n;
    for(int i=2,p;i<=n;i++){
        cin>>p;
        e[p].push_back(i);
    }
    for(int i=1;i<=n;i++)cin>>a[i];
    if(n==1){ cout<<"0\n"; return 0; }
    for(int i=2;i<=n;i++)tag[i] = (e[i].size() == 0);
    dfs1(1);

    for(int i=1;i<=n;i++)for(int j=0;j<=n;j++)dp[i][j] = INF;
    dfs2(1);

    cout<<dp[1][0]<<endl;

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3780kb

input:

4
1 1 2
2 1 3 2

output:

10

result:

ok 1 number(s): "10"

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 5792kb

input:

50
1 2 1 1 2 1 6 3 7 5 11 11 8 10 7 8 9 7 17 2 18 4 23 8 17 21 3 19 2 4 21 18 1 26 21 36 26 24 7 7 29 27 19 29 36 11 29 42 21
15 31 15 40 15 33 2 33 15 6 50 48 33 6 43 36 19 37 28 32 47 50 8 26 50 44 50 31 32 44 22 15 46 11 33 38 22 27 43 29 8 1 21 31 28 26 39 29 39 42

output:

22838

result:

wrong answer 1st numbers differ - expected: '22728', found: '22838'