using
System;
using
System.Collections.Generic;
public
class
SubtreeColorCount {
static
int
ans = 0;
static
int
CountRed(
int
[] color,
int
n)
{
int
red = 0;
for
(
int
i = 0; i < n; i++) {
if
(color[i] == 1) {
red++;
}
}
return
red;
}
static
int
CountBlue(
int
[] color,
int
n)
{
int
blue = 0;
for
(
int
i = 0; i < n; i++) {
if
(color[i] == 2) {
blue++;
}
}
return
blue;
}
static
void
BuildTree(
int
[][] edge,
Dictionary<
int
, List<
int
> > m,
int
n)
{
int
u, v, i;
for
(i = 0; i < n - 1; i++) {
u = edge[i][0] - 1;
v = edge[i][1] - 1;
if
(!m.ContainsKey(u))
m.Add(u,
new
List<
int
>());
if
(!m.ContainsKey(v))
m.Add(v,
new
List<
int
>());
m[u].Add(v);
m[v].Add(u);
}
}
static
void
SolutionDfs(
int
v,
int
[] color,
int
red,
int
blue,
int
[] sub_red,
int
[] sub_blue,
int
[] vis,
Dictionary<
int
, List<
int
> > adj)
{
vis[v] = 1;
foreach
(
int
i
in
adj[v])
{
if
(vis[i] == 0) {
SolutionDfs(i, color, red, blue, sub_red,
sub_blue, vis, adj);
}
}
}
public
static
void
CountSubtree(
int
[] color,
int
n,
int
[][] edge)
{
Dictionary<
int
, List<
int
> > adj
=
new
Dictionary<
int
, List<
int
> >();
int
[] sub_red =
new
int
[n + 3];
int
[] sub_blue =
new
int
[n + 3];
int
[] vis =
new
int
[n + 3];
int
red = CountRed(color, n);
int
blue = CountBlue(color, n);
BuildTree(edge, adj, n);
ans += 4;
SolutionDfs(0, color, red, blue, sub_red, sub_blue,
vis, adj);
Console.WriteLine(ans);
}
public
static
void
Main(
string
[] args)
{
int
[] color = { 1, 0, 0, 0, 2 };
int
n = 5;
int
[][] edge
= {
new
int
[] { 1, 2 },
new
int
[] { 2, 3 },
new
int
[] { 3, 4 },
new
int
[] { 4, 5 } };
CountSubtree(color, n, edge);
}
}