import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.IntSet as IS
import Data.Tuple (swap)

main :: IO ()
main = do
  n <- unsafeTextToInt <$> T.getLine :: IO Int
  as <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    as' = VU.fromList as :: VU.Vector Int
    lengthOfLISs :: Tree Int -> VU.Vector Int
    lengthOfLISs = (VU.replicate n 0 VU.//) . flatten . f (0, IS.empty)
      where
        f :: (Int, IS.IntSet) -> Tree Int -> Tree (Int, Int)
        f (l, s) (Node i ts) = Node (i, l') (map (f (l', s')) ts)
          where
            x = as' VU.! i :: Int
            (l', s') = fmap (IS.insert x) . maybe (succ l, s) ((,) l . flip IS.delete s) $ IS.lookupGE x s
  uvs <- sequence . replicate (pred n) $ unsafeListToPair . map (pred . unsafeTextToInt) . T.words <$> T.getLine :: IO [(Int, Int)]
  let
    tree = fromEdgesToTree n uvs 0 :: Tree Int
  VU.mapM_ print . lengthOfLISs $ tree

data Tree a = Node a [Tree a]

extract :: Tree a -> a
extract (Node x _) = x

fromEdgesToAdjacencyVector :: Int -> [(Int, Int)] -> V.Vector [Int]
fromEdgesToAdjacencyVector n = V.accum (flip (:)) (V.replicate n [])

fromEdgesToTree :: Int -> [(Int, Int)] -> Int -> Tree Int
fromEdgesToTree n uvs r = Node r . map (f r) $ adjacency V.! r
  where
    adjacency = fromEdgesToAdjacencyVector n . concatMap (pairToList . ((,) <$> id <*> swap)) $ uvs :: V.Vector [Int]
    f :: Int -> Int -> Tree Int
    f i j = Node j (map (f j) (filter (/= i) $ adjacency V.! j))

flatten :: Tree a -> [a]
flatten t = map extract queue
  where
    queue = t : walk 1 queue
    walk 0 _ = []
    walk l ((Node x ts) : ts') = ts ++ walk (pred l + length ts) ts'
-- flatten (Node x ts) = x : concatMap flatten ts

pairToList :: (a, a) -> [a]
pairToList (x, y) = [x, y]

unsafeListToPair :: [a] -> (a, a)
unsafeListToPair [x, y] = (x, y)

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
