human
Sub | Tree traversal to compute minimum difference.
Initial min difference is the color of node.
Traversing its children.
Not traversing the parent.
If the child is Adding positively to difference , we include it in the answer Otherwise , we leave the sub - tree and include 0 ( nothing ) in the answer.
DFS for colour difference : 1 colour - 2 colour.
Minimum colour difference is maximum answer value.
Clearing the current value to check for colour2 as well.
Interchanging the colours.
DFS for colour difference : 2 colour - 1 colour.
Checking if colour2 makes the minimum colour difference.
Nodes.
Adjacency list representation.
Edges.
Index represent the colour of that node There is no Node 0 , so we start from index 1 to N.
Printing the result"
How can the above be solved in Python?
 What is a code to sub and the other steps required?
assistant
#Here is code to sub and the other steps required.
 def dfs ( node , parent , tree , colour , answer ) :
	answer [ node ] = colour [ node ]
	for u in tree [ node ] :
		if ( u == parent ) :
			continue
		dfs ( u , node , tree , colour , answer )
		answer [ node ] += max ( answer [ u ] , 0 )
def maxDiff ( tree , colour , N ) :
	answer = [ 0 for _ in range ( N + 1 ) ]
	dfs ( 1 , 0 , tree , colour , answer )
	high = 0
	for i in range ( 1 , N + 1 ) :
		high = max ( high , answer [ i ] )
		answer [ i ] = 0
	for i in range ( 1 , N + 1 ) :
		if colour [ i ] == - 1 :
			colour [ i ] = 1
		else :
			colour [ i ] = - 1
	dfs ( 1 , 0 , tree , colour , answer )
	for i in range ( 1 , N ) :
		high = max ( high , answer [ i ] )
	return high
N = 5
tree = [ list ( ) for _ in range ( N + 1 ) ]
tree [ 1 ] . append ( 2 )
tree [ 2 ] . append ( 1 )
tree [ 1 ] . append ( 3 )
tree [ 3 ] . append ( 1 )
tree [ 2 ] . append ( 4 )
tree [ 4 ] . append ( 2 )
tree [ 3 ] . append ( 5 )
tree [ 5 ] . append ( 3 )
colour = [ 0 , 1 , 1 , - 1 , - 1 , 1 ]
print ( maxDiff ( tree , colour , N ) )