CSES - Datatähti 2024 alku - Results
Submission details
Task:Laskettelukeskus
Sender:Aatos
Submission time:2023-11-04 21:49:15 +0200
Language:C++ (C++20)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:74:57: error: invalid initialization of reference of type 'const std::vector<long unsigned int>&' from expression of type 'std::vector<unsigned int>'
   74 |     std::vector<Node*> nodes = create_nodes(num_slopes, times_per_slope);
      |                                                         ^~~~~~~~~~~~~~~
input/code.cpp:40:79: note: in passing argument 2 of 'std::vector<Node*> create_nodes(uint32_t, const std::vector<long unsigned int>&)'
   40 | std::vector<Node*> create_nodes(uint32_t amount, const std::vector<uint64_t>& values)
      |                                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~

Code

#include <iostream>
#include <string>
#include <vector>
#include <stdint.h>
#include <cassert>
#include <stdexcept>

class Node
{
public:
    Node(uint64_t val) : m_val(val) {}

    uint64_t get_val()
    {
        return m_val;
    }
 
    void add_child(Node* child)
    {
        assert(child);
        m_children.push_back(child);
    }

    uint64_t evalualte()
    {
        uint64_t child_sum = 0u;
        for (Node* child : m_children)
        {
            child_sum += child->evalualte();
        }
        
        return std::max(child_sum, m_val);
    }
 
private:
    uint64_t m_val;
    std::vector<Node*> m_children;
};
 
std::vector<Node*> create_nodes(uint32_t amount, const std::vector<uint64_t>& values)
{
    std::vector<Node*> output;
    for (uint32_t i = 0; i < amount; i++)
    {
        output.push_back(new Node(values.at(i)));
    }
    return output;
}
 
int main()
{
    // inputs
    uint32_t num_slopes;
    std::cin >> num_slopes;
 
    std::vector<std::vector<uint32_t> > connections(num_slopes - 1);
    for (uint32_t i = 0; i < num_slopes - 1; i++)
    {
        for (uint32_t j = 0; j < 2; j++)
        {
            uint32_t input;
            std::cin >> input;
            connections.at(i).push_back(input);
        }
    }
 
    std::vector<uint32_t> times_per_slope(num_slopes);
    for (uint32_t i = 0; i < num_slopes; i++)
    {
        std::cin >> times_per_slope.at(i);
    }
 
    // nodes
    std::vector<Node*> nodes = create_nodes(num_slopes, times_per_slope);
 
    for (uint32_t i = 0; i < connections.size(); i++)
    {
        const uint32_t from = connections[i][0];
        const uint32_t to = connections[i][1];
 
        nodes[from - 1]->add_child(nodes[to - 1]);
    }
 
    //output
    std::cout << nodes.at(0)->evalualte() << std::endl;
 
	return 0;
}