using
System;
using
System.Collections.Generic;
class
GFG
{
static
void
dfs(
int
node,
int
parent,
List<
int
> []tree,
int
[]colour,
int
[]answer)
{
answer[node] = colour[node];
foreach
(
int
u
in
tree[node])
{
if
(u == parent)
continue
;
dfs(u, node, tree, colour, answer);
answer[node] += Math.Max(answer[u], 0);
}
}
static
int
maxDiff(List<
int
> []tree,
int
[]colour,
int
N)
{
int
[]answer =
new
int
[N + 1];
dfs(1, 0, tree, colour, answer);
int
high = 0;
for
(
int
i = 1; i <= N; i++)
{
high = Math.Max(high, answer[i]);
answer[i] = 0;
}
for
(
int
i = 1; i <= N; i++)
{
if
(colour[i] == -1)
colour[i] = 1;
else
colour[i] = -1;
}
dfs(1, 0, tree, colour, answer);
for
(
int
i = 1; i < N; i++)
high = Math.Max(high, answer[i]);
return
high;
}
public
static
void
Main(String []args)
{
int
N = 5;
List<
int
> []tree =
new
List<
int
>[N + 1];
for
(
int
i = 0; i < N + 1; i++)
tree[i] =
new
List<
int
>();
tree[1].Add(2);
tree[2].Add(1);
tree[1].Add(3);
tree[3].Add(1);
tree[2].Add(4);
tree[4].Add(2);
tree[3].Add(5);
tree[5].Add(3);
int
[]colour = { 0, 1, 1, -1, -1, 1 };
Console.WriteLine(maxDiff(tree, colour, N));
}
}