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
i
in
graph[v])
{
if
(!visited[i]) {
depthFirst(i, graph, visited, storeChain);
}
}
}
static
void
createMap(Dictionary<
string
,
char
> um)
{
um.Add(
"000"
,
'0'
);
um.Add(
"001"
,
'1'
);
um.Add(
"010"
,
'2'
);
um.Add(
"011"
,
'3'
);
um.Add(
"100"
,
'4'
);
um.Add(
"101"
,
'5'
);
um.Add(
"110"
,
'6'
);
um.Add(
"111"
,
'7'
);
}
static
string
octal(
string
bin)
{
int
l = bin.Length;
int
t = bin.IndexOf(
'.'
);
int
len_left = t != -1 ? t : l;
for
(
int
m = 1; m <= (3 - len_left % 3) % 3; m++)
bin =
'0'
+ bin;
if
(t != -1) {
int
len_right = l - len_left - 1;
for
(
int
p = 1; p <= (3 - len_right % 3) % 3;
p++)
bin = bin +
'0'
;
}
Dictionary<
string
,
char
> bin_oct_map
=
new
Dictionary<
string
,
char
>();
createMap(bin_oct_map);
int
i = 0;
string
octal =
""
;
while
(
true
) {
octal += bin_oct_map[bin.Substring(i, 3)];
i += 3;
if
(i == bin.Length)
break
;
if
(bin[i] ==
'.'
) {
octal +=
'.'
;
i++;
}
}
return
octal;
}
static
void
octalValue(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
t = 0; t < sizeChain; t++) {
int
temp = values[storeChain[t] - 1];
chainValues[t] = temp;
}
Console.Write(
"Chain = "
);
for
(
int
j = 0; j < sizeChain; j++) {
Console.Write(chainValues[j] +
" "
);
}
Console.Write(
"\t"
);
string
s =
""
;
for
(
int
k = 0; k < sizeChain; k++) {
string
s1 = chainValues[k].ToString();
s += s1;
}
Console.WriteLine(
"Octal equivalent = "
+ octal(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, 0, 0, 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(7);
graph[7].Add(6);
octalValue(graph, V, values);
}
}