Sort by

recency

|

18 Discussions

|

  • + 0 comments

    scala

    import scala.io.StdIn
    
    object Solution {
      private final case class Pair(i1: Int, i2: Int)
    
      private def group(n: Int, pairs: Seq[Pair]): Seq[Seq[Int]] = {
        val parents = (0 to n).toArray
    
        def root(i: Int): Int = {
          if (parents(i) != i)
            parents(i) = root(parents(i))
          parents(i)
        }
    
        pairs.foreach { pair =>
          val root1 = root(pair.i1)
          val root2 = root(pair.i2)
          if (root1 < root2)
            parents(root2) = root1
          else if (root1 > root2)
            parents(root1) = root2
        }
        (1 to n).groupBy(root).values.toSeq
      }
    
      private def cost(n: Int): Int = {
        math.ceil(math.sqrt(n)).toInt
      }
    
      def main(args: Array[String]): Unit = {
        val arr = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        val n = arr.next().toInt
        val m = arr.next().toInt
        val pairs = (1 to m)
          .map(_ => arr.next().split(' ').map(_.toInt))
          .map(p => Pair(p.head, p.last))
        val groups = group(n, pairs)
        val costs = groups.map(_.length).map(cost)
        println(costs.sum)
      }
    }
    
  • + 0 comments

    Hi guys! In my opinion, some test case data are not following common sense rule. How many hands have inmate? Rather not more than 2. So how many pairs can inmate create - in my opinion also 2. But in TC3 data points that inmate 22 is cuffed with 5 other inmates! Click here to know more

  • + 0 comments

    This is a chart issue. Detainees are vertices and chains are edges. All we really want is to work out the size of each component(group of detainees) of the diagram, then compute cost of a transport for that limit, and total all expenses.click here

  • + 0 comments

    This is a Disjoint Set problem, one of the few data structures that are specifically imperative, so I wonder why this problem is in the "Functional Structures" category. Anyways, here is my Haskell solution using mutable arrays to implement DSU:

    import Control.Monad (replicateM_)
    import Data.List (sort, group)
    import Data.Array.IO
    
    main :: IO ()
    main = do
      g <- (initDSU =<< readLn)
      m <- readLn
      replicateM_ m $ addEdge g
      getSizes g >>= print . cost
    
    addEdge :: DSU -> IO ()
    addEdge g = do
      pq <- getLine
      let [p, q] = fmap read $ words pq
      unionSets p q g
    
    -- Given: sizes of connected components.
    cost :: [Int] -> Int
    cost = sum . fmap (ceiling . sqrt . fromIntegral)
    
    data DSU = DSU {
      _n :: Int,
      parent :: IOUArray Int Int,
      size :: IOUArray Int Int
    }
    
    setSize :: DSU -> Int -> Int -> IO ()
    setSize = writeArray . size
    
    setParent :: DSU -> Int -> Int -> IO ()
    setParent = writeArray . parent
    
    getSize :: DSU -> Int -> IO Int
    getSize = readArray . size
    
    getParent :: DSU -> Int -> IO Int
    getParent = readArray . parent
    
    initDSU :: Int -> IO DSU
    initDSU n = do
      p <- mkArray [1..n]
      s <- mkArray $ replicate n 1
      pure $ DSU n p s
      where mkArray = newListArray (1, n)
    
    findSet :: Int -> DSU -> IO Int
    findSet v g = do
      p <- getParent g v
      if v == p then pure p
      else do
        r <- findSet p g
        setParent g v r
        pure r
    
    unionSets :: Int -> Int -> DSU -> IO ()
    unionSets a b g = do
      a' <- findSet a g
      b' <- findSet b g
      if a' == b' then pure ()
      else do
        sa <- getSize g a'
        sb <- getSize g b'
        let (ar, br) = if sa < sb then (b', a') else (a', b')
        setParent g br ar
        setSize g ar $ sa + sb
    
    getReprs :: DSU -> IO [Int]
    getReprs g@(DSU n _ _) = mapM (flip findSet g) [1..n] >>=
      pure . fmap head . group . sort
    
    getSizes :: DSU -> IO [Int]
    getSizes g = getReprs g >>= mapM (getSize g)
    
  • + 0 comments

    can do this in 10 lines using import Data.Graph (buildG, scc) in Haskell