using
System;
using
System.Collections.Generic;
public
class
GFG {
public
class
Edge {
public
int
u;
public
int
v;
public
Edge(
int
u,
int
v)
{
this
.u = u;
this
.v = v;
}
}
public
static
void
ConstructPrimeTree(
int
N,
int
[][] edges)
{
List<Edge>[] graph =
new
List<Edge>[ N ];
for
(
int
i = 0; i < N; i++) {
graph[i] =
new
List<Edge>();
}
string
[] record =
new
string
[N - 1];
for
(
int
i = 0; i < edges.Length; i++) {
int
u = edges[i][0];
int
v = edges[i][1];
graph[u - 1].Add(
new
Edge(u - 1, v - 1));
graph[v - 1].Add(
new
Edge(v - 1, u - 1));
record[i] = (u - 1) +
" "
+ (v - 1);
}
int
src = 0;
for
(
int
i = 0; i < N; i++) {
if
(graph[i].Count > 2) {
Console.WriteLine(
"-1"
);
return
;
}
else
if
(graph[i].Count == 1) {
src = i;
}
}
Dictionary<
string
,
int
> vertices
=
new
Dictionary<
string
,
int
>();
int
count = 0;
int
weight = 2;
while
(count < N) {
List<Edge> ed = graph[src];
int
i = 0;
while
(i < ed.Count
&& vertices.ContainsKey(src +
" "
+ ed[i].v)) {
i++;
}
if
(i < ed.Count) {
int
nbr = ed[i].v;
vertices[src +
" "
+ nbr] = weight;
vertices[nbr +
" "
+ src] = weight;
weight = 5 - weight;
src = nbr;
}
count++;
}
for
(
int
i = 0; i < record.Length; i++) {
Console.Write(vertices[record[i]] +
" "
);
}
}
public
static
void
Main(
string
[] args)
{
int
N = 5;
int
[][] edges
= {
new
int
[] { 1, 2 },
new
int
[] { 3, 5 },
new
int
[] { 3, 1 },
new
int
[] { 4, 2 } };
ConstructPrimeTree(N, edges);
}
}