import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import qualified Data.ByteString.Char8 as C
import Data.List
import qualified Data.Map.Strict as M

main = sub <$> get >>= put

get = fmap (unfoldr (C.readInt . C.dropWhile (==' '))) . C.lines <$> C.getContents

sub ([n,k,_]:es) = sol n (splitAt k es)

put = putStrLn . unwords . fmap show

sol n (es0,es1) = runST $ do
  uf0 <- newUF n
  forM_ es0 (unionUF uf0) 
  uf1 <- newUF n
  forM_ es1 (unionUF uf1)
  ps <- mapM (liftM2 (,) <$> findUF uf0 <*> findUF uf1) [1..n]
  return $ ((<$>) =<< cnts) ps

cnts = (M.!) . M.fromList . fmap (liftM2 (,) head length) . group . sort

-- | Union-Find
data UnionFind s = UnionFind {ids:: STUArray s Int Int, szs:: STUArray s Int Int}

newUF :: Int -> ST s (UnionFind s)
newUF n = liftM2 UnionFind (newListArray (1,n) [1..n]) (newArray (1,n) 1)

findUF :: UnionFind s -> Int -> ST s Int
findUF uf i = do
  ix <- readArray (ids uf) i
  if ix == i
    then return i
    else do
      writeArray (ids uf) i =<< readArray (ids uf) ix
      findUF uf ix

unionUF :: UnionFind s -> [Int] -> ST s ()
unionUF uf [p,q] = do
  i <- findUF uf p
  j <- findUF uf q
  szi <- readArray (szs uf) i
  szj <- readArray (szs uf) j
  if szi < szj
    then do
      writeArray (ids uf) i j
      writeArray (szs uf) j (szi+szj)
      writeArray (szs uf) i 0
    else do
      writeArray (ids uf) j i
      writeArray (szs uf) i (szj+szi)
      writeArray (szs uf) j 0
