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
Party at Hali-BulaGiven a company hierarchy tree, find the largest set of employees with no boss and employee both chosen, and report whether that maximum set is unique.Medium6TreeDynamic programming+2No attempts yet1s128 MBJudgeable
ReorganizationGiven each employee's rank in ID order, decide whether a binary hierarchy exists where every non-root has a supervisor with a smaller ID and a better rank.Medium6GreedyTree+2No attempts yet2s512 MBJudgeable
Tree PruningGiven a rooted binary tree with colored nodes, prune subtrees to make the whites minus blacks equal exactly D, minimizing the number of prunes.Medium6TreeDynamic programming+2No attempts yet2s512 MBJudgeable
HerdingPlace the fewest traps on a grid of arrows so that a cat starting anywhere and following arrows forever eventually enters a trapped cell.Medium6GraphDFS+2No attempts yet1s128 MBJudgeable
QuadtreesGiven two quadtree preorder strings for 32x32 black-and-white images, count the black pixels in their union by recursively merging overlapping quadrants.Medium6RecursionTree+2No attempts yet1s128 MBJudgeable
Tree IsomorphismGiven two rooted trees in pre-order with '#' closing each node's child list, decide whether the trees are isomorphic ignoring node labels.Medium6TreeDFS+2No attempts yet1s128 MBJudgeable
JourneyGiven a weighted tree, a start city k, and a set of target cities, find the length of the shortest walk from k that visits every target at least once.Medium6TreeDFS+2No attempts yet1s128 MBJudgeable
Turning a TreeReroot a given ordered tree at a specified leaf so the counter-clockwise order of neighbors at each node stays the same, then print the new tree.Medium6TreeDFS+2No attempts yet1s1024 MBJudgeable
BonsaiRoot a weighted tree and cut edges of minimum total weight so that no original leaf stays connected to the root.Medium6TreeDFS+2No attempts yet1s128 MBJudgeable
Three-Coloring of Binary TreesGiven a binary tree as a digit specification, color each node red, green, or blue so adjacent nodes differ and siblings differ, then report the maximum and minimum number of green nodes.Medium6TreeDFS+2No attempts yet3s128 MBJudgeable
BackpackChoose a set of items whose total mass is at most p, where each chosen item requires its named lower-indexed prerequisite to be chosen too.Medium6Dynamic programmingTree+1No attempts yet1s128 MBJudgeable
Road Network 2Count the labeled trees that realize a prescribed degree sequence, or report that none exist, with n up to two million.Medium6TreeCombinatorics+1No attempts yet5s128 MBJudgeable
MatchingsGiven a tree, compute the size of its maximum matching and count how many maximum matchings exist, modulo m.Medium6Dynamic programmingTree+2No attempts yet3s128 MBJudgeable
Binary Search TreeCount the insertion orders that build the same binary search tree as the given permutation.Medium6CombinatoricsTree+1No attempts yet2s256 MBJudgeable
Binary Search Tree 2Count the permutations of 1 to N that build the same binary search tree as the given permutation.Medium6CombinatoricsTree+1No attempts yet1s128 MBJudgeable
Unscrambling ImagesThe solver recovers the hidden child order at each quadtree node from the test encoding and restores the secret image.Medium6TreeRecursion+1No attempts yet1s128 MBJudgeable
Klingon WarfarePick one subclan from each ordered clan tree so the pair matches in style, child count and sibling order with the largest size.Medium6TreeHash map+1No attempts yet5s128 MBJudgeable
String Insert and PrintMaintain a single string under positional insertions and print the requested substring for each query.Medium6TreeString+1No attempts yet10s256 MBJudgeable
Cartesian TreeBuild the Cartesian tree whose inorder follows one key and heap order follows the other, printing parents and children, or NO when impossible.Medium6StackSorting+1No attempts yet2s64 MBJudgeable
Sky CitiesAdd the fewest bridges so the connected cities stay connected after any single bridge fails, and print the pairs fixed by the leaf-pairing rule.Medium6DFSGraph+1No attempts yet1s256 MBJudgeable
TreeSupport parent changes, path recoloring, and distinct-color path queries on a dynamic rooted tree.Medium6TreeBrute force+1No attempts yet3s256 MBJudgeable
Chicken JoggersPlace the fewest extra lamps so every trail a jogger who starts at intersection 1 and returns after exactly S meters could use has a lamp on one end.Medium6Dynamic programmingTree+1No attempts yet1s256 MBJudgeable
Adjoin the NetworksJoin the forest of cable trees with the fewest new links so the connected network has the smallest possible diameter, and report that diameter.Medium6TreeGreedy+2No attempts yet1s256 MBJudgeable
Farthest node in a weighted treeFor every node of a weighted tree with up to 50000 nodes, print its distance to the farthest node.Medium6TreeDFSNo attempts yet1s256 MBJudgeable
Bundles of JoyBuy the cheapest set of nested or disjoint bundles that covers every dessert type.Medium6Dynamic programmingTreeNo attempts yet3s256 MBJudgeable
Special Christmas TreeFind the maximum node count of a binary tree with exactly L leaves and height at most H.Medium6MathGreedy+1No attempts yet3s256 MBJudgeable
Max FlowCount how many of the K given tree paths pass through each stall and report the largest count.Medium6TreePrefix sum+1No attempts yet2s512 MBJudgeable
Symmetric Trees (Small)Decide whether a colored tree with at most 12 vertices admits a planar straight-line drawing with a vertical line of symmetry.Medium6Brute forceTree+1No attempts yet5s512 MBJudgeable
Rational Number TreeGiven the infinite binary tree that lists every positive rational once, find the nth fraction in level order and the level-order position of a given fraction.Medium6MathNumber theory+2No attempts yet5s512 MBJudgeable
World Cup 2010 (Small)Buy the cheapest set of knockout match tickets so each team misses at most its allowed number of games whatever the results.Medium6Dynamic programmingTreeNo attempts yet5s512 MBJudgeable
Rainbow TreesCount edge colorings of a small tree with k colors so that any two or three consecutive edges on a path get distinct colors, modulo 1e9+9.Medium6Dynamic programmingTree+1No attempts yet5s512 MBJudgeable
Mixing Bowls (Small)Given a recipe tree of mixtures, find the minimum number of bowls needed by choosing the order of preparation.Medium6TreeDFS+1No attempts yet5s512 MBJudgeable
Modern Art PlagiarismDecide whether the smaller tree is isomorphic to some subtree cut from the larger tree.Medium6TreeDFS+1No attempts yet50s512 MBJudgeable
SwapGiven a permutation, decide for each k = 2..n whether to swap position k with floor(k/2), producing the lexicographically smallest reachable sequence.Medium6GreedyTree+1No attempts yet1s256 MBJudgeable
Republic of InhanicaGiven a tree rooted at island 1, cut a minimum-cost set of edges so that every leaf other than the root is disconnected from the root.Medium6TreeDynamic programmingNo attempts yet1s256 MBJudgeable
UniversitiesOn a tree whose nodes are black or white with weighted happiness, find the maximum total weight of a path that contains equally many black and white nodes.Medium6TreePrefix sum+1No attempts yet1s1024 MBJudgeable
Building a GraphBuild a connected graph (tree) with N nodes and N-1 edges, where each node's score depends on its degree, and maximize the total score.Medium6TreeDynamic programming+1No attempts yet2s512 MBJudgeable
Tree CountryCount the subsets of K vertices in a tree that form a connected subtree, modulo 1e9+7.Medium6Dynamic programmingTree+2No attempts yet2s512 MBJudgeable
Happy TreeFind the minimum number of leaves to remove so that no remaining vertex has a descendant whose path distance exceeds that descendant's value.Medium6TreeDFS+1No attempts yet2s512 MBJudgeable
A Dark Flame Dragon Sleeps in My Left HandFor each node in a weighted tree, find the distance to the farthest other node (the tree's eccentricity).Medium6TreeDFS+1No attempts yet2s512 MBJudgeable
Sum of subtree sizesCount all connected subgraphs of a tree and output the sum of their vertex counts modulo 1e9+7.Medium6TreeDynamic programming+2No attempts yet2s512 MBJudgeable
TreeGiven a rooted tree, process a mix of edge deletions and connectivity queries in order, answering YES or NO for each query.Medium6Union-findTree+2No attempts yet2s512 MBJudgeable
Carrot FarmMaintain a set of disjoint planted intervals under plant and harvest operations; after each, report the empty or planted area directly left and right of the affected span. Each area is (number of columns) times L.Medium6IntervalsTree+2No attempts yet3s512 MBJudgeable
Lost in the NightOn a tree, a walker starting at node A repeatedly picks a uniformly random neighbor until reaching hotel B or C; find the probability of hitting B first.Medium6ProbabilityGraph+2No attempts yet2s512 MBJudgeable
Fighting cancerGiven two trees on atoms 1..N, decide whether they are isomorphic as unlabeled graphs and print S or N.Medium6TreeDFS+1No attempts yet2s512 MBJudgeable
Alien Creature NumberingCount the labelings of a complete binary tree of height H with 1..2^(H+1)-1 where every parent's label is smaller than its children's labels, modulo 1e9+7.Medium6CombinatoricsTree+1No attempts yet1s256 MBJudgeable
Free FigurinesGiven two valid nesting configurations of n matryoshka dolls, find the minimum number of place and take-out moves to transform one into the other.Medium6TreeGreedy+1No attempts yet1s512 MBJudgeable
Phonomenal ReviewsGiven a tree and a set of M marked nodes, find the minimum number of edges Jo must walk to visit every marked node, starting anywhere.Medium6TreeDFS+2No attempts yet2s512 MBJudgeable
Sequence and Queries 17Maintain an array under point updates and answer range-minimum queries over subarrays.Medium6Segment treeArray+2No attempts yet2s512 MBJudgeable
Marbles on the treeGiven a rooted ordered binary tree, find the leaf where the K-th marble stops, where each marble at a two-child node goes to the left if left subtree has at most the right subtree's resting marbles, else right.Medium6TreeDFS+2No attempts yet1s128 MBJudgeable
Ant NestGiven paths of food names from the top floor down, print the merged tree with each node indented by two dashes per depth and children in dictionary order.Medium6TrieTree+2No attempts yet1s256 MBJudgeable
Balanced TreeGiven a tree whose vertices each hold A or B, swap characters along edges so no edge joins equal letters, using the fewest swaps, or report -1.Medium6TreeDFS+2No attempts yet1s512 MBJudgeable
Hidden HierarchyBuild a directory tree from file paths, and print the smallest set of directories (expanding or collapsing as needed) that covers every directory whose total size is at least t.Medium6TreeHash map+2No attempts yet1s512 MBJudgeable
Decisions, DecisionsGiven the truth table of an n-variable boolean function, count the vertices in its unique minimal binary decision diagram.Medium6Dynamic programmingDivide and conquer+2No attempts yet2s512 MBJudgeable
Barn PaintingCount the proper 3-colorings of a tree consistent with some pre-colored nodes, modulo 1e9+7.Medium6TreeDynamic programming+2No attempts yet2s512 MBJudgeable
Tournament ChartParse a knockout bracket given as a string and decide whether the reported win counts for all players can be consistent with some assignment of match winners.Medium6TreeDFS+2No attempts yet2s512 MBJudgeable
Pepper WreathGiven a tree whose vertices carry non-negative weights and a cap k, find the minimum number of edges to cut so every resulting component has total weight at most k.Medium6TreeDFS+2No attempts yet1s1024 MBJudgeable
Directory TraversalGiven a directory tree, choose a directory that minimizes the total length of all relative paths from it to every file.Medium6TreeDFS+2No attempts yet2s512 MBJudgeable
Tree Country Tour GuideGiven the move sequence of a shortest round trip that visits every node of an unknown rooted tree, reconstruct each city's parent.Medium6StackTree+1No attempts yet1s512 MBJudgeable
Tree and ColorsGiven a rooted tree with colored vertices and queries f(v,c) counting subtree vertices of color at most c, print the sum of all answers modulo 1e9+7.Medium6TreeDFS+2No attempts yet2s512 MBJudgeable
Two RobotsOn a weighted tree, two robots at given nodes must meet on some edge or its endpoints; find the minimum combined distance traveled.Medium6TreeDFS+2No attempts yet2s512 MBJudgeable
Prime Tree - 3Relabel a tree's vertices with 1 to n to minimize edges whose endpoints share a prime factor, and output the labeling for every test case.Medium6TreeGreedy+2No attempts yet10s512 MBJudgeable
Lost MapGiven an all-pairs distance table of an unknown tree with n up to 2500, recover the tree by its n-1 edges.Medium6TreeGraph+1No attempts yet5s512 MBJudgeable
DebloGiven a tree with numbers on its nodes, add up the XOR of every node-value along every path between two nodes, counting single-node paths too.Medium6TreeBit manipulation+2No attempts yet1s512 MBJudgeable
DFS Special JudgeGiven a tree and a permutation of its vertices, decide whether that permutation can be the DFS visit order starting from vertex 1.Medium6DFSTree+2No attempts yet2s512 MBJudgeable
Life in WartimeGiven N distinct cities (all below 250) in an infinite binary heap tree, count cities that either host a unit or lie on the unique path between two units.Medium6TreeHash map+2No attempts yet2s512 MBJudgeable
StrapsChoose a set of straps forming a rooted tree: each strap occupies one port of its parent, one strap hangs from the phone, and total happiness is maximized.Medium6Dynamic programmingTree+2No attempts yet1s512 MBJudgeable
Give Me Back My Binary Tree!!!Count the number of distinct binary trees with exactly E edges, where mirror images count as different trees, modulo 1e9+7.Medium6Dynamic programmingCombinatorics+2No attempts yet2s1024 MBJudgeable
Bob in WonderlandGiven a tree of N connected links, find the minimum number of link reconnections needed to turn it into a path (each vertex has degree at most two).Medium6TreeGreedy+1No attempts yet2s512 MBJudgeable
Milk VisitsGiven a tree with each node labeled G or H, answer queries asking whether the path between two nodes contains at least one node of a given label.Medium6TreeDFS+2No attempts yet1s512 MBJudgeable
Nested Set ModelRoot an undirected tree at S, traverse children in ascending order, and label each node with nested left/right interval numbers.Medium6DFSTree+2No attempts yet1s1024 MBJudgeable
ExpeditionEach candidate forbids at most one other candidate; choose the largest subset where no included candidate's forbidden partner is also included.Medium6GraphGreedy+2No attempts yet2s512 MBJudgeable
European TripChoose N-1 roads to keep every country connected, then find the closed tour that pays each road cost twice plus visit costs, and return the least total.Medium7Minimum spanning treeGraph+2No attempts yet2s128 MBJudgeable
WarGiven a forest of vassal relations and per-country conquest costs, find the minimum days needed to conquer or force surrender of at least M countries using tree knapsack DP.Medium7Dynamic programmingTree+1No attempts yet2s128 MBJudgeable
Special NodesIn a rooted tree where child weights exceed parent weights, mark vertices special or ordinary to minimize the total of each ordinary vertex's weight minus its nearest special ancestor's weight.Medium7Dynamic programmingTree+2No attempts yet2s128 MBJudgeable
Binary Search TreeGiven the insertion order of 0..N-1 values, compute the sum of node heights in the resulting binary search tree efficiently for N up to 250000.Medium7TreeDivide and conquer+2No attempts yet2s256 MBJudgeable
Treasure HuntGiven a tree of rooms, compute the minimum worst-case number of queries needed to locate a hidden treasure using an optimal centroid-based questioning strategy.Medium7TreeDivide and conquer+2No attempts yet2s128 MBJudgeable
Binary Tree DrawingGiven a binary tree reconstructed from preorder and inorder traversals, compute the minimum area of a grid drawing where each right child extends the row and each down child extends the column.Medium7Dynamic programmingTree+1No attempts yet2s128 MBJudgeable
Compare Tree Traversal PathsGiven two 0/1 Euler-tour strings from DFS traversals of a tree rooted at the same vertex, decide whether both could come from the same underlying tree.Medium7TreeString+1No attempts yet2s128 MBJudgeable
Tree Path PartitionGiven a tree and integer K, find the minimum number of vertex-disjoint paths of length at most K that cover all vertices.Medium7TreeGreedy+1No attempts yet1s128 MBJudgeable
Magic Colored PaperSimulate recursive cutting of a sheet into black/white pieces by successive points, tracking each piece's rectangle, and report the largest and smallest resulting areas.Medium7SimulationBinary search+2No attempts yet1s128 MBJudgeable
Emergency Contact NetworkDesign a fastest call schedule so a class leader's contact network reaches every student, respecting call directions and one-call-per-student, minimizing total minutes.Medium7TreeGreedy+1No attempts yet1s128 MBJudgeable
Salary IncreaseFor each new employee added to a hierarchy tree, count how many ancestors get their salary raised to match the new hire's salary until the chain stabilizes.Medium7TreeBinary search+1No attempts yet1s128 MBJudgeable
Road RepairGiven a tree with per-edge reducible travel times and a total repair budget, minimize the maximum distance from city 1 to any other city by optimally allocating budget across edges.Medium7Binary searchTree+2No attempts yet2s128 MBJudgeable
Railway Route CoverGiven a tree, partition all vertices into vertex-disjoint paths covering every node so that the total edge weight used by the paths is maximized, using tree DP.Medium7Dynamic programmingTree+1No attempts yet1s128 MBJudgeable
Tram Line ColoringGiven a tree covered by paths (tram lines) that share stations, assign colors to lines so that lines sharing a station differ, using the minimum number of colors.Medium7GraphGreedy+1No attempts yet1s128 MBJudgeable
Two Snow PlowsGiven a tree rooted at S, find minimum total edge traversal cost for two walks starting at S that together cover every edge, without returning to S.Medium7TreeDynamic programming+1No attempts yet1s128 MBJudgeable
Royal TreasuryGiven a tree hierarchy, find the maximum matching between parent-child pairs and count the number of maximum matchings, likely modulo something implicit.Medium7TreeDynamic programming+1No attempts yet1s128 MBJudgeable
Which is NextGiven a binary tree's numeric identifier under a bijective encoding, compute the identifier of the next tree in a defined ordering among same-size trees, wrapping around if it is the largest.Medium7RecursionMath+1No attempts yet1s128 MBJudgeable
Mitochondrial EveGiven birth and death events tracking maternal lineage and some sequenced mitochondrial DNA samples, determine whether all currently living individuals must share, might share, or must not share the same mitochondrial DNA.Medium7Union-findTree+2No attempts yet1s128 MBJudgeable
Castle GuardsGiven a recursively described tree of small buildings connected by corridors, compute the minimum vertex cover (guard placement) that watches every corridor in the whole castle.Medium7Dynamic programmingTree+2No attempts yet2s128 MBJudgeable
MobileGiven a recursively nested mobile of weighted objects, find the minimum number of object weights to change so every rod balances left and right.Medium7TreeDynamic programming+1No attempts yet1s128 MBJudgeable
Network MessReconstruct a tree with given leaf-to-leaf distances and output the degrees of internal switch nodes in ascending order.Medium7TreeGraph+2No attempts yet3s128 MBJudgeable
Last Minute ConstructionsGiven a forest of undirected roads plus a set of directed tunnels that must all be used, decide whether a simple path exists from a start to an end village that uses exactly those tunnels.Medium7GraphDFS+2No attempts yet1s128 MBJudgeable
MobileGiven a full binary tree of rods and toys, find the minimum number of left-right child swaps so that all toys sit at depths differing by at most 1, with deeper toys to the left.Medium7TreeGreedy+2No attempts yet1s128 MBJudgeable
Family FortuneChoose K nodes in a rooted tree, no one an ancestor of another, maximizing the sum of weights; print 0 if impossible.Medium7Dynamic programmingTree+1No attempts yet10s128 MBJudgeable
Preorder and PostorderCount how many m-ary trees share the given pre-order and post-order traversals.Medium7TreeDynamic programming+2No attempts yet1s128 MBJudgeable
Tournament BracketsGiven team pairings listed in column-major order and the champion, reconstruct the tournament bracket and render it with slashes, backslashes, and underscores.Medium7ImplementationSimulation+2No attempts yet1s128 MBJudgeable
Trie, Again TrieGiven trees encoded in preorder, find the repeated subtree whose replacement by one shared copy saves the most nodes, breaking ties by size then preorder.Medium7TreeDFS+2No attempts yet1s128 MBJudgeable
MobilesGiven a mobile tree with one unknown object weight, find the weight that balances every bar and check that no two bars collide when they rotate.Medium7TreeDFS+2No attempts yet1s128 MBJudgeable