using
System;
using
System.Collections.Generic;
public
class
GFG {
static
int
mxx = 1000007;
static
int
[] subtree_xor;
static
Dictionary<
long
, List<IntPair> > store;
class
IntPair {
public
int
first;
public
int
second;
public
IntPair(
int
x,
int
y)
{
this
.first = x;
this
.second = y;
}
}
static
void
addEdge(List<List<
int
> > tree,
int
u,
int
v)
{
tree[u].Add(v);
tree[v].Add(u);
}
static
void
dfs(List<List<
int
> > tree,
int
[] val,
int
cur_node,
int
par)
{
subtree_xor[cur_node] = val[cur_node];
foreach
(
int
child
in
tree[cur_node])
{
if
(child == par)
continue
;
dfs(tree, val, child, cur_node);
subtree_xor[cur_node] ^= subtree_xor[child];
}
}
static
void
store_xor_product(List<List<
int
> > tree,
int
cur_node,
int
par,
int
tot_xor)
{
foreach
(
int
child
in
tree[cur_node])
{
if
(child == par)
continue
;
long
first_comp_xor = subtree_xor[child];
long
second_comp_xor = tot_xor ^ first_comp_xor;
long
xor_product
= first_comp_xor * second_comp_xor;
if
(!store.ContainsKey(xor_product)) {
store.Add(xor_product,
new
List<IntPair>());
}
store[xor_product].Add(
new
IntPair(cur_node, child));
store_xor_product(tree, child, cur_node,
tot_xor);
}
}
static
void
print_edges(
long
mx_product)
{
if
(store.ContainsKey(mx_product)) {
foreach
(IntPair edges
in
store[mx_product])
{
Console.WriteLine(edges.first +
" "
+ edges.second);
}
}
}
static
long
findVal(
int
N,
int
[] val,
int
[][] edges)
{
int
tot_xor = 0;
for
(
int
i = 0; i < N; i++)
tot_xor ^= val[i];
List<List<
int
> > tree =
new
List<List<
int
> >(N);
for
(
int
i = 0; i < N; i++) {
tree.Add(
new
List<
int
>());
}
for
(
int
i = 0; i < N - 1; i++)
addEdge(tree, edges[i][0], edges[i][1]);
dfs(tree, val, 0, -1);
store_xor_product(tree, 0, -1, tot_xor);
long
mx_product = -1;
foreach
(
KeyValuePair<
long
, List<IntPair> > ele
in
store)
{
long
cur_product = ele.Key;
mx_product = Math.Max(mx_product, cur_product);
}
return
mx_product;
}
static
public
void
Main()
{
int
N = 6;
int
[][] edges
= {
new
int
[] { 0, 5 },
new
int
[] { 0, 4 },
new
int
[] { 5, 1 },
new
int
[] { 5, 3 },
new
int
[] { 5, 2 } };
subtree_xor =
new
int
[mxx];
store =
new
Dictionary<
long
, List<IntPair> >();
int
[] val = { 7, 15, 9, 1, 1, 12 };
long
mx_product = findVal(N, val, edges);
Console.WriteLine(mx_product);
print_edges(mx_product);
}
}