#include <bits/stdc++.h>
using
namespace
std;
void
dfs(
int
src, vector<
bool
>& visited,
vector<vector<
int
> >& graph,
vector<
int
>& component,
int
N)
{
visited[src] =
true
;
component.push_back(src);
for
(
int
dest = 0; dest < N; dest++) {
if
(graph[src][dest] != INT_MAX) {
if
(!visited[dest])
dfs(dest, visited, graph,
component, N);
}
}
}
void
floydWarshall(
vector<vector<
int
> >& graph,
int
N)
{
for
(
int
k = 0; k < N; k++) {
for
(
int
i = 0; i < N; i++) {
for
(
int
j = 0; j < N; j++) {
if
(graph[i][k] != INT_MAX
&& graph[k][j] != INT_MAX) {
if
(graph[i][k] + graph[k][j]
< graph[i][j])
graph[i][j]
= graph[i][k] + graph[k][j];
}
}
}
}
}
int
maxInThisComponent(vector<
int
>& component,
vector<vector<
int
> >& graph)
{
int
maxDistance = INT_MIN;
int
n = component.size();
for
(
int
i = 0; i < n; i++) {
for
(
int
j = i + 1; j < n; j++) {
maxDistance
= max(maxDistance,
graph[component[i]][component[j]]);
}
}
return
(maxDistance == INT_MIN
? 0
: maxDistance);
}
vector<
int
> maximumShortesDistances(
vector<vector<
int
> >& graph,
int
N)
{
vector<
bool
> visited(N,
false
);
vector<vector<
int
> > components;
vector<
int
> temp;
for
(
int
i = 0; i < N; i++) {
if
(!visited[i]) {
temp.clear();
dfs(i, visited, graph, temp, N);
components.push_back(temp);
}
}
floydWarshall(graph, N);
vector<
int
> result;
int
numOfComp = components.size();
int
maxDistance;
for
(
int
i = 0; i < numOfComp; i++) {
maxDistance
= maxInThisComponent(components[i], graph);
result.push_back(maxDistance);
}
return
result;
}
int
main()
{
int
N = 8;
const
int
inf = INT_MAX;
vector<vector<
int
> > graph1 = {
{ 0, inf, 9, inf, inf, inf, 3, inf },
{ inf, 0, inf, 10, 1, 8, inf, inf },
{ 9, inf, 0, inf, inf, inf, 11, inf },
{ inf, 10, inf, 0, 5, 13, inf, inf },
{ inf, 1, inf, 5, 0, 3, inf, inf },
{ 8, inf, inf, 13, 3, 0, inf, inf },
{ 3, inf, 11, inf, inf, inf, 0, inf },
{ inf, inf, inf, inf, inf, inf, inf, 0 },
};
vector<
int
> result1
= maximumShortesDistances(graph1, N);
for
(
int
mx1 : result1)
cout << mx1 <<
' '
;
return
0;
}