using
System;
using
System.Collections.Generic;
class
Program
{
static
void
Print(
int
[] parent,
int
s,
int
d)
{
while
(s != d)
{
Console.Write(d +
" <- "
);
d = parent[d];
}
Console.WriteLine(d);
}
static
void
Bfs(List<List<
int
>> adjList,
int
source,
int
n)
{
int
[] parent =
new
int
[n];
int
[] que =
new
int
[n];
Array.Fill(parent, 0);
Array.Fill(que, 0);
int
front = -1, rear = -1;
int
[] visited =
new
int
[n];
Array.Fill(visited, 0);
visited = 1;
parent = source;
que[++rear] = source;
int
k;
while
(front != rear)
{
k = que[++front];
List<
int
> list = adjList[k];
for
(
int
i = 0; i < list.Count; i++)
{
int
j = list[i];
if
(visited[j] == 0)
{
que[++rear] = j;
visited[j] = 1;
parent[j] = k;
}
}
}
for
(k = 0; k < n; k++)
Print(parent, source, k);
}
static
void
Main(
string
[] args)
{
List<List<
int
>> adjList =
new
List<List<
int
>>();
List<
int
> tmp =
new
List<
int
>(
new
int
[] { 1, 2 });
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 3 });
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 0, 5, 6 });
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 1, 4 });
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 2,3});
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 4,6 });
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 5 });
adjList.Add(tmp);
int
n = adjList.Count;
int
source = 2;
Bfs(adjList, source, n);
}
}
using
System;
using
System.Collections.Generic;
class
Program
{
static
void
Print(
int
[] parent,
int
s,
int
d)
{
while
(s != d)
{
Console.Write(d +
" <- "
);
d = parent[d];
}
Console.WriteLine(d);
}
static
void
Bfs(List<List<
int
>> adjList,
int
source,
int
n)
{
int
[] parent =
new
int
[n];
int
[] que =
new
int
[n];
Array.Fill(parent, 0);
Array.Fill(que, 0);
int
front = -1, rear = -1;
int
[] visited =
new
int
[n];
Array.Fill(visited, 0);
visited = 1;
parent = source;
que[++rear] = source;
int
k;
while
(front != rear)
{
k = que[++front];
List<
int
> list = adjList[k];
for
(
int
i = 0; i < list.Count; i++)
{
int
j = list[i];
if
(visited[j] == 0)
{
que[++rear] = j;
visited[j] = 1;
parent[j] = k;
}
}
}
for
(k = 0; k < n; k++)
Print(parent, source, k);
}
static
void
Main(
string
[] args)
{
List<List<
int
>> adjList =
new
List<List<
int
>>();
List<
int
> tmp =
new
List<
int
>(
new
int
[] { 1, 2 });
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 3 });
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 0, 5, 6 });
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 1, 4 });
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 2,3});
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 4,6 });
adjList.Add(tmp);
tmp =
new
List<
int
>(
new
int
[] { 5 });
adjList.Add(tmp);
int
n = adjList.Count;
int
source = 2;
Bfs(adjList, source, n);
}
}