using
System;
using
System.Collections.Generic;
public
class
GFG
{
static
void
dfs(
int
s, List<List<
int
>> g,
int
p,
int
d,
ref
int
ans,
ref
List<
int
> count) {
foreach
(
int
i
in
g[s])
{
if
(i != p) {
ans += d;
dfs(i, g, s, d + 1,
ref
ans,
ref
count);
count[s] = count[s] + count[i];
}
}
}
static
void
dfs2(
int
s, List<List<
int
>> g,
int
p,
ref
List<
int
> pd_all,
int
n,
List<
int
> count) {
foreach
(
int
i
in
g[s]) {
if
(i != p) {
pd_all[i] = pd_all[s] - count[i] + n - count[i];
dfs2(i, g, s,
ref
pd_all, n, count);
}
}
}
static
void
maxPotentialDistance(
int
N,
List<List<
int
>> edges) {
int
ans = 0;
List<List<
int
>> g =
new
List<List<
int
>>();
for
(
int
i = 0; i < N; i++) {
g.Add(
new
List<
int
>());
}
foreach
(List<
int
> it
in
edges) {
g[it[0]].Add(it[1]);
g[it[1]].Add(it[0]);
}
List<
int
> count =
new
List<
int
>();
for
(
int
i = 0; i < N; i++) {
count.Add(1);
}
int
pd_0 = 0;
dfs(0, g, -1, 1,
ref
pd_0,
ref
count);
List<
int
> pd_all =
new
List<
int
>();
for
(
int
i = 0; i < N; i++) {
pd_all.Add(0);
}
pd_all[0] = pd_0;
dfs2(0, g, -1,
ref
pd_all, N, count);
foreach
(
int
i
in
pd_all) {
ans = Math.Max(ans, i);
}
Console.WriteLine(ans);
}
static
void
Main(
string
[] args) {
int
N = 6;
List<List<
int
>> edges =
new
List<List<
int
>>() {
new
List<
int
>() {0, 1},
new
List<
int
>() {0, 2},
new
List<
int
>() {2, 3},
new
List<
int
>() {2, 4},
new
List<
int
>() {2, 5},
};
maxPotentialDistance(N, edges);
}}