using System;
using System.Collections.Generic;
class MainClass {
public static void ColorEdges(int ptr, List<List<Tuple<int, int>>> gra, List<int> edgeColors, bool[] isVisited) {
Queue<int> q = new Queue<int>();
int c = 0;
HashSet<int> colored = new HashSet<int>();
if (isVisited[ptr]) {
return;
}
isVisited[ptr] = true;
for (int i = 0; i < gra[ptr].Count; i++) {
if (edgeColors[gra[ptr][i].Item2] != -1) {
colored.Add(edgeColors[gra[ptr][i].Item2]);
}
}
for (int i = 0; i < gra[ptr].Count; i++) {
if (!isVisited[gra[ptr][i].Item1]) {
q.Enqueue(gra[ptr][i].Item1);
}
if (edgeColors[gra[ptr][i].Item2] == -1) {
while (colored.Contains(c)) {
c++;
}
edgeColors[gra[ptr][i].Item2] = c;
colored.Add(c);
c++;
}
}
while (q.Count != 0) {
int temp = q.Dequeue();
ColorEdges(temp, gra, edgeColors, isVisited);
}
return;
}
public static void Main() {
// Enter the Number of Vertices and the number of edges
int ver = 4;
int edge = 4;
List<List<Tuple<int, int>>> gra = new List<List<Tuple<int, int>>>(ver);
for (int i = 0; i < ver; i++) {
gra.Add(new List<Tuple<int, int>>());
}
List<int> edgeColors = new List<int>(new int[edge]);
for (int i = 0; i < edgeColors.Count; i++) {
edgeColors[i] = -1;
}
bool[] isVisited = new bool[100000];
gra[0].Add(new Tuple<int, int>(1, 0));
gra[1].Add(new Tuple<int, int>(0, 0));
gra[1].Add(new Tuple<int, int>(2, 1));
gra[2].Add(new Tuple<int, int>(1, 1));
gra[2].Add(new Tuple<int, int>(3, 2));
gra[3].Add(new Tuple<int, int>(2, 2));
gra[0].Add(new Tuple<int, int>(3, 3));
gra[3].Add(new Tuple<int, int>(0, 3));
ColorEdges(0, gra, edgeColors, isVisited);
// printing all the edge colors
for (int i = 0; i < edge; i++) {
Console.WriteLine("Edge " + (i + 1) + " is of color " + (edgeColors[i] + 1));
}
}
}