Curated sets

Graphs and traversal

BFS, DFS, shortest paths, and trees.

All problems
Total results3,710 problems
TopicsJudge
A graph that times out coloring backtrackingPrint one fixed graph: a 55-vertex clique joined by a path, chosen to make a backtracking coloring solver time out.Easy1GraphImplementation+1No attempts yet1s128 MBJudgeable
Friend countsCount the friends of each of N students from M mutual friendship pairs and print the N totals in order.Easy1GraphImplementationNo attempts yet1s256 MBJudgeable
Hands are faster than computersPrint a fixed 4-vertex, 5-edge graph and a fixed proper 4-coloring, with no input to read.Easy1ImplementationGraphNo attempts yet2s512 MBJudgeable
Is Dinic quartic?Print a fixed 4-vertex, 5-edge flow network with the exact edges and capacities given in the statement.Easy1GraphImplementation+1No attempts yet2s512 MBJudgeable
VirusCount how many computers other than computer 1 lie in computer 1's connected component of a small undirected graph.Easy2GraphBFS+1No attempts yet1s128 MBJudgeable
K-Graph OddityCompute the maximum vertex degree in a graph and output the smallest odd integer greater than or equal to it.Easy2GraphImplementation+1No attempts yet1s128 MBJudgeable
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
Making test data 8Print a specific fixed graph: 98 vertices, 1501 edges forming a complete bipartite graph, using the exact listed edge order.Easy2GraphImplementation+2No attempts yet1s128 MBJudgeable
Huffman TreeGiven Z characters and arity N, decode the stored digit string into the per-character encoding.Easy2TreeString+1No attempts yet3s128 MBJudgeable
The Friend of My Enemy Is My EnemyPrint the direct friends of suspect s in increasing order for each network.Easy2GraphSortingNo attempts yet1s128 MBJudgeable
Sanggeun's travelsGiven a connected graph of N countries and M flights, find the fewest flights that visit every country.Easy2GraphNo attempts yet1s256 MBJudgeable
Toroidal gridPrint the given snake sweep of columns 0 to n-2 plus the climb up column n-1 to list a Hamiltonian cycle of the m by n toroidal grid.Easy2ImplementationGraphNo attempts yet1s256 MBJudgeable
Counting FriendsCount the friends of each of N students from M mutual friendship pairs and print the N totals.Easy2GraphArrayNo attempts yet1s256 MBJudgeable
ICPC CalculatorEvaluate a dot-indented prefix expression where + sums its operands and * multiplies them.Easy2RecursionTree+1No attempts yet1s256 MBJudgeable
ITAI VirusCount the initially infected cities plus every city directly joined to one by a single road.Easy2GraphImplementationNo attempts yet1s256 MBJudgeable
Graph Maximum MatchingGiven a small graph, decide whether some edges can be kept so every vertex has degree exactly 1.Easy2GraphBacktracking+1No attempts yet2s512 MBJudgeable
László BabaiFor each of up to 100 test cases, decide whether two simple graphs on 3 vertices given by their edge lists are isomorphic.Easy2GraphBrute force+1No attempts yet1s256 MBJudgeable
LiesUsing union-find on party attendees, determine which parties can be exaggerated without conflicting with people who must always hear the truth.Easy3Union-findGraph+1No attempts yet2s128 MBJudgeable
FriendsGiven an N x N friendship matrix (N ≤ 50), find the maximum count of people reachable within two friendship links from any single person.Easy3GraphMatrix+1No attempts yet2s128 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
Come Back HomeCount simple paths of exact length K from the bottom-left to the top-right cell of a small grid, avoiding blocked cells and revisits.Easy3BacktrackingDFS+1No attempts yet2s128 MBJudgeable
DFS and BFSGiven an undirected graph, output the vertex visit order for DFS then BFS starting from a given vertex, always preferring the smallest-numbered neighbor.Easy3DFSBFS+1No attempts yet2s128 MBJudgeable
Six Degrees of Kevin BaconGiven an unweighted friendship graph, find the vertex whose sum of shortest distances to all others is minimal, breaking ties by smallest index.Easy3BFSGraph+1No attempts yet2s128 MBJudgeable
Hide and SeekCompute the minimum number of moves to reach position K from N on the number line using BFS with steps +1, -1, or doubling.Easy3BFSGraphNo attempts yet2s128 MBJudgeable
Disjoint Set OperationsImplement a union-find structure that merges sets and answers whether two elements share a set across a sequence of operations.Easy3Union-findNo attempts yet2s128 MBJudgeable
Avoiding Food WasteGiven a grid marked with food waste cells, find the size of the largest 4-directionally connected component using BFS/DFS or union-find.Easy3BFSDFS+1No attempts yet2s128 MBJudgeable
Painted AreasCount connected components of 1-cells in a grid (4-directional adjacency) and output the count and the size of the largest component.Easy3BFSDFS+1No attempts yet2s128 MBJudgeable
Travel PlanGiven an adjacency matrix of cities, decide if consecutive cities in a given visit order all lie in the same connected component.Easy3Union-findGraph+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
Maze SearchFind the minimum number of cells traversed on a grid path from top-left to bottom-right using BFS shortest path.Easy3BFSGraph+1No attempts yet1s192 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
Chairperson CandidatesGiven a friendship graph, compute each member's eccentricity via shortest paths and report the minimum eccentricity value with all members achieving it.Easy3GraphBFS+1No attempts yet1s128 MBJudgeable
Housing Complex NumberingGiven a binary grid, count connected groups of adjacent 1-cells and print the number of groups plus each group's size in ascending order.Easy3BFSGraph+1No attempts yet1s128 MBJudgeable
Keypad PasswordsCount length-N digit sequences on a phone keypad where consecutive digits must be adjacent keys, modulo 1,234,567, for up to N=1000.Easy3Dynamic programmingGraph+1No attempts yet1s128 MBJudgeable
RadioGiven a start and target frequency plus up to 5 preset shortcut buttons, find the minimum presses using +1, -1, or jump to a preset to reach the target.Easy3GraphShortest path+1No attempts yet1s128 MBJudgeable
Unreachable FunctionsGiven a control flow graph built from three instruction types with fall-through or jump edges, count how many functions are unreachable from the first one.Easy3GraphBFS+1No attempts yet1s128 MBJudgeable
GatesGiven an acyclic NAND-gate circuit whose inputs are all tied to one variable x, decide whether the overall output depends on x (answer 1) or is constant (answer 0).Easy3SimulationGraph+1No attempts yet1s128 MBJudgeable
NetworkingGiven points and weighted candidate cable routes, compute the minimum total cable length needed to connect all points (minimum spanning tree).Easy3Minimum spanning treeGraph+1No attempts yet1s128 MBJudgeable
Modulo SolitaireGiven a modulus m, up to 10 affine maps, and a start s0, find the fewest moves to reach 0.Easy3BFSGraphNo attempts yet1s128 MBJudgeable
Choose Your Own AdventureEach page is a node with two outgoing choices or a terminal ending; print the unique path from page 1 to the single HAPPY ending.Easy3GraphDFS+2No attempts yet1s128 MBJudgeable
The Great SaharaOn a fixed 54-triangle hex board, decide whether player one can move one pyramid to trap an opponent's pyramid immediately.Easy3SimulationImplementation+2No attempts yet1s128 MBJudgeable
Number of IslandsGiven a grid of land and sea cells with 8-directional adjacency, count the connected land components.Easy3GraphDFS+2No attempts yet1s128 MBJudgeable
Red and BlackCount how many black tiles are reachable from a start tile in a small grid by moving up, down, left, and right.Easy3DFSGraph+1No attempts yet1s128 MBJudgeable
Car TroubleGiven a directed graph of street ids with ring road 0, report streets that cannot reach 0 and streets that 0 cannot reach, preserving input order.Easy3GraphDFS+2No attempts yet1s128 MBJudgeable
Connected or Not ConnectedGiven a graph with n sites and k edges, decide whether every site can reach every other.Easy3GraphDFS+1No attempts yet1s128 MBJudgeable
Mining MapsParse each graph block, count distinct node names, and count distinct undirected tunnels including self-loops.Easy3Hash mapGraph+2No attempts yet1s128 MBJudgeable
Asteroid FieldFind the minimum number of moves from the top-left cell to the bottom-right cell in a grid, avoiding asteroid cells.Easy3BFSGraph+1No attempts yet1s128 MBJudgeable
Breed AssignmentCount the breed assignments for N cows under same/different constraints, or report 0 if they conflict.Easy3GraphBacktracking+2No attempts yet1s128 MBJudgeable
Daisy Chains in the FieldGiven an undirected graph of cows joined by ropes, list in ascending order every cow that cannot reach cow 1, or print 0 if all cows are connected to it.Easy3GraphDFS+2No attempts yet1s128 MBJudgeable
Parcel DeliveryGiven a weighted undirected graph, find the minimum total edge weight along a path from barn 1 to barn N.Easy3Shortest pathGraphNo attempts yet1s128 MBJudgeable
3D Space ExplorationCount connected groups of '*' blocks in an N x N x N grid, where blocks connect only across shared faces.Easy3GraphDFS+2No 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
Invasion of the MilkweedSpread milkweed from a start cell to all eight neighbors each week and report the week it covers the last non-boulder cell.Easy3BFSGraph+2No attempts yet1s128 MBJudgeable
Feeding TimeGiven a W by H grid of grass and rock, find the size of the largest connected grass region using 8-directional adjacency.Easy3DFSBFS+2No attempts yet1s128 MBJudgeable
Wheel RotationGiven N-1 belts that chain N pulleys from pulley 1, with each belt either straight (same direction) or crossed (reversed), find the rotation direction of pulley N.Easy3GraphDFS+2No attempts yet1s128 MBJudgeable
Bad GrassCount connected components of nonzero cells in a grid, where two cells connect if they touch horizontally, vertically, or diagonally.Easy3GraphDFS+2No attempts yet1s128 MBJudgeable
Best GrassCount clumps of # cells in a grid, where each clump is one cell or two orthogonally adjacent cells and different clumps never touch.Easy3ArraySimulation+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
Shipping RoutesGiven warehouses and bidirectional shipping legs, answer each request with the cheapest cost, which is shipment size times the minimum number of legs times 100, or report no route.Easy3GraphBFS+2No 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
SurfingParse links out of HTML pages, print each link, then answer reachability queries between pages.Easy3GraphDFS+1No attempts yet1s128 MBJudgeable
Making shortest-path test dataPrint the prescribed shortest-path test graph: a chain with a computed number of self-loops at vertex 0, plus Q queries from V-1 to 0.Easy3ImplementationGraph+2No attempts yet1s128 MBJudgeable
Graph that exceeds Dijkstra's counterPrint the exact constructed directed weighted graph that makes Floyd-Warshall finish while the lazy-deletion Dijkstra times out on one query.Easy3GraphShortest path+1No attempts yet1s128 MBJudgeable
Attendance CheckGiven a permutation forming one cycle, find the student who responds last when the calling chain starts at student k.Easy3ImplementationSimulation+2No attempts yet1s1024 MBJudgeable
Social Networking ApplicationGiven a friendship graph, answer queries asking whether two users lie in the same connected component.Easy3Union-findGraph+2No attempts yet1s128 MBJudgeable
Subdivision of KingdomSplit n towns (n even, n <= 26) into two halves of equal size so that the number of roads crossing between the halves is minimized.Easy3Brute forceBit manipulation+1No 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
The DanceFind the largest number of boy-girl pairs that can dance at once when each boy may only invite a girl he knows.Easy3GraphDFSNo attempts yet1s128 MBJudgeable
Network InvestmentFind the tree edge whose removal maximizes the product of the two component sizes.Easy3DFSTreeNo attempts yet1s128 MBJudgeable
The HareFind the fewest knight jumps from the start cell to the burrow on a grid with blocked cells, or output NIE when unreachable.Easy3BFSGraph+1No attempts yet1s512 MBJudgeable
Rectangle ColoringCount the groups of overlapping rectangles where touching edges count as overlap and each group gets one color.Easy3Union-findGeometryNo attempts yet1s128 MBJudgeable
The SuspectsCount every student connected to student 0 through shared groups, since one suspect makes each joined group suspect.Easy3Union-findNo attempts yet1s128 MBJudgeable
Hauling materialCompute the cheapest directed route from the start to the destination for each road network.Easy3Shortest pathGraphNo attempts yet1s128 MBJudgeable
The Prague LinkCompute the largest shortest-path distance over all pairs of posts, or report that the network is disconnected.Easy3Shortest pathGraphNo attempts yet1s128 MBJudgeable
Robot in a MazeFind the fewest up, down, left, or right moves from S to any G in each grid maze, or report that no exit exists.Easy3BFSMatrixNo attempts yet1s128 MBJudgeable
CrankCount the boundary rooftops that can reach the boss by moving only to equal or lower adjacent buildings.Easy3BFSGraph+1No attempts yet1s128 MBJudgeable
Strahler orderGiven a river DAG, compute its Strahler order at node M by processing nodes in topological order.Easy3Topological sortGraphNo attempts yet1s128 MBJudgeable
Who you knowFind the cheapest chain of introductions from politician 0 to politician M-1 in an undirected graph with edge weights 1 to 4, printing -1 when unreachable.Easy3Shortest pathGraphNo attempts yet1s128 MBJudgeable
Rainforest CanopyCount the groups of 1s connected through all eight neighbours in each square binary image.Easy3DFSGraph+1No attempts yet1s128 MBJudgeable
Bi-coloringCount the two-colorings where every edge joins different colors, or report -1 when the graph is not bipartite.Easy3BFSGraphNo 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
Knight MovesFind the fewest knight moves from K to X on a grid with blocked squares, or output -1 when unreachable.Easy3BFSMatrixNo attempts yet1s128 MBJudgeable
Elephant ShowCount the yellow tiles reachable from the elephant start by moving up, down, left, or right.Easy3DFSMatrixNo 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
Mystery Graph ColoringColor vertices 0 to V-1 with the smallest color unused by colored neighbors, then print the color count, the colors, and the loop counter.Easy3GraphGreedy+1No attempts yet1s128 MBJudgeable
Complete Binary TreeReconstruct each level of a complete binary tree from its inorder visit order.Easy3TreeRecursionNo attempts yet1s128 MBJudgeable
Asteroids!Find the shortest 6-directional path through an N by N by N grid with blocked cells, or report that no route exists.Easy3BFSMatrixNo attempts yet1s128 MBJudgeable
Color WeaknessCount connected R, G, B regions in an N by N grid twice, once normally and once with R and G merged.Easy3BFSMatrixNo attempts yet1s128 MBJudgeable
Balance ScaleGiven pairwise heavier-than results, count for each object how many others have no implied comparison.Easy3GraphDFSNo attempts yet1s256 MBJudgeable
Minion WalkMark every grid cell reachable from the top-left corner, print the room as an ASCII table, and report whether the bottom-right cell is reachable.Easy3BFSMatrix+1No attempts yet1s256 MBJudgeable
Neighborhoods in GraphsCount the distinct vertices at distance 1 or 2 from a query vertex in an undirected graph, excluding the vertex itself.Easy3BFSGraphNo attempts yet1s256 MBJudgeable
The Trojan HorseMark all cells visited by patrol routes on an h by w grid, then count 4-connected unvisited regions with at least s cells.Easy3DFSSimulation+1No attempts yet1s256 MBJudgeable
Counting Communication GroupsCount the connected groups of camps whose circular communication areas touch or overlap.Easy3Union-findGeometryNo attempts yet8s256 MBJudgeable
Corn mazeStarting from the single entrance on the border, move through open cells in four directions and report the farthest shortest distance.Easy3BFSMatrixNo attempts yet1s256 MBJudgeable
Algorist ClubThe program gives each unfamiliar pair in input order the lowest free of max-degree-plus-one slots and prints zeros when a pair fits nowhere.Easy3SimulationGraphNo attempts yet1s256 MBJudgeable
Permutation CyclesCount the disjoint directed cycles in the permutation given in each test case.Easy3GraphDFSNo attempts yet1s256 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
DOM TV ChannelsPensioners switch the TV from each hated channel to the youngest hater's favorite, and you count switches until the channel stabilizes or repeats.Easy3GraphSimulationNo attempts yet1s256 MBJudgeable
Cable CleanupFor each test case, keep N minus 1 of the M cables that join N computers into one network and print how many cables go.Easy3GraphMathNo attempts yet1s256 MBJudgeable
Legacy CodeCount methods unreachable from any PROGRAM method given each method and its direct callers.Easy3GraphBFSNo attempts yet1s256 MBJudgeable