import
java.util.*;
class
GFG{
static
void
add_edge(
int
u,
int
v,
Vector<Integer> graph[])
{
graph[u].add(v);
graph[v].add(u);
}
static
int
depthFirst(
int
v,
Vector<Integer> graph[],
Vector<Boolean> visited,
int
ans)
{
visited.set(v,
true
);
System.out.print(v +
" "
);
ans++;
for
(
int
i : graph[v])
{
if
(visited.get(i) ==
false
)
{
ans = depthFirst(i, graph, visited, ans);
}
}
return
ans;
}
static
void
UniqueConnectedComponent(
int
n,
Vector<Integer> graph[])
{
Vector<Boolean> visited =
new
Vector<>();
for
(
int
i =
0
; i < n +
1
; i++)
visited.add(
false
);
HashSet<Integer> result =
new
HashSet<>();
for
(
int
i =
1
; i <= n; i++)
{
if
(visited.get(i) ==
false
)
{
int
ans =
0
;
ans = depthFirst(i, graph, visited, ans);
result.add(ans);
System.out.print(
"Count = "
+
ans +
"\n"
);
}
}
System.out.print(
"Unique Counts of "
+
"connected components: "
);
System.out.print(result.size() +
"\n"
);
}
public
static
void
main(String[] args)
{
int
n =
7
;
@SuppressWarnings
(
"unchecked"
)
Vector<Integer>[] graph =
new
Vector[n+
1
];
for
(
int
i =
0
; i < graph.length; i++)
graph[i] =
new
Vector<Integer>();
add_edge(
1
,
2
, graph);
add_edge(
3
,
4
, graph);
add_edge(
3
,
5
, graph);
add_edge(
6
,
7
, graph);
UniqueConnectedComponent(n, graph);
}
}