QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#625865#6538. Lonely KingCryingRE 2ms19268kbC++141.3kb2024-10-09 21:29:482024-10-09 21:29:49

Judging History

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

  • [2024-10-09 21:29:49]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:19268kb
  • [2024-10-09 21:29:48]
  • 提交

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] += w;
        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: 0ms
memory: 3836kb

input:

4
1 1 2
2 1 3 2

output:

10

result:

ok 1 number(s): "10"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3900kb

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:

22728

result:

ok 1 number(s): "22728"

Test #3:

score: 0
Accepted
time: 2ms
memory: 12652kb

input:

500
1 1 2 4 3 1 7 2 8 10 8 12 1 7 11 9 14 18 1 17 9 1 16 17 6 14 17 1 26 25 26 29 6 8 7 15 32 9 27 11 34 31 35 6 25 4 35 40 12 2 39 34 21 8 48 8 49 1 39 32 30 46 10 1 45 29 2 17 31 22 30 16 59 10 63 15 71 53 28 50 46 29 59 53 5 3 5 83 48 50 39 18 24 76 6 65 28 72 81 38 54 8 35 88 89 89 18 99 9 99 76...

output:

150134230018

result:

ok 1 number(s): "150134230018"

Test #4:

score: 0
Accepted
time: 0ms
memory: 19268kb

input:

1000
1 1 2 2 4 2 7 3 8 1 9 4 4 4 4 15 3 12 12 6 21 10 20 19 14 23 3 24 26 26 25 31 2 25 28 27 22 38 29 37 16 26 40 5 7 4 25 38 18 41 47 45 14 53 45 18 18 5 10 26 24 59 31 27 58 59 20 47 58 38 29 34 67 68 42 51 44 4 79 7 45 8 73 82 36 51 38 44 77 80 70 21 15 76 40 82 60 61 17 94 46 1 46 25 83 56 57 1...

output:

291482220072

result:

ok 1 number(s): "291482220072"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3752kb

input:

20
1 1 2 3 2 6 1 5 4 7 5 7 9 11 6 7 9 12 4
78702 90933 62714 66603 17557 48708 96257 69094 34701 81307 3224 81752 2616 58113 86806 5963 73405 5059 66996 97390

output:

34002240599

result:

ok 1 number(s): "34002240599"

Test #6:

score: -100
Runtime Error

input:

200000
1 2 2 3 1 5 5 8 3 3 1 1 5 13 3 10 15 18 13 5 3 12 18 15 8 8 21 13 4 5 15 8 27 24 20 17 8 26 4 21 36 32 36 42 25 41 25 8 22 37 51 13 26 6 41 26 28 41 13 51 37 35 1 35 28 25 66 14 49 40 24 46 38 20 8 6 15 35 40 2 26 57 65 53 51 17 23 41 14 17 80 54 74 7 32 43 13 49 29 16 17 13 45 51 51 18 20 75...

output:


result: