CSES - Datatähti 2024 alku - Results
Submission details
Task:Laskettelukeskus
Sender:Verlet
Submission time:2023-11-03 13:49:26 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:57:33: error: taking address of rvalue [-fpermissive]
   57 |     nodes.push_back(&create_node(data[i]));
      |                      ~~~~~~~~~~~^~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <deque>

using namespace std;

struct Node {
  long data;
  vector<Node> children;
};

vector<Node*> nodes;

Node create_node(long data)
{
  Node node;
  node.data = data;
  node.children = vector<Node>(0);
  return node;
}

long eval(Node* root)
{
  if (root->children.size() == 0) return root->data;
  long sum;
  for (Node child : root->children)
  {
    sum += eval(&child);
  }
  return max(root->data, sum);
}

int main()
{
  int n;

  cin >> n;

  int path_start[n-1];
  int path_end[n-1];

  for (int i = 0; i < n - 1; i++)
  {
    cin >> path_start[i];
    cin >> path_end[i];
  }

  int data[n];

  for (int i = 0; i < n; i++)
  {
    cin >> data[i];
  }

  for (int i = 0; i < n; i++)
  {
    nodes.push_back(&create_node(data[i]));
  }

  for (int i = 0; i < n - 1; i++)
  {
    nodes[path_start[i]]->children.push_back(*nodes[path_end[i]]);
  }

  cout << eval(nodes[0]);
}