using
System;
using
System.Collections.Generic;
class
GFG
{
static
void
depthFirst(
int
v, List<List<
int
>> graph,
bool
[] visited, List<
int
> storeChain)
{
visited[v] =
true
;
storeChain.Add(v);
foreach
(
int
t
in
graph[v])
{
if
(!visited[t])
{
depthFirst(t, graph, visited, storeChain);
}
}
}
static
void
createMap(Dictionary<
string
,
char
> um)
{
um.Add(
"0000"
,
'0'
);
um.Add(
"0001"
,
'1'
);
um.Add(
"0010"
,
'2'
);
um.Add(
"0011"
,
'3'
);
um.Add(
"0100"
,
'4'
);
um.Add(
"0101"
,
'5'
);
um.Add(
"0110"
,
'6'
);
um.Add(
"0111"
,
'7'
);
um.Add(
"1000"
,
'8'
);
um.Add(
"1001"
,
'9'
);
um.Add(
"1010"
,
'A'
);
um.Add(
"1011"
,
'B'
);
um.Add(
"1100"
,
'C'
);
um.Add(
"1101"
,
'D'
);
um.Add(
"1110"
,
'E'
);
um.Add(
"1111"
,
'F'
);
}
static
string
hexaDecimal(
string
bin)
{
int
l = bin.Length;
int
t = bin.IndexOf(
'.'
);
int
len_left = t != -1 ? t : l;
for
(
int
j= 1; j<= (4 - len_left % 4) % 4; j++)
{
bin =
"0"
+ bin;
}
if
(t != -1)
{
int
len_right = l - len_left - 1;
for
(
int
k = 1; k <= (4 - len_right % 4) % 4; k++)
{
bin = bin +
"0"
;
}
}
Dictionary<
string
,
char
> bin_hex_map =
new
Dictionary<
string
,
char
>();
createMap(bin_hex_map);
int
i = 0;
string
hex =
""
;
while
(
true
)
{
hex += bin_hex_map[bin.Substring(i, 4)];
i += 4;
if
(i == bin.Length)
{
break
;
}
if
(bin[i] ==
'.'
)
{
hex +=
'.'
;
i++;
}
}
return
hex;
}
static
void
hexValue(List<List<
int
>> graph,
int
vertices, List<
int
> values)
{
bool
[] visited =
new
bool
[1001];
for
(
int
i = 1; i <= vertices; i++)
{
if
(!visited[i])
{
int
sizeChain;
List<
int
> storeChain =
new
List<
int
>();
depthFirst(i, graph, visited, storeChain);
sizeChain = storeChain.Count;
int
[] chainValues =
new
int
[sizeChain + 1];
for
(
int
j = 0; j < sizeChain; j++)
{
int
temp = values[storeChain[j] - 1];
chainValues[j] = temp;
}
Console.Write(
"Chain = "
);
for
(
int
j = 0; j < sizeChain; j++)
{
Console.Write(chainValues[j] +
" "
);
}
Console.WriteLine();
Console.Write(
"\t"
);
string
s =
""
;
for
(
int
j = 0; j < sizeChain; j++)
{
string
s1 = chainValues[j].ToString();
s += s1;
}
Console.WriteLine(
"Hexadecimal equivalent = "
+ hexaDecimal(s));
}
}
}
static
void
Main(
string
[] args)
{
List<List<
int
>> graph =
new
List<List<
int
>>();
for
(
int
i = 0; i < 1001; i++)
graph.Add(
new
List<
int
>());
int
E = 4, V = 7;
List<
int
> values =
new
List<
int
>() { 0, 1, 1, 1, 0, 1, 1 };
graph[1].Add(2);
graph[2].Add(1);
graph[3].Add(4);
graph[4].Add(3);
graph[4].Add(5);
graph[5].Add(4);
graph[6].Add(5);
graph[5].Add(6);
graph[6].Add(7);
graph[7].Add(6);
hexValue(graph, V, values);
}
}