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 results839 problems
TitleLevelTopicsSolvedTime limitMemory limitJudge
Pesky HeroesGiven a tree-like cave structure with orc traversal rules based on left/right turns and one allowed right-turn override, find the minimum number of gateways needed to reach all trap-free dead ends.Hard8TreeSimulation+1No attempts yet1s128 MBJudgeable
Cosmic StationGiven all pairwise distances between leaves of a tree, determine the number of internal (branching) nodes.Hard8TreeGraph+1No attempts yet1s128 MBJudgeable
Ninja AssignmentChoose a manager and up to budget many ninjas from the manager's subtree, possibly passing through unassigned ninjas, to maximize assigned count times the manager's leadership.Hard8TreeDFS+2No attempts yet1s256 MBJudgeable
PatrolBuild K (1 or 2) unit-length shortcuts in a tree so that the shortest closed walk from village 1 covering every edge exactly as required is minimized.Hard8TreeDynamic programming+2No attempts yet1s64 MBJudgeable
Paper RouteWith N+1 nodes and exactly N roads, find the cheapest closed walk from node 0 covering all addresses, then add the campus travel cost from wherever you end.Hard8GraphDFS+2No attempts yet1s128 MBJudgeable
Rotate to RootGiven a binary tree, compute the height of the tree after each node is rotated to the root one at a time.Hard8TreeDFS+2No attempts yet1s128 MBJudgeable
TreequivalenceGiven two textual tree notations, decide whether they describe the same unrooted planar drawing, allowing any root and cyclic order around each vertex.Hard8TreeHash map+2No attempts yet1s128 MBJudgeable
Split WindowsGiven a preorder traversal of a split tree, draw the minimum-sized grid whose boundaries match the layout, applying proportional rounding at each split.Hard8TreeRecursion+2No attempts yet1s128 MBJudgeable
Dr. Podboq, or: How We Became AsymmetricRead a binary tree of cells, define each cell's left-right similarity by shared subtree shapes up to child swaps, then reorder children by asymmetry and print the normalized tree.Hard8TreeDFS+2No attempts yet1s128 MBJudgeable
Flight PlanningGiven a tree, delete one edge and add one edge so the result is a tree with the smallest possible diameter.Hard8TreeDFS+2No attempts yet1s128 MBJudgeable
FishGiven fish lengths and gem kinds, count how many distinct gem-count combinations a single fish can ever hold, modulo M, where a fish can eat another only if at least twice as long.Hard8Dynamic programmingSorting+2No attempts yet3s128 MBJudgeable
Sabotaging the Marathon TrainingGiven a spanning tree of paved edges plus weighted unpaved edges, delete cheap unpaved edges so that no even-length simple cycle remains.Hard8GraphDynamic programming+2No attempts yet1s128 MBJudgeable
Tree PathGiven a directed tree, find the minimum number of reversed-edge paths to add so that every node can reach every other node.Hard8TreeGraph+2No attempts yet1s128 MBJudgeable
Joining CouplesEach city has one directed outbound flight, forming a functional graph; for each query find the minimum combined distance from two starting cities to any common reachable city, or -1.Hard8GraphTree+2No attempts yet1s128 MBJudgeable
Power GenerationBuild the tree formed by attaching each new plant to the nearest older one, then split it into the most connected subtrees each having total capacity at least C.Hard8TreeDynamic programming+2No attempts yet3s128 MBJudgeable
Optical FiberGiven a tree of cities, each with up to 50 candidate router sites, pick one site per city to minimize the sum of Euclidean edge lengths.Hard8Dynamic programmingTree+2No attempts yet1s128 MBJudgeable
The Crayfish ScrivenerProcess type and undo commands, including nested undos, and answer queries for the character at a given position.Hard8StackTree+2No attempts yet2s512 MBJudgeable
RaceGiven a weighted tree, find a path of total length exactly K that uses the fewest edges, or report -1 if none exists.Hard8TreeDivide and conquer+2No attempts yet3s256 MBJudgeable
Balanced TreesGiven a tree whose nodes are labeled with parentheses, find the maximum nesting depth over all paths that spell a balanced parenthesis string.Hard8TreeDFS+2No attempts yet1s128 MBJudgeable
Farm ManagementA tree of N farms gets path updates that add 1 to every edge on a path, plus path queries that sum edge values on a path; process M operations online.Hard8TreeSegment tree+2No attempts yet1s128 MBJudgeable
Cow TelephonesGiven a tree with cows at its leaves and vertex capacity K plus unit edge capacity, find the maximum number of disjoint leaf-to-leaf conversation paths.Hard8TreeDynamic programming+2No attempts yet1s128 MBJudgeable
Rocks and TreesRocks sit on the non-root nodes of a rooted tree; players alternately push up to L rocks from a node to its parent, and after each point update you decide if the first player wins.Hard8Game theoryTree+2No attempts yet1s128 MBJudgeable
Pink FloydGiven the all-pairs shortest-distance matrix of a weighted tree, reconstruct any tree that produces these distances and print its adjacency list.Hard8TreeGraph+2No attempts yet1s128 MBJudgeable
Rectangular PaintingGiven a nesting tree of rectangles and photo leaf sizes, orient each sibling group horizontally or vertically to minimize the root rectangle area.Hard8TreeDFS+2No attempts yet1s128 MBJudgeable
Minimum-Cost Prefix-Free LanguageGiven n and d character costs, find the minimum total cost of a prefix-free set of exactly n words; multiple test cases end with 0 0.Hard8Dynamic programmingGreedy+2No attempts yet1s128 MBJudgeable
Quelling BladeGiven a tree of weapon prerequisites with costs and benefits, find a buying order that reaches the root in minimum time while maximizing the sum over time of owned benefit.Hard8GreedyDFS+2No attempts yet1s128 MBJudgeable
Ball MachineSimulate a ball machine on a rooted tree: dropping balls follows a fixed priority path, and removing a ball makes balls above roll down; report resting node or number of moves.Hard8TreeSimulation+2No attempts yet1s128 MBJudgeable
Tree InsertionsCount how many permutations of a given sequence build the same binary search tree; values may repeat and answers need big integers.Hard8TreeCombinatorics+2No attempts yet1s128 MBJudgeable
Failing RoadsGiven an expression tree of merge and complement operations, compute the maximum independent set of the resulting graph.Hard8TreeDynamic programming+2No attempts yet1s128 MBJudgeable
BoatherdsGiven a weighted tree and up to 100 queries, decide for each target value whether some pair of vertices has a path cost exactly equal to it.Hard8Divide and conquerTree+2No attempts yet1s128 MBJudgeable
Nutrient TreeGiven a binary tree whose leaves produce nutrients and whose edges have capacity (1+w)^2 after spending w agents, distribute X agents over edges and leaves to maximize the flow reaching the root.Hard8TreeDynamic programming+2No attempts yet2s512 MBJudgeable
Word CountingGiven a rooted tree whose edges carry letter strings, count distinct occurrences of a query word along all paths from the root to the leaves, identifying each occurrence by its start and end position.Hard8String matchingTrie+2No attempts yet1s128 MBJudgeable
TollA billionaire sets tolls on K new roads of his choosing so that the minimum spanning tree routing all traffic to town 1 maximizes his revenue, where K is at most 20.Hard8Minimum spanning treeGreedy+2No attempts yet3s128 MBJudgeable
UnterA connected graph with N houses and exactly N edges (one cycle) must answer up to 1e6 shortest distance queries.Hard8GraphDFS+2No attempts yet1s1024 MBJudgeable
ThievesGiven a tree with K robbed cities, block some cities at cost a_i so that the reachable set of cities from the robbed nodes through unblocked cities is minimized in total cost (blocking plus M per searched city).Hard8TreeDynamic programming+2No attempts yet1s1024 MBJudgeable
Fixing CodesGiven a prefix-free code and a new binary string, find the minimum total number of bits to append so the whole multiset becomes prefix-free again.Hard8GreedyTree+2No attempts yet1s128 MBJudgeable
Tree SimilarityGiven two ordered rooted trees, find the minimum number of node relabel, delete, and insert operations to turn the first tree into the second.Hard8Dynamic programmingTree+2No attempts yet3s128 MBJudgeable
City DrivingIn a connected graph with N nodes and N edges, answer many shortest-path queries between pairs of nodes.Hard8TreeGraph+2No attempts yet1s128 MBJudgeable
Highway ConstructionChoose a path in a weighted tree so that the maximum distance from any node to the path is minimized, and report that distance.Hard8TreeBinary search+2No attempts yet1s128 MBJudgeable
Worst LocationsGiven a perfect binary tree and two distance-from-leaf descriptions, decide whether some pair of matching vertices sits farther than Z apart.Hard8TreeGeometry+2No attempts yet1s128 MBJudgeable
Counting BSTCount insertion sequences of distinct values from 1..M that build a BST with the same shape as a given sequence, modulo 1000003.Hard8CombinatoricsTree+2No attempts yet1s128 MBJudgeable
Counting HeapsCount the number of ways to label a rooted tree with 1..n so each node is smaller than its parent, modulo a possibly composite m.Hard8CombinatoricsTree+2No attempts yet5s128 MBJudgeable
CaveGiven a tree of n nodes, find every k such that the tree splits into k connected parts of equal size.Hard8TreeDFS+2No attempts yet3s256 MBJudgeable
Ternary TreesLabel the leaves of a complete ternary tree so that, given a fixed query order, the leaf values stay hidden until every leaf is asked.Hard8TreeRecursion+2No attempts yet1s128 MBJudgeable
FirefighterOn a graph with max degree 3, fire spreads one step per hour while one house can be protected each hour; maximize houses kept safe.Hard8GraphTree+2No attempts yet1s128 MBJudgeable
Fruit ChickenA tree has shops at one end and houses at the other, separated by a single bridge edge; assign each open shop a distinct house minimizing the time until all couriers arrive, with no two couriers sharing a road at once.Hard8TreeBinary search+2No attempts yet3s128 MBJudgeable
IslandGiven all pairwise shortest tolls among the n seaside triangles, recover the adjacency structure and edge weights of the underlying border tree.Hard8TreeDFS+2No attempts yet1s128 MBJudgeable
PolygonsA convex polygon is triangulated, one triangle is black; players alternately cut off an ear triangle, and whoever removes the black triangle wins. Decide if the first player wins.Hard8Game theoryTree+2No attempts yet1s128 MBJudgeable
The Lightest LanguageGiven n, k, and letter weights, find the minimum total weight of a prefixless set of exactly n words over k letters.Hard8TreeGreedy+2No attempts yet1s128 MBJudgeable
Step Traversing a TreeGiven a tree on n vertices, find the smallest c such that the vertices can be visited in some order where each consecutive pair is at distance at most c.Hard8TreeDFS+2No attempts yet1s128 MBJudgeable
SubwayGiven a tree with n stations, choose l non-branching paths (routes) to cover as many distinct vertices as possible.Hard8TreeDynamic programming+1No attempts yet3s128 MBJudgeable
Fire Extinguisher InstallationPlace extinguishers on tree rooms, each covering at most S rooms within distance K, so every room is covered; minimize the count.Hard8TreeGreedy+1No attempts yet1s128 MBJudgeable
DynamiteLight the fuses in exactly m chambers of a tree so that every charge-detonation is covered as early as possible, and report the last detonation time.Hard8TreeBinary search+2No attempts yet2s128 MBJudgeable
InspectionFor each root of a tree, find the minimum time for a tour that inspects every node and returns to the root each time, with no two consecutive trips using the same first edge.Hard8TreeDFS+2No attempts yet5s128 MBJudgeable
SalariesGiven a rooted tree with salaries a permutation of 1 to n increasing toward the root and some values revealed, print each value forced by the revealed ones or 0 otherwise.Hard8TreeGreedy+2No attempts yet1s128 MBJudgeable
Triumphal ArchGiven a tree rooted at town 1, find the minimum number of crews so that each town gets its arch built before the king's first arrival, where the king's walk is unknown.Hard8TreeGreedy+2No attempts yet1s128 MBJudgeable
FuelFind the maximum number of distinct vertices of a tree that can be visited by a walk of length at most m edges.Hard8TreeDFS+1No attempts yet1s128 MBJudgeable
Number of Tree AutomorphismsCount the automorphisms of a tree modulo 1e9+7.Hard8TreeDFS+2No attempts yet5s128 MBJudgeable
FirmProcess hires and queries on a growing rooted tree, counting employees at exact depth offset k below a given node at query time.Hard8TreeDFS+2No attempts yet2s512 MBJudgeable
DivisorsGiven n and an expression built from divisors of n with gcd and lcm, decide whether the expression's value is the same for every assignment of variables.Hard8Number theoryTree+2No attempts yet2s512 MBJudgeable
Termites 2Given a tree with its edges ordered, two players alternately remove an uneaten endpoint of the next edge; report the round the loser is forced and loses, or -1 for a draw.Hard8Game theoryTree+2No attempts yet2s512 MBJudgeable
Enumeration of Road Network PlansCount the isomorphism classes of trees on n vertices whose diameter equals d, modulo a prime p.Hard8CombinatoricsTree+2No attempts yet1s128 MBJudgeable
WormsOn a tree, worms each move every hour to an adjacent house; decide whether they can all meet and find the minimum number of hours.Hard8TreeGraph+2No attempts yet1s128 MBJudgeable
Binary Tree Lexicographic NumberGiven binary trees with ordered left and right children, find each tree's rank in height-first lexicographic order modulo 1000000000.Hard8Dynamic programmingTree+2No attempts yet1s128 MBJudgeable
Untamed TreeThe task is to output for each leaf label the compressed subtree of its leaves and branching ancestors in preorder.Hard8TreeSorting+2No attempts yet1s128 MBJudgeable
The Company ChoirGiven a rooted tree where each node has a pitch and a distinct ability score, answer queries that ask for the k highest-ability subordinates of a node whose pitch lies in a range [a,b].Hard8TreeDFS+2No attempts yet1s128 MBJudgeable
RacesGiven a tree and a set of allowed endpoints, find the maximum number of vertex-disjoint paths whose endpoints both lie in the allowed set.Hard8TreeDynamic programming+2No attempts yet1s128 MBJudgeable
SpidersGiven two planar triangulations built by repeatedly attaching a new vertex to an outer edge, decide whether they are isomorphic as graphs.Hard8GraphTree+2No attempts yet1s128 MBJudgeable
House of CardsMarcel removes at most k cards in whole leaning pairs, clearing cards above before the cards they rest on, to maximize the recovered sum.Hard8Dynamic programmingTreeNo attempts yet1s512 MBJudgeable
DrzewaFor each node of a labeled rooted tree, find the leaf below it whose downward label string is lexicographically largest, breaking ties by smaller leaf number.Hard8TreeGreedy+2No attempts yet1s128 MBJudgeable
Heavy BlocksTopple n distinct-weight blocks with the fewest pushes when each push fells lighter neighbors in one direction until a heavier block or gap.Hard8Dynamic programmingStack+2No attempts yet1s128 MBJudgeable
The ChampionshipPair as many employees as possible so each team holds two people with neither managing the other.Hard8GreedyTree+2No attempts yet1s128 MBJudgeable
Contour MapGiven up to 20000 non-crossing convex orthogonal polygons, compute the maximum nesting depth where the outermost level is 1.Hard8GeometrySorting+2No attempts yet3s128 MBJudgeable
Tree LabelingThe program counts labelings of a tree with up to 1000 vertices that preserve each label's neighbor label set.Hard8TreeCombinatorics+1No attempts yet1s128 MBJudgeable
Series-Parallel Parking LotPlace as many extra cars as possible on empty spaces of the encoded lot so every car still reaches the exit through empty spaces.Hard8Dynamic programmingTree+1No attempts yet2s256 MBJudgeable
Disjoint water supplyCount the pairs of cities with paths from city 1 that meet only at city 1 in a pipe network ordered by decreasing altitude.Hard8GraphTopological sort+2No attempts yet1s128 MBJudgeable
Inverting HuffmanGiven code lengths that some Huffman run can produce, find the smallest total character count that allows those lengths.Hard8GreedyTree+1No attempts yet1s128 MBJudgeable
Moves on an Infinite Binary TreeStarting from the node reached by S, count the distinct nodes reachable by following any subsequence of T on an infinite binary tree.Hard8Dynamic programmingTree+1No attempts yet2s128 MBJudgeable
Hidden TreeFind the longest subsequence that forms the leaves of a binary tree where each internal node has equal left and right sums.Hard8Dynamic programmingTree+1No attempts yet5s128 MBJudgeable
Safari ParkTriangles are inserted one at a time and each query asks which earlier triangle strictly contains a point, reporting -1 on a boundary and 0 outside.Hard8GeometryTreeNo attempts yet5s128 MBJudgeable
PolarizationOrient every edge of the given tree and report the smallest and largest possible numbers of town pairs connected by a directed path.Hard8TreeDynamic programming+1No attempts yet3s512 MBJudgeable
Beads and WiresYou choose append and insert orders that build the given weighted tree to maximize the total length of insert-created edges.Hard8Dynamic programmingTree+2No attempts yet1s128 MBJudgeable
SupercomputerGiven a rooted tree of unit-time tasks and many processor counts, compute the fastest finishing time for each count.Hard8TreePrefix sum+2No attempts yet2s256 MBJudgeable
Persuading the AdvisersTwo rivals take turns claiming undecided experts for opposite sides, and the first asks whether he can force the majority-vote tree to favor him.Hard8Game theoryTree+2No attempts yet1s256 MBJudgeable
Recovering the PopulationsGiven a tree and the distance-weighted sums at each node, recover the node weights that produce them.Hard8TreeDFS+1No attempts yet4s256 MBJudgeable
CheatsCount the completion orders of a tree of prerequisites when up to k parent edges can be skipped to grandparents, with no two skipped edges adjacent.Hard8Dynamic programmingTree+1No attempts yet10s256 MBJudgeable
Lengthy Traveling SalesmanArrange every vertex of a tree into a tour that maximizes the total walked distance and return the lexicographically smallest among the longest tours.Hard8TreeGreedyNo attempts yet1s256 MBJudgeable
Road RepairFind a tree path with total cost at most C that maximizes total benefit.Hard8TreeDivide and conquer+1No attempts yet1s256 MBJudgeable
String TransformationFind the fewest adjacent swaps turning one balanced a/b string into another with every intermediate string balanced, or output -1 if impossible.Hard8TreeStack+1No attempts yet1s256 MBJudgeable
ParadesPick the most parade routes between pairs of junctions in a tree so no street is shared by two parades.Hard8Dynamic programmingTree+1No attempts yet3s256 MBJudgeable
Volunteer CampStarting from each house in a weighted tree, find the shortest truck route that visits K marked houses without driving back.Hard8TreeDynamic programming+1No attempts yet2s128 MBJudgeable
Transmutation CirclesFind the activation order of nested circles that maximizes total energy from element flips in already active circles.Hard8Dynamic programmingTree+1No attempts yet10s256 MBJudgeable
Pangaea 2Starting from a given tree, each added road updates the minimum total length that keeps all cities connected, and each test case outputs the XOR of the answers.Hard8Minimum spanning treeTreeNo attempts yet20s256 MBJudgeable
Which intersections did I pass?Given a connected undirected graph, each query asks how many vertices lie on some walk from a to b that never revisits the endpoints in the middle.Hard8GraphDFS+1No attempts yet2s256 MBJudgeable
Reducing Network DiameterPay per unit of reduction on tree edge weights so the longest path between any two nodes is at most D at minimum total cost.Hard8GreedyTree+2No attempts yet2s256 MBJudgeable
Who Do You Think You Are?Read a family tree and answer queries naming the relationship of name1 to name2, including blood, cousin, and in-law terms.Hard8GraphTree+2No attempts yet2s256 MBJudgeable
Tree of PainDecide for each small pattern tree whether it embeds into the organization tree with matching labels and ancestry preserved both ways.Hard8TreeDynamic programming+2No attempts yet1s256 MBJudgeable
Party joke setsCount distinct joke-type sets from root-connected guest groups with unique values where each subtree below a guest forms consecutive numbers.Hard8Dynamic programmingTree+1No attempts yet1s32 MBJudgeable
Juice JunctionsAdd up the maximum unit-capacity flow between every pair of junctions in a graph where each junction joins at most three pipes.Hard8GraphTree+2No attempts yet7s512 MBJudgeable
Tree AllocationPartition the nodes into blocks of at most B to minimize the worst root-to-leaf block count, for every choice of root.Hard8Dynamic programmingTree+1No attempts yet10s64 MBJudgeable