using
System;
using
System.Collections.Generic;
public
class
GFG {
static
void
DFS(
int
V,
int
parent,
ref
int
flag,
List<
int
> vis, List<List<
int
> > adj)
{
vis[V] = 1;
foreach
(
int
U
in
adj[V])
{
if
(vis[U] == 0) {
DFS(U, V,
ref
flag, vis, adj);
}
else
if
(U != parent && parent != -1) {
flag = 1;
}
}
}
static
void
FindMinMaxCC(
int
[] A,
int
N)
{
List<List<
int
> > adj =
new
List<List<
int
> >();
for
(
int
i = 0; i <= N; i++) {
adj.Add(
new
List<
int
>());
}
for
(
int
i = 0; i < N; i++) {
adj[i + 1].Add(A[i]);
adj[A[i]].Add(i + 1);
}
int
loopConnectedComponents = 0;
int
withoutLoopConnectedComponents = 0;
List<
int
> vis =
new
List<
int
>();
for
(
int
i = 0; i <= N; i++) {
vis.Add(0);
}
int
flag = 0;
for
(
int
i = 1; i <= N; i++) {
if
(vis[i] == 0) {
DFS(i, -1,
ref
flag, vis, adj);
if
(flag == 1) {
loopConnectedComponents++;
}
else
{
withoutLoopConnectedComponents++;
}
flag = 0;
}
}
int
maxConnectedComponents
= loopConnectedComponents
+ withoutLoopConnectedComponents;
int
minConnectedComponents
= loopConnectedComponents;
if
(withoutLoopConnectedComponents != 0) {
minConnectedComponents++;
}
Console.WriteLine(minConnectedComponents +
" "
+ maxConnectedComponents);
}
public
static
void
Main(
string
[] args)
{
int
[] A = { 2, 1, 4, 3, 6, 5 };
int
N = 6;
FindMinMaxCC(A, N);
int
[] A1 = { 2, 3, 1, 5, 6, 4 };
int
N1 = 6;
FindMinMaxCC(A1, N1);
}
}