Python to CPP

closed account (STD8C542)
I have a code snippet of tree in python but I can't change it to C++ so I need help here

1
2
 def max_profit(node):
    return max(-X, node.value + sum(map(max_profit, node.children)))
What does that code do?

(Assume that we know nothing about Python. Explain your algorithm in language-independent manner.)
Last edited on
1
2
3
4
5
6
int Node::max_profit() const{
    int sum = this->value;
    for (auto &child : this->children)
        sum += child->max_profit();
    return std::max(-X, sum);
}
closed account (STD8C542)
Node is structure in my code as
1
2
3
4
5
struct node 
{ 
	int key; 
	vector<node *>child; 
}; 


so the error is saying
error: no ‘int node::max_profit()’ member function declared in class ‘node’
int node::max_profit(){
Declare the missing member:
1
2
3
4
5
6
struct node 
{ 
	int key; 
	vector<node *> child;
	int max_profit() const;
};
Last edited on
closed account (STD8C542)
its giving the following error
prog.cpp:53:5: error: prototype for ‘int node::max_profit()’ does not match any in class ‘node’
int node::max_profit(){
^~~~
prog.cpp:11:6: error: candidate is: int node::max_profit() const
int max_profit() const;
^~~~~~~~~~


1st error in the max_profit() function and 2nd error in the node structure
Last edited on
closed account (STD8C542)
thanks i got it!

but the max_profit() function which @helios converted to cpp is giving the wrong answer and different answer for same input

Here's the full code of the question https://www.codechef.com/APRIL19B/problems/SUBREM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// CPP program to do level order traversal 
// of a generic tree 
#include <bits/stdc++.h> 
using namespace std; 

// Represents a node of an n-ary tree 
struct node 
{ 
	int key; 
	vector<node *>child;
	int max_profit() const;
}; 
int x;
// Utility function to create a new tree node 
node *newNode(int key) 
{ 
	node *temp = new node; 
	temp->key = key; 
	return temp; 
} 

// Prints the n-ary tree level wise 
void LevelOrderTraversal(node * root) 
{ 
	if (root==NULL) 
		return; 

	// Standard level order traversal code 
	// using queue 
	queue<node *> q; // Create a queue 
	q.push(root); // Enqueue root 
	while (!q.empty()) 
	{ 
		int n = q.size(); 

		// If this node has children 
		while (n > 0) 
		{ 
			// Dequeue an item from queue and print it 
			node * p = q.front(); 
			q.pop(); 
			cout << p->key << " "; 

			// Enqueue all children of the dequeued item 
			for (int i=0; i<p->child.size(); i++) 
				q.push(p->child[i]); 
			n--; 
		} 

		cout << endl; // Print new line between two levels 
	} 
} 

int node::max_profit() const{
    int sum = this->key;
    for (auto &child : this->child)
        sum += child->max_profit();
    return std::max(-x, sum);
}
    
int main() 
{ 
    int t;
    cin >> t;
    while(t--){
    int n;
    cin >> n >> x;
    int a[n]; //array to store values of nodes
    for(int i = 0; i < n; i++) cin >> a[i];
    n-=1; //to get n-1 edges
    int i = 1, u, v; node *root = newNode(a[0]); 
    while(n--){
	cin >> u >> v; //edges
	if(u == 1)
	    (root->child).push_back(newNode(a[i])); 

	else
	(root->child[u-2]->child).push_back(newNode(a[i])); 
    i++;
    }
	cout << "Level order traversal\n"; 
	LevelOrderTraversal(root); 
	node np;
	cout << np.max_profit();
	
 }
	return 0; 
} 
Topic archived. No new replies allowed.