Problems

Pick a problem and write your solution in the built-in editor. The judge runs it against real test cases while you watch, and the wider archive is open to read whenever you like.

Total results837 problems
TitleLevelTopicsSolvedTime limitMemory limitJudge
UnfriendCount the subsets of nodes in a rooted tree that can be removed, where removing a node forces removal of all its descendants.Easy2TreeBrute force+1No attempts yet2s512 MBJudgeable
Huffman TreeGiven Z characters and arity N, decode the stored digit string into the per-character encoding.Easy2TreeString+1No attempts yet3s128 MBJudgeable
ICPC CalculatorEvaluate a dot-indented prefix expression where + sums its operands and * multiplies them.Easy2RecursionTree+1No attempts yet1s256 MBJudgeable
Leaf Nodes in a TreeGiven a tree by parent array, delete a node and all its descendants, then count how many leaf nodes remain.Easy3TreeDFS+1No attempts yet2s128 MBJudgeable
Tree TraversalBuild a binary tree from parent-child input and print its preorder, inorder, and postorder traversals.Easy3TreeDFS+1No attempts yet2s128 MBJudgeable
Kinship DistanceGiven a family forest of parent-child edges, find the shortest kinship distance between two given people or output -1 if unconnected.Easy3GraphBFS+1No attempts yet1s128 MBJudgeable
The Leisurely StrollGiven a rooted tree of choice-nodes where leaf edges lead to pastures, find the maximum number of edges on any root-to-pasture path.Easy3TreeDFS+2No attempts yet1s128 MBJudgeable
S-TreesGiven an S-tree's variable ordering and terminal labels, evaluate the Boolean function for each supplied variable assignment.Easy3TreeSimulation+1No attempts yet1s128 MBJudgeable
Help the problem setterFor each test case, read a binary search tree on labels 1..n and print each node's frequency, computed bottom-up as 1 plus the sum of the frequencies of all its proper descendants.Easy3TreeDFS+2No attempts yet1s128 MBJudgeable
Computation of a Road Network PlanGiven the number of cities n and a target diameter d, print a specific tree: a path of length d with all remaining cities hung off its middle vertex.Easy3TreeImplementation+1No attempts yet1s128 MBJudgeable
Network InvestmentFind the tree edge whose removal maximizes the product of the two component sizes.Easy3DFSTreeNo attempts yet1s128 MBJudgeable
Candy FactoryEach parent in a heap-ordered binary tree makes as many candies as the smaller child count, and the total subtracts consumed ingredients from all made candies.Easy3TreeRecursion+1No attempts yet1s128 MBJudgeable
Cracking Tree CodesGiven a tree and its leaf-stripping code with some entries erased, the program replays the encoding to recover the missing numbers.Easy3SimulationTree+1No attempts yet1s128 MBJudgeable
Complete Binary TreeReconstruct each level of a complete binary tree from its inorder visit order.Easy3TreeRecursionNo attempts yet1s128 MBJudgeable
Hyacinth frequency assignmentAssign one frequency to each edge of a tree by the stated DFS rule so each node uses at most two frequencies.Easy3TreeDFS+1No attempts yet1s256 MBJudgeable
Neurotic NetworkEvaluate the weighted sum from the leaves to the root of a tree and print FREAK OUT for an even result, else the value modulo 1,000,000,007.Easy3TreeDynamic programming+1No attempts yet1s256 MBJudgeable
Finding Parents in a TreeStarting from node 1 as the root, print the parent of every other node in the given tree.Easy3BFSTreeNo attempts yet1s256 MBJudgeable
Orienting Molecular BondsDirect every tree edge from the endpoint at even distance from node 1 to the one at odd distance.Easy3BFSTreeNo attempts yet1s64 MBJudgeable
Modern Art Plagiarism (Small)Decide whether the smaller tree is a connected subgraph of the larger tree, with only the shapes mattering, not the original labels.Easy3TreeBacktrackingNo attempts yet5s512 MBJudgeable
Largest Common Vertex on a Rooted TreeFor two nodes in a complete binary tree numbered heap-style, find the deepest common ancestor k and print 10k.Easy3TreeMath+1No attempts yet2s512 MBJudgeable
Binary treeGiven each node's parent in a binary tree with n up to 20, print the height (distance from the root) of every node.Easy3TreeDFSNo attempts yet2s512 MBJudgeable
Trees and QueriesCount the vertices in the subtree of each queried node in a tree with a given root.Easy3TreeDFS+1No attempts yet1s128 MBJudgeable
Binary Search TreeInsert a sequence of integers into a BST and output the depth of each inserted node.Easy3TreeRecursion+1No attempts yet2s512 MBJudgeable
Thread TreeGiven n posts where each post names its parent post, print the messages in preorder with dots showing each post's depth.Easy3TreeDFS+1No attempts yet2s512 MBJudgeable
Distance Between Tree NodesGiven a weighted tree and multiple node pairs, compute the path distance between each pair using tree traversal.Medium4TreeBFS+1No attempts yet2s128 MBJudgeable
Diameter of a TreeGiven a weighted tree of up to 10,000 nodes rooted at node 1, compute the maximum-length path between any two nodes.Medium4TreeDFS+1No attempts yet2s128 MBJudgeable
Maximum Independent Set in a TreeGiven a weighted tree, compute a maximum weight independent set using tree DP and output the chosen vertices.Medium4Dynamic programmingTree+1No attempts yet2s128 MBJudgeable
Nearest Common AncestorGiven a rooted tree and two nodes, find their nearest common ancestor for each test case.Medium4TreeDFS+1No attempts yet1s128 MBJudgeable
TreeGiven the preorder and inorder traversals of a binary tree, reconstruct the tree and print its postorder traversal.Medium4TreeRecursion+1No attempts yet1s192 MBJudgeable
Relative RelativesGiven Ted's age of 100 and each descendant's father name plus the father's age at the child's birth, compute every descendant's age and list them oldest first, ties broken by name.Medium4TreeDFS+2No attempts yet1s128 MBJudgeable
Tree GraftingGiven a depth-first traversal string of an ordered tree, report its height and the height after converting it to a left-child/right-sibling binary tree.Medium4TreeStack+2No attempts yet1s128 MBJudgeable
Prefix CodesDecode several binary messages with a prefix code given as a heap-indexed tree string, where a bit 0 or 1 walks to a child until a leaf symbol is reached.Medium4TreeImplementation+2No attempts yet1s128 MBJudgeable
Meeting PlaceGiven a rooted tree and M queries, report the lowest common ancestor of two nodes for each query.Medium4TreeDFS+2No attempts yet1s128 MBJudgeable
Clear Cold WaterGiven a rooted binary tree described by branch points, print the distance from the barn to the endpoint of every pipe.Medium4TreeBFS+2No attempts yet1s128 MBJudgeable
Is It a Tree?For each test case, read directed edges until a pair of zeros and decide whether the graph is a tree under the three given conditions, printing the case number and verdict.Medium4GraphUnion-find+2No attempts yet1s128 MBJudgeable
Tree RecoveryGiven a binary tree's preorder and inorder traversal strings, print its postorder traversal. Process runs until end of file.Medium4TreeRecursion+1No attempts yet1s128 MBJudgeable
From Prefix to PostfixTranslate each prefix arithmetic expression over + and - into its equivalent postfix form, stopping at the terminating 0.Medium4StackTree+2No attempts yet1s128 MBJudgeable
Packet RoutingGiven a tree with weighted edges connecting N computers, compute the travel time along the unique path between each query pair of computers.Medium4TreeDFS+2No attempts yet1s128 MBJudgeable
Christmas Tree OrnamentA tree of N lamps is built incrementally and recolored M times; after each recolor, report how many edges join two lamps of equal color.Medium4TreeImplementationNo attempts yet1s1024 MBJudgeable
Holiday GiftsAssign one of two priced gifts to each node of a rooted tree so no two adjacent employees share a gift, minimizing total cost.Medium4TreeDynamic programming+2No attempts yet1s128 MBJudgeable
Handong the Salesman!Given a tree, start at node 1 and visit m listed nodes in order, summing the tree distances between consecutive stops.Medium4GraphTree+2No attempts yet1s128 MBJudgeable
DyzioParse a 0/1 description of recursive halving cuts and output the cut count at which the first shortest piece appears.Medium4TreeDFS+2No attempts yet1s128 MBJudgeable
MegavirusGiven k and n viruses from generation k in a binary tree, find the deepest generation whose node is an ancestor of all given viruses.Medium4TreeBit manipulation+2No attempts yet1s128 MBJudgeable
MerchantFind the simple path, possibly empty, in a weighted tree whose edge weights sum to the largest value.Medium4TreeDynamic programming+1No attempts yet1s128 MBJudgeable
Quad TreesBuild the quadtree partition of each binary image and print its level-order bitstream as uppercase hex without leading zeros.Medium4Divide and conquerRecursion+2No attempts yet1s128 MBJudgeable
CousinsReconstruct the tree defined by consecutive-number groups and count the cousins of node k.Medium4TreeSimulationNo attempts yet3s128 MBJudgeable
Tree ColoringCount colorings of an N-node tree with K colors so adjacent nodes differ, modulo 93563.Medium4Dynamic programmingTree+1No attempts yet1s128 MBJudgeable
ClawsCompute each hinge grade from subtree and root-path bar weights and report the largest root-to-claw sum of grades over all claws.Medium4TreeDFSNo attempts yet2s512 MBJudgeable
Traffic CongestionPick the tree city that minimizes the largest number of fans traveling on any single road when all fans leave the arena city.Medium4TreeDFSNo attempts yet3s256 MBJudgeable
Turtle ElderPick safe start and end islands in a tree so the sum of values along the path is as large as possible, staying home when the best sum is not positive.Medium4TreeDynamic programmingNo attempts yet5s256 MBJudgeable
Switch toggling instructionSimulate trains arriving over time through a binary switch tree and emit the fewest latest possible toggles routing each train to its platform.Medium4SimulationTreeNo attempts yet2s256 MBJudgeable
Binary Mobile WidthCompute the horizontal width of a balanced binary mobile from rod lengths and bead weights using torque balance.Medium4TreeDFSNo attempts yet1s256 MBJudgeable
NetworkAdd the fewest edges to a tree so it stays connected after any single edge breaks, pairing leaves in the prescribed DFS order.Medium4TreeDFS+1No attempts yet1s256 MBJudgeable
Lowest Common AncestorGiven a rooted tree, answer each query with the number of the deepest vertex that is an ancestor of both given vertices.Medium4TreeDFSNo attempts yet3s256 MBJudgeable
Lowest Common Ancestor 2Given a rooted tree with up to 100,000 nodes, answer up to 100,000 lowest common ancestor queries.Medium4TreeDFSNo attempts yet1.5s256 MBJudgeable
Rational Number Tree (Small)Given the level-order listing of the rational number tree, find the n-th fraction and the position of a given fraction.Medium4TreeBFS+1No attempts yet5s512 MBJudgeable
Decision TreeParse a recursively defined decision tree, then for each animal walk the tree using its features and multiply node weights to get the probability.Medium4TreeRecursion+2No attempts yet5s512 MBJudgeable
Ceiling FunctionInsert each prototype's values into a binary search tree in order, then count how many distinct tree shapes appear across the prototypes.Medium4TreeImplementation+1No attempts yet5s512 MBJudgeable
Road Network of a Perfect Binary TreeFind the minimum number of cars whose vertex-disjoint paths cover every vertex of a perfect binary tree of height H exactly once.Medium4TreeDynamic programming+1No attempts yet2s512 MBJudgeable
Scrooge MinhoGiven a tree, place one fire station at the vertex minimizing the maximum distance to any other vertex, and output that distance.Medium4TreeGraph+2No attempts yet2s512 MBJudgeable
Tree and paths of length twoDecide whether some tree on N nodes has exactly S simple paths of length 2.Medium4TreeCombinatorics+1No attempts yet2s512 MBJudgeable
Rational SequenceGiven p/q, find its index in the breadth-first ordering of the Calkin-Wilf tree, where each node p/q has children p/(p+q) and (p+q)/q.Medium4MathNumber theory+2No attempts yet2s512 MBJudgeable
Far Far AwayGiven a directed tree rooted at city 1 with weighted edges, find the maximum root-to-node path weight, or -1 if it stays below M.Medium4TreeDFS+2No attempts yet2s512 MBJudgeable
DragsterGiven pairwise win probabilities and a binary elimination bracket, compute the probability that driver 1 wins the tournament.Medium4ProbabilityTree+1No attempts yet2s512 MBJudgeable
Obfuscated TreesDecode a tree from its obfuscated token stream, where each internal node carries an ordering code and subtree count, then print its values in pre-order.Medium4TreeRecursion+1No attempts yet2s512 MBJudgeable
Build a treeConstruct a tree on n nodes with exactly m leaves whose sorted edge list is lexicographically smallest, and print its n-1 edges.Medium4TreeGreedy+2No attempts yet2s512 MBJudgeable
Company culture 1Given each employee's manager and a list of praises, propagate every praise value down the whole subtree and print the total each employee receives.Medium4TreeDFS+2No attempts yet2s512 MBJudgeable
Cut Vertices and BridgesGiven a tree with N vertices and queries, report for each query whether a specified vertex is a cut vertex or a specified edge is a bridge.Medium4TreeDFS+2No attempts yet1s512 MBJudgeable
Grass PlantingGiven a tree with N fields, plant grass so that no two fields at distance one or two share a type, and output the minimum number of types needed.Medium4TreeGreedy+1No attempts yet2s512 MBJudgeable
Milk FactoryGiven a directed tree on N nodes, find the smallest node reachable from every other node, or output -1 if none exists.Medium4GraphDFS+2No attempts yet2s512 MBJudgeable
Road ConstructionGiven a tree with n countries and weighted edges, sum over all edges of weight times the absolute difference in size of the two components the edge splits the tree into.Medium4TreeDFS+2No attempts yet2s512 MBJudgeable
I Will Be Your Bridge!A tree lost one edge, splitting it into two components. Print any pair of islands, one from each component, that reconnects the tree.Medium4GraphDFS+2No attempts yet1s512 MBJudgeable
Gugu the RaccoonGiven a weighted tree rooted at node 1, find the maximum distance from node 1 to any other node.Medium4TreeDFS+2No attempts yet1s1024 MBJudgeable
CocktailGiven a tree of N ingredients linked by N-1 known mass ratios, compute the smallest positive integer masses that satisfy every ratio.Medium5TreeDFS+2No attempts yet2s128 MBJudgeable
PrefixGiven up to 50 words, find the largest subset where no word is a prefix of another, using a trie and tree DP.Medium5TrieDynamic programming+2No attempts yet2s128 MBJudgeable
Tree DiameterGiven a weighted tree with up to 100,000 vertices, compute the diameter as the maximum distance between any two vertices.Medium5TreeDFS+2No attempts yet2s256 MBJudgeable
Roads of the Northern CountryGiven the edges of a weighted tree of up to 10,000 cities, compute the length of the tree's diameter (the longest path between two nodes).Medium5TreeGraph+2No attempts yet1s128 MBJudgeable
New Year PartyPick a maximum-score guest list from a company tree so no employee and their direct manager both attend, computed with and without the root.Medium5Dynamic programmingTree+1No attempts yet2s128 MBJudgeable
Distance Between VerticesGiven a weighted tree with up to 40,000 nodes, answer up to 10,000 queries about the path distance between two vertices, requiring an LCA-based approach for efficiency.Medium5TreeBinary search+2No attempts yet2s128 MBJudgeable
Bug on a TreeGiven a tree with fruit values on vertices, find the maximum sum path (simple path) and its smallest-numbered starting endpoint.Medium5TreeDynamic programming+1No attempts yet2s128 MBJudgeable
Tree Height and WidthGiven a binary tree's parent-child structure, place nodes on a grid by binary tree layout rules and find the level with the maximum column width, breaking ties by smallest level.Medium5TreeBFS+1No attempts yet2s128 MBJudgeable
Recover Tree PreorderGiven a tree's inorder and postorder sequences, reconstruct the tree and output its preorder traversal.Medium5TreeRecursion+1No attempts yet5s128 MBJudgeable
Tree CuttingGiven a tree with n vertices, find the minimum number of edges to cut so some resulting piece has exactly m vertices, or report impossibility.Medium5TreeDynamic programming+1No attempts yet2s128 MBJudgeable
GemsGiven a tree, assign positive integer prices to vertices so adjacent vertices differ, minimizing total sum, essentially a greedy coloring based on tree structure.Medium5TreeGreedy+1No attempts yet1s128 MBJudgeable
LabyrinthGiven a grid maze that forms a tree of free cells, compute the diameter (longest path in number of steps) between any two free cells.Medium5GraphBFS+1No attempts yet1s256 MBJudgeable
Kingdom RoadmapGiven a tree, compute the minimum number of extra edges needed so the graph stays connected after any single edge removal, which equals the number of leaves divided by two, rounded up.Medium5TreeGraph+1No attempts yet2s128 MBJudgeable
Family TreeGiven a tree where each node lists its single child (parent-of relation), compute the minimum number of extra ancestor nodes to insert so no node has more than d parents.Medium5TreeGreedy+1No attempts yet2s64 MBJudgeable
GodfatherGiven an undirected tree, find all vertices that minimize the largest connected component size after removal (the tree centroid(s)).Medium5TreeDFS+1No attempts yet2s64 MBJudgeable
Pathological PathsGiven a set of file paths defining a directory tree, resolve query paths (with '.', '..', and index.html shortcuts) and decide if two paths refer to the same existing file.Medium5StringHash map+2No attempts yet1s128 MBJudgeable
Router Placement to Minimize Maximum TTLGiven a tree, pick the vertex that minimizes the largest distance to any other vertex, and output that minimum maximum distance (the tree's radius).Medium5TreeGraph+2No attempts yet1s128 MBJudgeable
CaterpillarGiven up to 100 nodes, decide whether the graph is a connected tree whose every node lies on or adjacent to some single path.Medium5GraphDFS+2No attempts yet1s128 MBJudgeable
Genealogical ResearchProcess birth and death records, then answer ancestor and descendant queries by printing the family tree recursively with dates.Medium5RecursionTree+2No attempts yet1s128 MBJudgeable
Mobiles AlabamaParse a nested mobile description and compute, for each bar, the tie point that balances the weights hanging from its two sides.Medium5TreeDFS+2No attempts yet1s128 MBJudgeable
Normal FormEvaluate a fully parenthesized AND/OR tree where odd levels are AND and even levels are OR, for several long test cases.Medium5TreeImplementation+2No attempts yet1s128 MBJudgeable
Road TripGiven a weighted tree rooted at city 1, remove exactly one non-root vertex so the round trip from city 1 covering all remaining cities is shortest, and report that length.Medium5TreeDFS+2No attempts yet1s128 MBJudgeable
Pasture WalkingGiven a weighted tree with N vertices and Q queries, find the path length between each queried pair of vertices.Medium5TreeDFS+2No attempts yet1s128 MBJudgeable
Treasure CaveGiven a binary branching from passage 1, find the list of passages on the unique path from the entrance to passage T and its length.Medium5TreeDFS+2No attempts yet1s128 MBJudgeable
EntropyFor each line of text, print the fixed 8-bit ASCII bit length, the optimal prefix-free Huffman bit length, and the compression ratio rounded to one decimal.Medium5GreedyHeap+2No attempts yet1s128 MBJudgeable
Code the TreeParse a parenthesized tree description, then repeatedly remove the smallest-numbered leaf and print its neighbor to build the Prufer code.Medium5TreeImplementation+2No attempts yet1s128 MBJudgeable
Decode the TreeGiven a Prüfer code, rebuild the labeled tree on n vertices and print it as a canonical rooted word with children sorted by number.Medium5TreeHeap+2No attempts yet1s128 MBJudgeable