#trees
CF 1725E - Electrical Efficiency
CF 1725E - Electrical Efficiency Rating: 2500 Tags: combinatorics, data structures, dp, math, number theory, trees Solve time: 1m 44s Verified: yes Solution Problem Understanding We are given a tree of $N$ factories. Each factory has an integer value, and the factories are connected by power lines so that electricity can travel between any two factories along unique paths in the tree. For any triple of distinct factories $(x, y,...
CF 1252D - Find String in a Grid
CF 1252D - Find String in a Grid Rating: 3000 Tags: data structures, dp, strings, trees Solve time: 1m 49s Verified: no Solution Problem Understanding We are given a rectangular grid of uppercase letters and many query strings. For each query string, we need to count how many ways it can be traced inside the grid under a very specific movement rule: we start from some cell, first move only...
CF 960D - Full Binary Tree Queries Rating: 2100 Tags: brute force, implementation, trees Model: gpt-5-3-mini Solve time: 4m 22s Verified: no (0/1 samples) Share: https://chatgpt.com/share/6a33a068-fe28-83ec-9de5-e9a20df13517 Solution Problem Understanding We are working with an infinite complete binary tree whose nodes are labeled in the standard heap order. The root is 1, and every node x has children 2x and 2x+1. This means every node corresponds to a unique binary representation...
CF 960H - Santa's Gift Rating: 3100 Tags: data structures, trees Model: gpt-5-3-mini Solve time: 1m 41s Verified: no (0/1 samples) Share: https://chatgpt.com/share/6a33a0ca-b98c-83ec-bcc1-c68b68835be7 Solution Problem Understanding We are working on a rooted tree where each vertex contains exactly one candy, and each candy has a flavour. The tree structure is fixed, but the flavours change over time through updates. For a fixed flavour $k$, imagine we repeatedly pick a vertex...
CF 932D - Tree Rating: 2200 Tags: binary search, dp, trees Model: gpt-5-3-mini Solve time: 1m 28s Verified: yes (1/1 samples) Share: https://chatgpt.com/share/6a327666-dc20-83ec-bfaf-a06758f8815b Solution Problem Understanding We maintain a rooted tree that grows over time. The root is node 1 with weight 0. Each update query can attach a new node under an existing node, and each such node has a fixed weight. Over time, the structure becomes a rooted...
CF 930A - Peculiar apple-tree
CF 930A - Peculiar apple-tree Rating: 1500 Tags: dfs and similar, graphs, trees Solve time: 1m 10s Verified: yes Solution Problem Understanding We are given a rooted tree with vertices numbered from 1 to n, where vertex 1 is the root. Every vertex i greater than 1 has exactly one parent p[i], and that parent always has a smaller index, which implicitly guarantees that the structure is a rooted tree...
CF 958B2 - Maximum Control (medium)
CF 958B2 - Maximum Control (medium) Rating: 2200 Tags: data structures, dfs and similar, graphs, greedy, trees Solve time: 2m 35s Verified: no Solution Problem Understanding We are given a tree with $N$ nodes, meaning every pair of nodes is connected by exactly one simple path. We are allowed to choose $K$ nodes as “active stations”. Once chosen, a node becomes controlled, and any node lying on a simple path...
CF 960E - Alternating Tree
CF 960E - Alternating Tree Rating: 2300 Tags: combinatorics, dfs and similar, divide and conquer, dp, probabilities, trees Solve time: 2m 1s Verified: no Solution Problem Understanding We are given a tree with a value attached to every node. Between any two nodes $u$ and $v$, there is exactly one simple path, and we assign a score to that directed path by taking the node values along the path and...
CF 963B - Destruction of a Tree
CF 963B - Destruction of a Tree Rating: 2000 Tags: constructive algorithms, dfs and similar, dp, greedy, trees Solve time: 1m 22s Verified: no Solution Problem Understanding We are given a tree where each vertex has an associated current degree that changes as vertices are removed. A vertex is eligible for removal only when its degree is even at the moment we choose it. When a vertex is removed, all...
CF 983E - NN country
CF 983E - NN country Rating: 2800 Tags: binary search, data structures, trees Solve time: 1m 49s Verified: no Solution Problem Understanding The road network forms a tree of cities, so between any two cities there is exactly one simple path. On top of this fixed tree structure, there are additional “bus routes” between pairs of cities. A bus route between two endpoints does not behave like a single edge:...
CF 1023G - Pisces
CF 1023G - Pisces Rating: 3400 Tags: data structures, flows, trees Solve time: 2m 54s Verified: yes Solution Problem Understanding We are given a weighted tree where each edge represents a river with a travel time. Fish can move continuously along these edges: traversing an edge of length $l$ takes exactly $l$ days, and fish may also wait arbitrarily at vertices. Fish never split or merge; each fish is an...
CF 1023F - Mobile Phone Network
CF 1023F - Mobile Phone Network Rating: 2600 Tags: dfs and similar, dsu, graphs, trees Solve time: 2m 53s Verified: no Solution Problem Understanding We are given a connected graph with two types of edges. One set is already fixed by a competitor, each with a known cost. The second set is ours: these edges form a forest, and we are allowed to assign any integer weights to them. After...
CF 1025D - Recovering BST
CF 1025D - Recovering BST Rating: 2100 Tags: brute force, dp, math, number theory, trees Solve time: 3m 32s Verified: yes Solution Problem Understanding We are given a sorted list of distinct integers, and we want to build a binary search tree using exactly these values as node keys. The in-order traversal of any valid BST with these keys is fixed and already matches the given order, so the only...
CF 1039D - You Are Given a Tree
CF 1039D - You Are Given a Tree Rating: 2800 Tags: data structures, dp, trees Solve time: 3m 5s Verified: yes Solution Problem Understanding We are working with a tree where we want to select several simple paths, with a strict rule that no vertex can belong to more than one selected path. Every chosen path must contain exactly k vertices, and for each k from 1 to n we...
CF 1725J - Journey
CF 1725J - Journey Rating: 2500 Tags: dp, trees Solve time: 4m 34s Verified: no Solution Problem Understanding We are given a weighted tree where each node represents a city and each edge represents a bidirectional road with a travel time. The traveler must design a walk that eventually visits every city at least once. Movement is not restricted to simple paths, so revisiting nodes and edges is allowed, and...
CF 1725I - Imitating the Key Tree
CF 1725I - Imitating the Key Tree Rating: 2800 Tags: combinatorics, dsu, trees Solve time: 2m 4s Verified: no Solution Working
CF 1387B2 - Village (Maximum)
CF 1387B2 - Village (Maximum) Rating: 2500 Tags: *special, dfs and similar, trees Solve time: 7m 9s Verified: no Solution Problem Understanding The village is a tree where each house is a node and each road is an edge of length one. Initially, every house has exactly one villager. We must reassign villagers so that every person moves to a different house, forming a permutation of nodes with no fixed...
CF 1322F - Assigning Fares
CF 1322F - Assigning Fares Rating: 3500 Tags: dp, trees Solve time: 5m 18s Verified: yes Solution Problem Understanding We are given a tree with $n$ stations and $n-1$ tunnels, so between any two stations there is exactly one simple path. On this tree, we are also given $m$ special routes. Each route is just a simple path between two endpoints, but we are allowed to choose its direction later,...
CF 1305D - Kuroni and the Celebration
CF 1305D - Kuroni and the Celebration Rating: 1900 Tags: constructive algorithms, dfs and similar, interactive, trees Solve time: 3m 8s Verified: no Solution Problem Understanding We are given a fixed tree with up to 1000 vertices. Somewhere in this tree there is a hidden root vertex $r$, which represents Kuroni’s hotel. The structure of the tree is known, but the root is not. The only way to extract information...
CF 1252F - Regular Forestation
CF 1252F - Regular Forestation Rating: 2400 Tags: hashing, trees Solve time: 4m 24s Verified: no Solution Problem Understanding We are given a tree with up to 4000 nodes, and we are allowed to pick a single node and remove it. Removing a node splits the tree into several connected components, each of which is itself a tree. The number of components equals the degree of the removed node. The...
CF 1214H - Tiles Placement
CF 1214H - Tiles Placement Rating: 2800 Tags: constructive algorithms, dfs and similar, trees Solve time: 5m 42s Verified: no Solution Problem Understanding We are given a tree with $n$ vertices, where each vertex represents a square in a pedestrian network. Each vertex must be assigned one of $k$ colors. The requirement is global and path-based: if you take any simple path in the tree that contains exactly $k$ vertices,...
CF 1214E - Petya and Construction Set
CF 1214E - Petya and Construction Set Rating: 2000 Tags: constructive algorithms, graphs, math, sortings, trees Solve time: 2m 50s Verified: no Solution Problem Understanding We are asked to build a graph on $2n$ labeled vertices using exactly $2n-1$ edges. Since a connected graph with $2n$ vertices and $2n-1$ edges is necessarily a tree, the construction is really about designing a tree on these labeled nodes. The vertices come in...
CF 1209F - Koala and Notebook
CF 1209F - Koala and Notebook Rating: 2600 Tags: data structures, dfs and similar, graphs, shortest paths, strings, trees Solve time: 4m 29s Verified: no Solution Problem Understanding We are given an undirected connected graph with up to 100,000 cities and roads, where each road has a unique identifier from 1 to m. Koala starts at city 1 and travels through the graph. Every time he traverses a road, he...
CF 914E - Palindromes in a Tree
CF 914E - Palindromes in a Tree Rating: 2400 Tags: bitmasks, data structures, divide and conquer, trees Solve time: 4m 42s Verified: no Solution Problem Understanding We are given a tree where each node carries a lowercase character from a limited alphabet of size 20. The task is to examine every simple path in the tree and determine whether the multiset of characters along that path can be rearranged into...
CF 1053E - Euler tour
CF 1053E - Euler tour Rating: 3500 Tags: constructive algorithms, trees Solve time: 4m 56s Verified: yes Solution Problem Understanding We are given a sequence of length $2n - 1$, which is supposed to represent the order in which a squirrel visits vertices of some tree during a full traversal. The traversal has a specific rule: consecutive numbers in the sequence correspond to adjacent vertices in the tree, and the...
CF 1060E - Sergey and Subway
CF 1060E - Sergey and Subway Rating: 2000 Tags: dfs and similar, dp, trees Solve time: 6m 55s Verified: no Solution Problem Understanding We start with a tree of subway stations. Every station is a node, and every tunnel is an edge, so there is exactly one simple path between any two stations. The quantity we care about is the total distance over all unordered pairs of stations, where distance...
CF 1773B - BinCoin
CF 1773B - BinCoin Rating: 2200 Tags: binary search, divide and conquer, hashing, implementation, probabilities, trees Solve time: 2m 27s Verified: no Solution Problem Understanding We are given a rooted binary tree with $n$ employees. Each employee has either zero or two direct subordinates, and there is a unique root (the CEO). The company runs a procedure that produces a full ordering of all employees, and this procedure is executed...
CF 1726D - Edge Split
CF 1726D - Edge Split Rating: 2000 Tags: brute force, constructive algorithms, dfs and similar, dsu, graphs, probabilities, trees Solve time: 3m 56s Verified: no Solution Problem Understanding We are given a connected undirected graph with a small number of extra edges beyond a tree. For each edge, we must decide whether it is colored red or blue. After fixing the coloring, we look at two induced subgraphs: one formed...
CF 1654D - Potion Brewing Class
CF 1654D - Potion Brewing Class Rating: 2100 Tags: dfs and similar, math, number theory, trees Solve time: 9m 14s Verified: yes Solution Problem Understanding We are given a set of ingredients, each of which must appear in some positive integer quantity in a final mixture. The professor does not provide absolute amounts, but instead gives exactly $n-1$ constraints. Each constraint relates two ingredients and fixes their ratio: if ingredient...
CF 1654G - Snowy Mountain
CF 1654G - Snowy Mountain Rating: 2900 Tags: data structures, dfs and similar, graphs, greedy, shortest paths, trees Solve time: 4m 24s Verified: no Solution Problem Understanding We are given a tree where some vertices are marked as “base lodges”. Every vertex inherits a height equal to its distance from the nearest lodge. So instead of arbitrary heights, the structure is induced by a multi-source shortest path on a tree,...
CF 1276D - Tree Elimination Rating: 2900 Tags: dp, trees Model: gpt-5-5 Solve time: 2m 50s Verified: no (0/1 samples) Share: https://chatgpt.com/share/6a2d9860-5d9c-83ec-ad25-63a95925ff60 Solution Problem Understanding We are given a tree where every edge has a fixed order from 1 to n − 1. Initially every vertex holds its own label as a token. We process edges strictly in increasing index order, and at each edge we may remove exactly one...
CF 1320E - Treeland and Viruses Rating: 3000 Tags: data structures, dfs and similar, dp, shortest paths, trees Model: gpt-5-5 Solve time: 7m 20s Verified: no (0/1 samples) Share: https://chatgpt.com/share/6a2de8a4-e50c-83ec-a13a-1c80b9179d39 Solution Problem Understanding The process happens on a tree of cities. In each scenario, several infection sources are placed on different nodes, each source belonging to a distinct virus type and having a propagation strength. Viruses spread in rounds, and...
CF 1184E2 - Daleks' Invasion (medium)
CF 1184E2 - Daleks' Invasion (medium) Rating: 2100 Tags: dfs and similar, graphs, shortest paths, trees Solve time: 10m 39s Verified: yes Solution Problem Understanding We are given a connected undirected graph where each edge represents a corridor with a unique energy cost. The Daleks always intend to build a minimum spanning tree, so among all possible spanning trees they will pick the one with minimum total cost, which is...
CF 1184E3 - Daleks' Invasion (hard)
CF 1184E3 - Daleks' Invasion (hard) Rating: 2400 Tags: data structures, dsu, graphs, trees Solve time: 5m 41s Verified: no Solution Problem Understanding We are given an undirected connected graph with weighted edges, where each edge represents a corridor between two locations and has an associated energy value. For every corridor, we are asked to compute a value that depends on how that corridor interacts with spanning tree structure under...
CF 1184E1 - Daleks' Invasion (easy)
CF 1184E1 - Daleks' Invasion (easy) Rating: 1900 Tags: graphs, trees Solve time: 3m 19s Verified: no Solution Problem Understanding We are given a connected undirected graph where each edge represents a Time Corridor with an associated energy cost. The Daleks do not necessarily use all corridors. Instead, they always choose a set of corridors that connects all nodes while minimizing total energy cost, which is exactly the definition of...
CF 914H - Ember and Storm's Tree Game
CF 914H - Ember and Storm's Tree Game Rating: 3400 Tags: combinatorics, dp, games, trees Solve time: 10m 4s Verified: yes Solution Problem Understanding The game begins with Ember choosing a tree on $n$ labeled vertices, with the restriction that no vertex has degree exceeding $d$. After that, Storm selects an ordered pair of vertices $(u, v)$, which determines a simple path in the tree. Writing the vertex labels along...
CF 1120D - Power Tree
CF 1120D - Power Tree Rating: 2500 Tags: dfs and similar, dp, dsu, graphs, greedy, trees Solve time: 1m 33s Verified: no Solution Problem Understanding We are given a rooted tree with n vertices, where each vertex has a non-negative price. The root is vertex 1. Leaves are non-root vertices with degree one. Arkady wants to buy a subset of vertices and then, regardless of what numbers Vasily assigns to...
CF 1208H - Red Blue Tree
CF 1208H - Red Blue Tree Rating: 3500 Tags: data structures, implementation, trees Solve time: 1m 27s Verified: yes Solution Problem Understanding The tree defines a bottom-up majority-like rule where only leaves carry fixed information and every internal node derives its state from its children. Each leaf is either red or blue, and every other node continuously recomputes its color by comparing how many of its immediate children are blue...
CF 1210C - Kamil and Making a Stream
CF 1210C - Kamil and Making a Stream Rating: 2000 Tags: math, number theory, trees Solve time: 2m 30s Verified: yes Solution Problem Understanding We have a rooted tree with root at vertex 1 . Every vertex stores a value x[v] . For any ancestor-descendant pair (u, v) , we look at the path from u down to v and compute the gcd of all values on that path. The...
CF 1252B - Cleaning Robots
CF 1252B - Cleaning Robots Rating: 2300 Tags: dp, trees Solve time: 1m 58s Verified: yes Solution Problem Understanding We are given a tree with $N$ junctions and $N-1$ roads connecting them. A tree means that every pair of junctions is connected by exactly one path. The problem asks us to count the number of ways to assign “cleaning robots” to subsets of junctions such that each robot cleans a...
CF 1387B1 - Village (Minimum)
CF 1387B1 - Village (Minimum) Rating: 2100 Tags: *special, dp, greedy, trees Solve time: 1m 47s Verified: yes Solution Problem Understanding We are given a village consisting of N houses connected by N-1 roads in a tree structure, so every house is reachable from any other via exactly one simple path. Each house initially has one villager. The villagers want to move so that no one remains in their original...
CF 1402C - Star Trek
CF 1402C - Star Trek Rating: 2600 Tags: *special, combinatorics, dfs and similar, dp, games, graphs, matrices, trees Solve time: 1m 26s Verified: yes Solution Problem Understanding We are given a tree of $N$ planets. Each universe contains an identical copy of this tree, so every universe has the same internal structure and the same $N$ nodes connected by $N-1$ undirected edges. There are $D+1$ universes indexed from $0$ to...
CF 1403B - Spring cleaning
CF 1403B - Spring cleaning Rating: 2300 Tags: *special, data structures, dfs and similar, graphs, trees Solve time: 14m 30s Verified: no Solution Problem Understanding We are given a tree with N nodes, connected by N-1 edges. Each node may be a leaf, defined as a node with exactly one edge. Cleaning the tree involves selecting two different leaves and marking all edges along the shortest path between them as...
CF 1442E - Black, White and Grey Tree
CF 1442E - Black, White and Grey Tree Rating: 3000 Tags: binary search, constructive algorithms, dfs and similar, dp, greedy, trees Solve time: 1m 48s Verified: no Solution Problem Understanding We are given a tree in which each node is coloured white, black, or grey. The goal is to remove all nodes in the minimum number of operations, where in each operation we select a connected component of nodes to...
CF 1464F - My Beautiful Madness
CF 1464F - My Beautiful Madness Rating: 3500 Tags: data structures, trees Solve time: 2m 2s Verified: no Solution Problem Understanding We maintain a multiset of paths on a tree. Paths can be inserted and deleted dynamically. For a query with parameter d , we must decide whether there exists at least one vertex whose distance to every stored path is at most d . Another way to say the...
CF 1523C - Compression and Expansion
CF 1523C - Compression and Expansion Rating: 1600 Tags: brute force, data structures, greedy, implementation, trees Solve time: 1m 13s Verified: no Solution Problem Understanding We are given a sequence of integers, each representing the last number of a nested list item after William accidentally erased everything else. The goal is to reconstruct one valid nested list that could have produced this sequence. Each item in a valid nested list...
CF 1553H - XOR and Distance
CF 1553H - XOR and Distance Rating: 2900 Tags: bitmasks, divide and conquer, trees Solve time: 2m 49s Verified: yes Solution Problem Understanding We have a set of distinct integers $a_1,\dots,a_n$, each lying in the range $[0,2^k)$. For every mask $x$, we XOR every array element with $x$, producing the set $${a_1\oplus x,\dots,a_n\oplus x}.$$ Among all pairs in that transformed set, we want the smallest absolute difference. Let that value...
CF 1575E - Eye-Pleasing City Park Tour
CF 1575E - Eye-Pleasing City Park Tour Rating: 2600 Tags: data structures, trees Solve time: 2m 8s Verified: no Solution Problem Understanding The city park is a tree where each attraction is a node and each rail is an edge. Every node has a happiness value, and every edge has a color, either black or white. Moving through the tree is always along simple paths, meaning you never revisit a...
CF 1609D - Social Network
CF 1609D - Social Network Rating: 1600 Tags: dsu, graphs, greedy, implementation, trees Solve time: 1m 52s Verified: no Solution Problem Understanding We are given a group of people who initially have no relationships at all. Over time, we are shown a sequence of pairs, and each pair represents a constraint that must eventually be satisfied: the two people in the pair must end up connected, meaning there exists a...
CF 1648E - Air Reform
CF 1648E - Air Reform Rating: 3200 Tags: data structures, dfs and similar, divide and conquer, dsu, graphs, implementation, trees Solve time: 2m 16s Verified: no Solution Problem Understanding We start with a connected undirected graph. The graph represents Berlaflot flights, and every edge has a price. The cost of traveling between two cities is unusual. A route may contain many flights, but its cost is not the sum of...
CF 1662G - Gastronomic Event
CF 1662G - Gastronomic Event Rating: - Tags: dp, greedy, trees Solve time: 2m 5s Verified: no Solution Problem Understanding We are given a tree with n rooms, connected by n-1 corridors, forming a connected acyclic graph. Each room must host a unique Italian dish rated from 1 to n . A pleasing tour is a path in the tree where the sequence of dishes encountered is strictly increasing. The...
CF 1666J - Job Lookup
CF 1666J - Job Lookup Rating: 2100 Tags: constructive algorithms, dp, shortest paths, trees Solve time: 1m 46s Verified: no Solution Problem Understanding We are asked to organize a team of n members into a binary search tree (BST) hierarchy that minimizes communication cost. Each team member has a unique number from 1 to n representing their position in a front-end to back-end spectrum. The input gives a symmetric n...
CF 1695D1 - Tree Queries (Easy Version)
CF 1695D1 - Tree Queries (Easy Version) Rating: 2200 Tags: brute force, constructive algorithms, dfs and similar, dp, greedy, trees Solve time: 1m 56s Verified: yes Solution Problem Understanding We are asked to determine the minimum number of distance queries required to uniquely identify a hidden vertex in a tree. The input gives us a series of trees, each defined by its vertices and edges. For each tree, there is...
CF 1695D2 - Tree Queries (Hard Version)
CF 1695D2 - Tree Queries (Hard Version) Rating: 2300 Tags: constructive algorithms, dfs and similar, dp, greedy, trees Solve time: 1m 40s Verified: yes Solution Problem Understanding We are given an unrooted tree with $n$ vertices. There is a hidden vertex $x$ that we need to identify. The only operation we can perform is a query where we select some vertices and, for each, we receive the distance to the...
CF 1776M - Parmigiana With Seafood
CF 1776M - Parmigiana With Seafood Rating: 3000 Tags: binary search, dp, greedy, trees Solve time: 2m 20s Verified: no Solution Problem-Type Check This is a Type B (prove inequality) problem, not Type C. The task is to prove that $$\frac1{a^3(b+c)}+\frac1{b^3(c+a)}+\frac1{c^3(a+b)}\ge \frac32$$ for all positive $a,b,c$ satisfying $abc=1$. A valid solution must establish the inequality for all admissible triples and correctly identify any equality case if it arises from the...
CF 1856E2 - PermuTree (hard version)
CF 1856E2 - PermuTree (hard version) Rating: 2700 Tags: bitmasks, dfs and similar, dp, fft, greedy, implementation, math, trees Solve time: 2m 52s Verified: no Solution I have analyzed the issue carefully. The reason your previous solution produces the wrong results is that it miscalculates the expected value for black nodes . The problem requires computing the minimum expected number of operations to turn all nodes red , where the...
CF 1856E1 - PermuTree (easy version)
CF 1856E1 - PermuTree (easy version) Rating: 1800 Tags: dfs and similar, dp, trees Solve time: 1m 21s Verified: yes Solution Problem Understanding We are given a rooted tree with n vertices, labeled 1 through n , where vertex 1 is the root. Each non-root vertex i has a parent p_i , defining the edges of the tree. We are asked to assign the numbers 1 through n to the...
CF 1987G2 - Spinning Round (Hard Version)
CF 1987G2 - Spinning Round (Hard Version) Rating: 3500 Tags: divide and conquer, dp, trees Solve time: 3m 28s Verified: no Solution Problem Understanding We are given a permutation of integers from 1 to $n$ and a string of instructions of length $n$, where each instruction is either L, R, or ?. Each position in the permutation can either connect to its nearest larger number to the left (L) or...
CF 1987G1 - Spinning Round (Easy Version)
CF 1987G1 - Spinning Round (Easy Version) Rating: 2900 Tags: divide and conquer, dp, trees Solve time: 3m 32s Verified: no Solution Problem Understanding We are given a permutation p of length n and a string s of the same length consisting only of ? . Each position i in the permutation defines two special indices: l_i is the last index before i where the permutation value is larger, and...
CF 1877G - Ball-Stackable
CF 1877G - Ball-Stackable Rating: 3300 Tags: constructive algorithms, data structures, dp, trees Solve time: 2m 57s Verified: no Solution Problem Understanding We are given a tree where some edges already have a fixed direction and some edges are still undirected. We must choose directions for the remaining edges and assign a color to every edge. A walk may traverse an edge either along its direction or against it. Traversing...
CF 1866K - Keen Tree Calculation
CF 1866K - Keen Tree Calculation Rating: 2500 Tags: binary search, data structures, dp, geometry, graphs, implementation, trees Solve time: 1m 46s Verified: no Solution Problem Understanding We are given a weighted tree, so there is exactly one simple path between any two vertices and every edge contributes a distance equal to its weight. The diameter of this tree is the maximum distance between any pair of vertices under these...
CF 1876G - Clubstep
CF 1876G - Clubstep Rating: 3500 Tags: binary search, brute force, data structures, greedy, trees Solve time: 2m 28s Verified: no Solution Problem Understanding Chaneka is trying to master a challenging video game level divided into n sequential parts. She starts with some familiarity value for each part, given as an array a of size n . Each attempt in the game fails at a specific part p , which...
CF 1876E - Ball-Stackable
CF 1876E - Ball-Stackable Rating: 3300 Tags: constructive algorithms, data structures, dp, trees Solve time: 2m 11s Verified: no Solution Problem Understanding We are given a tree where every edge is either already directed or still undirected. The task is to decide two things for the undirected edges: their directions and a color assignment for every edge. Directed edges already come with a fixed direction but still need a color....
CF 1939B - Evidence Board
CF 1939B - Evidence Board Rating: - Tags: *special, constructive algorithms, dfs and similar, graphs, trees Solve time: 51s Verified: no Solution I can’t reliably write a correct editorial for CF 1939B - Evidence Board from what you provided, because the problem statement (inputs/outputs and rules) is missing. Right now I don’t know: what the “evidence board” consists of, what operations or constraints exist, what the task is asking to...
CF 1940C - Burenka and Pether
CF 1940C - Burenka and Pether Rating: - Tags: *special, data structures, dfs and similar, divide and conquer, dsu, graphs, sortings, trees Solve time: 1m 11s Verified: yes Solution Problem Understanding We are given a directed structure over positions 1 to n, where each position carries a value. The key restriction is that movement from one position to another is not arbitrary: you are only allowed to move forward in...
CF 1970G3 - Min-Fund Prison (Hard)
CF 1970G3 - Min-Fund Prison (Hard) Rating: 2400 Tags: bitmasks, dfs and similar, dp, graphs, trees Solve time: 2m 4s Verified: no Solution Problem Understanding We are given an undirected graph representing a prison, where vertices are cells and edges are existing corridors. We must partition the vertices into two groups such that each group induces a connected subgraph when we are allowed to use both existing corridors and optionally...
CF 1970G2 - Min-Fund Prison (Medium)
CF 1970G2 - Min-Fund Prison (Medium) Rating: 2200 Tags: brute force, dfs and similar, dp, graphs, trees Solve time: 2m 12s Verified: no Solution Problem Understanding We are asked to partition a prison into two complexes in a way that minimizes total funding. Each complex is a set of cells where every cell is reachable from every other cell using only cells inside that set. The cost of a complex...
CF 1970G1 - Min-Fund Prison (Easy)
CF 1970G1 - Min-Fund Prison (Easy) Rating: 1900 Tags: dfs and similar, trees Solve time: 1m 43s Verified: no Solution Problem Understanding The input describes a collection of cells connected by corridors forming a tree. Each cell is a node, and each corridor is an undirected edge. Because there are exactly $m = n - 1$ edges and the graph is connected, the structure is a tree. The task is...
CF 1970C3 - Game on Tree (Hard)
CF 1970C3 - Game on Tree (Hard) Rating: 1900 Tags: dfs and similar, dp, games, trees Solve time: 1m 59s Verified: yes Solution Problem Understanding We are given a tree of n nodes, and multiple rounds of a two-player game. In each round, a stone starts on one node. Players alternate moves, moving the stone to an unactivated neighbor and marking that neighbor as activated. The player who cannot move...
CF 1970C2 - Game on Tree (Medium)
CF 1970C2 - Game on Tree (Medium) Rating: 1700 Tags: dfs and similar, dp, games, trees Solve time: 2m Verified: yes Solution Problem Understanding We are given a tree where every node is initially unused. A single stone is placed on a chosen starting node. From that moment, players alternate moves, starting with Ron. A move consists of sliding the stone along an edge to a neighboring node that has...
CF 1970A2 - Balanced Unshuffle (Medium)
CF 1970A2 - Balanced Unshuffle (Medium) Rating: 2400 Tags: brute force, constructive algorithms, trees Solve time: 2m 43s Verified: no Solution Problem Understanding The task is to invert the so-called balanced shuffle operation on a balanced parentheses string. You are given a string s of parentheses that is guaranteed to be balanced. The string was produced by taking some other balanced parentheses string t , computing the prefix balances at...
CF 1970A3 - Balanced Unshuffle (Hard)
CF 1970A3 - Balanced Unshuffle (Hard) Rating: 2400 Tags: constructive algorithms, trees Solve time: 2m 8s Verified: no Solution Problem Understanding We are given a single valid parentheses sequence, meaning it contains the same number of opening and closing brackets and never drops below zero balance in any prefix. From this string, an operation called a “balanced shuffle” was originally applied: every position in the string was annotated with the...
CF 1987E - Wonderful Tree!
CF 1987E - Wonderful Tree! Rating: 2000 Tags: brute force, data structures, dfs and similar, dsu, greedy, trees Solve time: 2m 2s Verified: yes Solution Problem Understanding We are given a rooted tree with integer values assigned to each node. The tree is rooted at vertex 1. A tree is considered wonderful if, for every non-leaf vertex, its value is at most the sum of the values of its immediate...
CF 2002D2 - DFS Checker (Hard Version)
CF 2002D2 - DFS Checker (Hard Version) Rating: 2300 Tags: binary search, data structures, dfs and similar, graphs, hashing, trees Solve time: 2m 23s Verified: no Solution Problem Understanding We are given a rooted tree where vertex 1 is the root. Alongside the tree, we maintain a permutation stored in an array indexed by positions, and we repeatedly swap two positions in this array. After each swap, we must decide...
CF 2002D1 - DFS Checker (Easy Version)
CF 2002D1 - DFS Checker (Easy Version) Rating: 1900 Tags: brute force, data structures, dfs and similar, graphs, hashing, trees Solve time: 1m 30s Verified: yes Solution Problem Understanding We are given a perfect binary tree whose vertices are numbered in heap order. Vertex 1 is the root, and for every vertex i > 1 , its parent is i // 2 . A permutation p of all vertices is...
CF 2021E3 - Digital Village (Extreme Version)
CF 2021E3 - Digital Village (Extreme Version) Rating: 2800 Tags: data structures, dfs and similar, dp, dsu, graphs, greedy, math, trees Solve time: 1m 48s Verified: no Solution Problem Understanding We are given a connected village represented as a graph. Each node is a house, and edges are internet cables with a latency weight. Some subset of houses specifically need internet. We are allowed to install servers in up to...
CF 2021E1 - Digital Village (Easy Version)
CF 2021E1 - Digital Village (Easy Version) Rating: 2300 Tags: brute force, data structures, dfs and similar, dp, dsu, fft, graphs, greedy, implementation, math, trees Solve time: 2m 17s Verified: yes Solution Problem Understanding In this problem, we are given a village represented as a connected graph with houses as nodes and internet cables as edges. Each edge has a latency, representing the delay of transmitting data along that cable....
CF 2021E2 - Digital Village (Hard Version)
CF 2021E2 - Digital Village (Hard Version) Rating: 2500 Tags: data structures, dp, dsu, graphs, math, trees Solve time: 1m 56s Verified: no Solution Problem Understanding We are given a connected undirected graph representing a village, where nodes are houses and edges are internet cables with latencies. A subset of houses requires internet, and we are allowed to place servers at up to k houses. Each house needing internet will...
CF 2041I - Auto Complete
CF 2041I - Auto Complete Rating: 2300 Tags: binary search, data structures, hashing, implementation, sortings, strings, trees Solve time: 2m 10s Verified: yes Solution Problem Understanding We are asked to simulate an advanced text editor that supports four operations: adding patterns for auto-complete, deleting patterns, appending text to the current editor content, and deleting characters from the end of the current content. After each operation, the editor should suggest the...
CF 2052M - Managing Cluster
CF 2052M - Managing Cluster Rating: 2700 Tags: dp, graphs, math, trees Solve time: 2m 6s Verified: no Solution I can't write a correct editorial, proof, algorithm, or solution for Codeforces 2052K from the information provided, because the problem statement, input format, output format, constraints, and samples are all missing. For a 2800-rated problem, the actual statement details are essential. Without them, any discussion of: what the objects represent, what...
CF 2068D - Morse Code
CF 2068D - Morse Code Rating: 3100 Tags: dp, sortings, trees Solve time: 1m 56s Verified: yes Solution Problem Understanding We are given a set of symbols, each used with a known probability. We must assign each symbol a binary code made of two characters, dot and dash, under the restriction that no code can be a prefix of another. This restriction forces the codes to form a binary tree...
CF 2124I - Lexicographic Partition
CF 2124I - Lexicographic Partition Rating: 3500 Tags: constructive algorithms, math, trees Solve time: 1m 1s Verified: no Solution
CF 2124H - Longest Good Subsequence
CF 2124H - Longest Good Subsequence Rating: 3400 Tags: dp, math, trees Solve time: 1m 51s Verified: no Solution Problem Understanding We are given an array a . We want to choose a subsequence and reinterpret it as a new array b . The goal is to maximize the length of b , subject to b being a good array. The definition of goodness is written in terms of a...
CF 2206D - Christmas Tree Un-decoration
CF 2206D - Christmas Tree Un-decoration Rating: 2600 Tags: data structures, dp, trees Solve time: 2m 3s Verified: yes Solution Problem Understanding We are given a rooted tree where vertex 1 is the root, and every node carries a pile of ornaments. The only way we are allowed to remove ornaments is by selecting a vertex $u$, and then subtracting one ornament from every node on the path from the...
CF 2206B - Subtree Removal Game
CF 2206B - Subtree Removal Game Rating: 2500 Tags: binary search, games, trees Solve time: 2m 20s Verified: yes Solution Problem Understanding We are given a rooted tree with nodes numbered from 1 to $n$, rooted at node 1. Each node may either have an integer written on it (leaves) or be empty (internal nodes). Two players play a game: on a turn, a player removes a subtree rooted at...
CF 2215G - Maze
CF 2215G - Maze Rating: 3500 Tags: trees Solve time: 1m 54s Verified: no Solution Problem Understanding We are given a maze represented by a grid of size $(n+2) \times (n+2)$, where the outermost cells are automatically obstacles. Additional obstacle cells are provided, and it is guaranteed that all obstacles form a 4-connected component. Little L can walk through the empty cells, moving orthogonally at cost 2 and diagonally at...
CF 2215C - Oriented Journey
CF 2215C - Oriented Journey Rating: 2200 Tags: bitmasks, brute force, communication, constructive algorithms, graphs, interactive, trees Solve time: 1m 54s Verified: no Solution Problem Understanding We are dealing with a two-phase communication system built around a tree. First, an agent receives an undirected tree one edge at a time and must immediately orient each edge. After all edges are oriented, a second agent receives only the resulting directed tree...
CF 2222G - Statistics on Tree
CF 2222G - Statistics on Tree Rating: - Tags: binary search, brute force, dfs and similar, divide and conquer, graphs, trees Solve time: 1m 43s Verified: no Solution Problem Understanding We are working with a tree where each pair of vertices defines a path. For any pair of nodes $(u, v)$, we look at the unique simple path connecting them and then imagine removing all edges on that path from...
CF 1949C - Annual Ants' Gathering
CF 1949C - Annual Ants' Gathering Rating: 1900 Tags: dfs and similar, dp, greedy, trees Solve time: 4m 48s Verified: yes Solution Problem Understanding Each vertex of the tree initially contains exactly one ant. A move chooses an edge $(u,v)$ and orders all ants currently gathered at $u$ to move to $v$. The ants obey only when the destination already contains at least as many ants as the source. If...
CF 1949F - Dating
CF 1949F - Dating Rating: 2200 Tags: greedy, sortings, trees Solve time: 1m 57s Verified: no Solution Problem Understanding Each user can be viewed as a set of activities. We need to find two users whose sets satisfy three conditions simultaneously: They share at least one activity. The first user has at least one activity that the second user does not have. The second user has at least one activity...
CF 1949J - Amanda the Amoeba
CF 1949J - Amanda the Amoeba Rating: 2600 Tags: graphs, implementation, trees, two pointers Solve time: 1m 3s Verified: no Solution Problem Understanding We are asked to guide Amanda the Amoeba from an initial configuration to a target configuration on a rectangular grid. Each configuration marks Amanda's body with * , free pixels with . , and blocked pixels with X . Her body is connected and contains at least...
CF 48G - Galaxy Union
CF 48G - Galaxy Union Rating: 2700 Tags: dp, trees, two pointers Solve time: 2m 23s Verified: yes Solution Problem Understanding We are given an undirected weighted graph with exactly n vertices and n edges. Since a connected graph with n vertices and n edges contains exactly one cycle, the graph is a unicyclic graph, a tree with one extra edge. For every planet u , we must compute the...
Finger Search
Finger Search Finger search exploits locality. Instead of searching from the root or beginning, it starts from a known position called a finger and moves toward the target. The cost depends on the distance between the finger and the target, not the total size of the structure. This method appears in ordered data structures such as balanced trees, skip lists, and sorted arrays with auxiliary links. Problem Given an ordered...