Language: C++

import Control.Monad (replicateM)
import Data.Set hiding (foldl, map, filter)
import qualified Data.Set as Set
import Data.Maybe

addPair :: [Set Int] -> (Int, Int) -> [Set Int]
addPair ls (x, y) = case (searchLinkedSets x y ls) of
    (Nothing, Nothing, r) -> (fromList [x, y]):r
    (Just(xs), Nothing, r) -> (insert y xs):r
    (Nothing, Just(ys), r) -> (insert x ys):r
    (Just(xs), Just(ys), r) -> (union xs ys):r

searchLinkedSets :: Int -> Int -> [Set Int] -> (Maybe(Set Int), Maybe(Set Int), [Set Int])
searchLinkedSets x y ls = foldl (search' x y) (Nothing, Nothing, []) ls
    where
        search' x y (Nothing, Nothing, ls) s | (member x s) && (member y s) = (Just s, Just s, ls)
        search' x y (Nothing, ys, ls) s | member x s = (Just s, ys, ls)
        search' x y (xs, Nothing, ls) s | member y s = (xs, Just s, ls)
        search' _ _ (xs, ys, ls) s             = (xs, ys, s:ls)

linkedSets :: [(Int, Int)] -> [Set Int]
linkedSets = foldl addPair []

equals :: [Int] -> [(Int, Int)] -> Int
equals l pairs = noNeedToMove + (sum $ map countIn linkedSets')
    where
        linkedSets' = linkedSets pairs
        countIn s = length $ filter ismember pickuped
            where
                ismember x = member x s
                at i = l !! (i - 1)
                pickuped = map at $ toList s
        linkedElements = unions linkedSets'
        notLinked x = notMember x linkedElements
        noNeedToMove = length $ filter notLinked $ map (\(_, x)->x) $ filter (\(i, x) -> i == x) $ zip [1..] l

main = do
    [n, m] <- (map read . words) <$> getLine
    l <- (map read . words) <$> getLine
    pairs <- map (map read . words) <$> replicateM m getLine --  :: IO [[Int]]
    print $ equals l $ map (\[x,y]->(x,y)) pairs


Compiler output:
a.cc:14:40: warning: missing terminating ' character
   14 | searchLinkedSets x y ls = foldl (search' x y) (Nothing, Nothing, []) ls
      |                                        ^
a.cc:14:40: error: missing terminating ' character
   14 | searchLinkedSets x y ls = foldl (search' x y) (Nothing, Nothing, []) ls
      |                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:16:15: warning: missing terminating ' character
   16 |         search' x y (Nothing, Nothing, ls) s | (member x s) && (member y s) = (Just s, Just s, ls)
      |               ^
a.cc:16:15: error: missing terminating ' character
   16 |         search' x y (Nothing, Nothing, ls) s | (member x s) && (member y s) = (Just s, Just s, ls)
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:17:15: warning: missing terminating ' character
   17 |         search' x y (Nothing, ys, ls) s | member x s = (Just s, ys, ls)
      |               ^
a.cc:17:15: error: missing terminating ' character
   17 |         search' x y (Nothing, ys, ls) s | member x s = (Just s, ys, ls)
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:18:15: warning: missing terminating ' character
   18 |         search' x y (xs, Nothing, ls) s | member y s = (xs, Just s, ls)
      |               ^
a.cc:18:15: error: missing terminating ' character
   18 |         search' x y (xs, Nothing, ls) s | member y s = (xs, Just s, ls)
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:19:15: warning: missing terminating ' character
   19 |         search' _ _ (xs, ys, ls) s             = (xs, ys, s:ls)
      |               ^
a.cc:19:15: error: missing terminating ' character
   19 |         search' _ _ (xs, ys, ls) s             = (xs, ys, s:ls)
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:25:62: warning: missing terminating ' character
   25 | equals l pairs = noNeedToMove + (sum $ map countIn linkedSets')
      |                                                              ^
a.cc:25:62: error: missing terminating ' character
   25 | equals l pairs = noNeedToMove + (sum $ map countIn linkedSets')
      |                                                              ^~
a.cc:27:19: warning: missing terminating ' character
   27 |         linkedSets' = linkedSets pairs
      |                   ^
a.cc:27:19: error: missing terminating ' character
   27 |         linkedSets' = linkedSets pairs
      |                   ^~~~~~~~~~~~~~~~~~~~
a.cc:33:43: warning: missing terminating ' character
   33 |         linkedElements = unions linkedSets'
      |                                           ^
a.cc:33:43: error: missing terminating ' character
a.cc:35:57: error: stray '\' in program
   35 |         noNeedToMove = length $ filter notLinked $ map (\(_, x)->x) $ filter (\(i, x) -> i == x) $ zip [1..] l
      |                                                         ^
a.cc:35:79: error: stray '\' in program
   35 |         noNeedToMove = length $ filter notLinked $ map (\(_, x)->x) $ filter (\(i, x) -> i == x) $ zip [1..] l
      |                                                                               ^
a.cc:35:105: error: too many decimal points in number
   35 |         noNeedToMove = length $ filter notLinked $ map (\(_, x)->x) $ filter (\(i, x) -> i == x) $ zip [1..] l
      |                                                                                                         ^~~
a.cc:41:29: error: stray '\' in program
   41 |     print $ equals l $ map (\[x,y]->(x,y)) pairs
      |                             ^
a.cc:1:1: error: 'import' does not name a type
    1 | import Control.Monad (replicateM)
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'