CSES - Aalto Competitive Programming 2024 - wk6 - Mon - Results
Submission details
Task:Tree game
Sender:louaha1
Submission time:2024-10-07 17:46:07 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#20.00 sdetails
#30.00 sdetails
#40.00 sdetails
#50.01 sdetails
#60.00 sdetails
#70.00 sdetails
#80.00 sdetails
#90.00 sdetails
#100.00 sdetails
#110.00 sdetails
#120.00 sdetails
#130.00 sdetails
#140.00 sdetails
#150.00 sdetails
#160.00 sdetails
#170.00 sdetails
#180.00 sdetails
#190.00 sdetails
#200.00 sdetails
#210.00 sdetails
#220.00 sdetails
#230.00 sdetails
#240.00 sdetails
#250.00 sdetails
#260.00 sdetails
#270.00 sdetails
#280.00 sdetails
#290.00 sdetails
#300.00 sdetails
#310.00 sdetails
#320.00 sdetails
#330.00 sdetails
#340.00 sdetails
#350.00 sdetails
#360.00 sdetails
#370.00 sdetails
#380.00 sdetails
#390.00 sdetails
#400.00 sdetails
#410.00 sdetails
#420.00 sdetails
#430.00 sdetails
#440.00 sdetails
#450.00 sdetails
#460.00 sdetails
#470.00 sdetails
#480.00 sdetails
#490.00 sdetails
#500.01 sdetails
#510.00 sdetails
#520.00 sdetails
#530.00 sdetails
#540.00 sdetails
#550.00 sdetails
#560.00 sdetails
#570.00 sdetails
#580.00 sdetails
#590.00 sdetails
#600.09 sdetails
#610.09 sdetails
#620.09 sdetails
#630.09 sdetails
#640.09 sdetails
#650.09 sdetails
#660.09 sdetails
#670.09 sdetails
#680.09 sdetails
#690.09 sdetails

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

vector<vector<int>> tree;
vector<int> points;

// DFS function to traverse the tree
int dfs(int node) {
    vector<int> subtree_scores;

    // Traverse all the children of the current node
    for (int child : tree[node]) {
        subtree_scores.push_back(dfs(child));
    }

    // If it's a leaf node, return the points of this node
    if (subtree_scores.empty()) {
        return points[node];
    }

    // Maija cuts all but the largest subtree, Uolevi takes the best one
    int max_subtree_score = *max_element(subtree_scores.begin(), subtree_scores.end());

    // Return current node points + the largest possible subtree score
    return points[node] + max_subtree_score;
}

int main() {
    int n;
    cin >> n; // Number of nodes

    points.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> points[i]; // Points for each node
    }

    vector<int> parent(n);
    tree.resize(n);

    for (int i = 1; i < n; i++) {
        cin >> parent[i]; // Parent of each node
        parent[i]--;      // Convert to 0-indexed
        tree[parent[i]].push_back(i); // Build the tree as an adjacency list
    }

    // Start DFS from the root (node 0)
    int result = dfs(0);
    cout << result << endl;

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
1


correct output
8

user output
8

Test 2

Verdict:

input
2
8 8 
1 1 

correct output
8

user output
16

Test 3

Verdict:

input
3
3 1 6 
1 3 1 

correct output
3

user output
4

Test 4

Verdict:

input
3
5 8 4 
1 1 2 

correct output
5

user output
13

Test 5

Verdict:

input
3
8 8 3 
1 1 1 

correct output
11

user output
16

Test 6

Verdict:

input
4
9 8 1 6 
1 1 2 2 

correct output
9

user output
23

Test 7

Verdict:

input
4
10 1 4 3 
1 1 1 2 

correct output
11

user output
14

Test 8

Verdict:

input
4
9 10 2 6 
1 4 1 1 

correct output
11

user output
19

Test 9

Verdict:

input
4
7 4 9 2 
1 1 1 3 

correct output
11

user output
16

Test 10

Verdict:

input
5
9 5 7 7 4 
1 1 4 2 4 

correct output
9

user output
18

Test 11

Verdict:

input
5
10 2 3 1 4 
1 1 1 1 3 

correct output
12

user output
14

Test 12

Verdict:

input
5
5 5 4 4 2 
1 4 4 1 2 

correct output
5

user output
10

Test 13

Verdict:

input
5
6 9 5 9 1 
1 1 1 3 1 

correct output
11

user output
15

Test 14

Verdict:

input
5
7 7 6 3 2 
1 1 2 3 3 

correct output
7

user output
17

Test 15

Verdict:

input
5
10 5 1 7 4 
1 1 5 3 1 

correct output
14

user output
22

Test 16

Verdict:

input
5
4 2 10 6 1 
1 1 2 1 1 

correct output
6

user output
14

Test 17

Verdict:

input
5
5 10 4 6 3 
1 1 2 1 2 

correct output
11

user output
21

Test 18

Verdict:

input
5
9 3 5 1 7 
1 5 5 3 1 

correct output
9

user output
12

Test 19

Verdict:

input
5
4 2 1 3 9 
1 1 1 1 1 

correct output
7

user output
13

Test 20

Verdict:

input
10
9 1 10 3 4 5 8 9 6 5 
1 4 2 1 7 4 8 6 7 8 

correct output
9

user output
32

Test 21

Verdict:

input
10
4 7 4 10 6 9 5 4 7 6 
1 1 1 2 1 1 8 2 3 3 

correct output
14

user output
18

Test 22

Verdict:

input
10
7 2 3 5 3 7 7 9 6 7 
1 8 2 7 8 7 1 1 6 7 

correct output
18

user output
30

Test 23

Verdict:

input
10
3 3 1 1 5 7 1 2 5 5 
1 5 7 2 1 5 8 5 1 1 

correct output
8

user output
16

Test 24

Verdict:

input
10
1 7 3 10 5 7 8 1 2 6 
1 1 1 2 9 2 9 3 2 9 

correct output
4

user output
16

Test 25

Verdict:

input
10
6 5 3 10 2 9 1 7 8 9 
1 8 5 1 1 10 2 5 8 2 

correct output
11

user output
28

Test 26

Verdict:

input
10
5 6 4 5 7 9 5 8 8 4 
1 1 4 2 6 1 6 5 1 1 

correct output
13

user output
26

Test 27

Verdict:

input
10
1 5 3 1 5 6 7 9 9 4 
1 5 10 9 1 9 9 4 5 5 

correct output
1

user output
7

Test 28

Verdict:

input
10
5 5 6 9 5 10 6 2 6 8 
1 1 4 9 9 1 2 1 6 8 

correct output
10

user output
29

Test 29

Verdict:

input
10
3 2 1 4 4 5 2 4 9 4 
1 5 1 2 1 1 3 1 1 9 

correct output
8

user output
14

Test 30

Verdict:

input
100
21461600 42077569 474283491 30...

correct output
966865392

user output
1501973387

Test 31

Verdict:

input
100
578129114 177547714 700400588 ...

correct output
1855515684

user output
1797841360

Test 32

Verdict:

input
100
994871996 791076134 5953277 40...

correct output
1814515836

user output
-1911319052

Test 33

Verdict:

input
100
222228197 71907871 993998009 7...

correct output
1135951805

user output
1878624402

Test 34

Verdict:

input
100
579900832 174882180 138713252 ...

correct output
716202927

user output
-1586863893

Test 35

Verdict:

input
100
189754281 378059061 342381828 ...

correct output
1551273774

user output
-1981060421

Test 36

Verdict:

input
100
915344 434975452 466660908 159...

correct output
907612293

user output
697960592

Test 37

Verdict:

input
100
857806942 521650866 432061350 ...

correct output
2316691892

user output
-1313333565

Test 38

Verdict:

input
100
126184681 630426411 840357157 ...

correct output
126184681

user output
1005510065

Test 39

Verdict:

input
100
877496128 782849725 556141400 ...

correct output
2248009755

user output
-1554825841

Test 40

Verdict:

input
200
903167338 160468896 971358120 ...

correct output
2615788195

user output
-1695616670

Test 41

Verdict:

input
200
139339061 77281799 316066077 3...

correct output
729411647

user output
1146500600

Test 42

Verdict:

input
200
19617302 94019529 17298266 328...

correct output
983806343

user output
1984318345

Test 43

Verdict:

input
200
578170987 336960706 697473976 ...

correct output
1901938137

user output
-1857744023

Test 44

Verdict:

input
200
956690801 79083637 339599313 1...

correct output
1857249559

user output
1761652206

Test 45

Verdict:

input
200
185716349 143762158 115416001 ...

correct output
1057534782

user output
1796314761

Test 46

Verdict:

input
200
275288896 20815354 254338864 6...

correct output
1267445164

user output
1950220999

Test 47

Verdict:

input
200
24900465 555962124 563413635 3...

correct output
747057370

user output
1544523071

Test 48

Verdict:

input
200
513524772 254574588 366558027 ...

correct output
2272884295

user output
-1996571023

Test 49

Verdict:

input
200
490774594 733870697 844510835 ...

correct output
1650288517

user output
-1898378388

Test 50

Verdict:

input
1000
861884121 322555995 898039039 ...

correct output
2972059986

user output
-1424356884

Test 51

Verdict:

input
1000
659913933 513100418 571680180 ...

correct output
659913933

user output
-1675035340

Test 52

Verdict:

input
1000
209874718 348676066 464074417 ...

correct output
899398937

user output
1299194155

Test 53

Verdict:

input
1000
645792798 158522181 659799184 ...

correct output
2154979899

user output
1389475310

Test 54

Verdict:

input
1000
859348061 421675209 599301462 ...

correct output
2387245650

user output
1832854582

Test 55

Verdict:

input
1000
226879800 528482225 208763773 ...

correct output
1424568237

user output
1796806793

Test 56

Verdict:

input
1000
233113004 975537019 524093912 ...

correct output
1182124106

user output
1208650023

Test 57

Verdict:

input
1000
874049734 75947226 679836952 6...

correct output
2874667578

user output
1799943719

Test 58

Verdict:

input
1000
136403860 348337953 506386641 ...

correct output
136403860

user output
1081447595

Test 59

Verdict:

input
1000
824089343 410028787 814008876 ...

correct output
1778275291

user output
2097535027

Test 60

Verdict:

input
100000
106979616 574727912 291579092 ...

correct output
3411435693

user output
1401133087

Test 61

Verdict:

input
100000
834972396 341945130 512367317 ...

correct output
1494272515

user output
-2061517043

Test 62

Verdict:

input
100000
964109644 356056141 521072685 ...

correct output
1283227450

user output
-1445867861

Test 63

Verdict:

input
100000
779580014 612148564 793692717 ...

correct output
4717954657

user output
-1531143638

Test 64

Verdict:

input
100000
124576659 286156851 307400435 ...

correct output
124576659

user output
776087884

Test 65

Verdict:

input
100000
452020023 593221033 326082759 ...

correct output
1178920108

user output
-2003037040

Test 66

Verdict:

input
100000
378587281 1836202 116838722 72...

correct output
587215770

user output
-2138880759

Test 67

Verdict:

input
100000
877997603 741843452 187099247 ...

correct output
1018714881

user output
-1338222998

Test 68

Verdict:

input
100000
280774790 48087557 668312713 4...

correct output
3930265099

user output
-2044279142

Test 69

Verdict:

input
100000
866596482 430317267 394041807 ...

correct output
1639137122

user output
1712098652