#dsu
CF 1305G - Kuroni and Antihype
CF 1305G - Kuroni and Antihype Rating: 3500 Tags: bitmasks, brute force, dp, dsu, graphs Solve time: 3m 16s Verified: no Solution Problem Understanding We are given a group of people, each with an associated integer value that can be interpreted as a bitmask. These people can form a network by joining a system where actions produce profit: once someone has joined, they can “invite” a person who has not...
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 1039C - Network Safety
CF 1039C - Network Safety Rating: 2200 Tags: dfs and similar, dsu, graphs, math, sortings Solve time: 8m 24s Verified: yes Solution Problem Understanding We are given a network of servers where each server has an integer label (an encryption key) in a fixed bit range. Some pairs of servers are connected, and a connection is considered safe only if the two endpoints currently hold different values. A virus is...
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 1553G - Common Divisor Graph
CF 1553G - Common Divisor Graph Rating: 2700 Tags: brute force, constructive algorithms, dsu, graphs, hashing, math, number theory Solve time: 3m 40s Verified: yes Solution Problem Understanding We are given a fixed set of nodes, each labeled by a distinct integer. Two nodes are connected if their labels share any prime factor. This means the graph is determined entirely by the prime factorizations of the given numbers. For each...
CF 1553E - Permutation Shift
CF 1553E - Permutation Shift Rating: 2100 Tags: brute force, combinatorics, constructive algorithms, dfs and similar, dsu, graphs, math Solve time: 4m 48s Verified: yes Solution Problem Understanding We start from the identity permutation, which is simply the numbers from 1 to n in order. Someone first rotates this array cyclically to the right by an unknown shift k, and then performs at most m arbitrary swaps of elements. After...
CF 1209D - Cow and Snacks
CF 1209D - Cow and Snacks Rating: 1700 Tags: dfs and similar, dsu, graphs Solve time: 2m 49s Verified: yes Solution Problem Understanding We are given a collection of snack types and a group of guests. Each snack type appears exactly once, so there are $n$ distinct items labeled $1$ to $n$. Each guest has two preferred snack types. Guests arrive in some chosen order, and when a guest arrives,...
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 1726C - Jatayu's Balanced Bracket Sequence
CF 1726C - Jatayu's Balanced Bracket Sequence Rating: 1300 Tags: data structures, dsu, graphs, greedy Solve time: 7m 30s Verified: no Solution Problem Understanding We are given a balanced bracket string of length $2n$. Each position in this string is treated as a vertex in a graph. Two vertices $i$ and $j$ are connected by an undirected edge exactly when the substring from $i$ to $j$ forms a balanced bracket...
CF 1578L - Labyrinth
CF 1578L - Labyrinth Rating: 2400 Tags: binary search, dsu, greedy Solve time: 3m 48s Verified: no Solution Problem Understanding The labyrinth can be seen as a connected weighted graph where rooms are nodes and passages are undirected edges with capacities. Each room also has a one-time “growth value” that increases Lucy’s width if she chooses to eat that room’s candy. Lucy starts at room 1 with some initial width,...
CF 1578B - Building Forest Trails
CF 1578B - Building Forest Trails Rating: 2800 Tags: data structures, dsu Solve time: 4m 26s Verified: yes Solution Problem Understanding We are given a circular arrangement of villages, labeled from 1 to n in clockwise order. Initially there are no connections between any pair of villages. Over time, we receive events of two types: we either add a straight road between two villages on the circle, or we ask...
CF 1556G - Gates to Another World
CF 1556G - Gates to Another World Rating: 3300 Tags: bitmasks, data structures, dsu, two pointers Solve time: 4m 44s Verified: no Solution Problem Understanding We are working on a graph whose vertices are all integers from $0$ to $2^n - 1$. Each vertex represents an $n$-bit binary string, and there is an undirected edge between two vertices if their binary representations differ in exactly one bit. This is the...
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 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 1209G1 - Into Blocks (easy version)
CF 1209G1 - Into Blocks (easy version) Rating: 2000 Tags: data structures, dsu, greedy, implementation, two pointers Solve time: 1m 44s Verified: yes Solution Problem Understanding We are asked to transform a given sequence of integers into a "nice" sequence. A sequence is nice if all occurrences of the same number appear in contiguous blocks. For example, [3, 3, 1, 1, 2] is nice, but [3, 1, 3] is not...
CF 1276B - Two Fairs
CF 1276B - Two Fairs Rating: 1900 Tags: combinatorics, dfs and similar, dsu, graphs Solve time: 1m 34s Verified: yes Solution Problem Understanding We are given a connected undirected graph of cities. Among all cities, two special nodes are distinguished, call them a and b , representing two fairs. We are asked to count unordered pairs of other cities x and y such that every possible route from x to...
CF 1386C - Joker
CF 1386C - Joker Rating: 2800 Tags: *special, bitmasks, data structures, divide and conquer, dsu Solve time: 2m 24s Verified: no Solution Solution Let $N = pq$ where $p \equiv 3 \pmod 8$ and $q \equiv 7 \pmod 8$. We first prove the claimed identity involving the Jacobi symbol. Recall that the Jacobi symbol is multiplicative and satisfies $$\left(\frac{a}{N}\right) = \left(\frac{a}{p}\right) \left(\frac{a}{q}\right)$$ for any integer $a$. We compute $\left(\frac{-1}{N}\right)$ and...
CF 1402A - Fancy Fence
CF 1402A - Fancy Fence Rating: 1800 Tags: *special, data structures, dsu, implementation, math, sortings Solve time: 1m 59s Verified: no Solution Problem Understanding We are given a fence composed of $N$ rectangular sections placed side by side. Each section $i$ has a width $w_i$ and a height $h_i$. Our task is to count all axis-aligned rectangles that can be formed entirely on top of these sections. A rectangle must...
CF 1434E - A Convex Game
CF 1434E - A Convex Game Rating: 3500 Tags: dsu, games Solve time: 1m 8s Verified: yes Solution Problem Understanding We are given several independent games. Each game consists of a strictly increasing array of integers. Two players alternate moves, and on each move a player selects one element from the array. The chosen indices must form a strictly increasing sequence over time, so once you pick a position, you...
CF 1442B - Identify the Operations
CF 1442B - Identify the Operations Rating: 1800 Tags: combinatorics, data structures, dsu, greedy, implementation Solve time: 1m 55s Verified: no Solution Problem Understanding We start with a permutation stored in a line. At each step, we remove one element from the current line and, depending on where we removed it, we are forced to append one of its immediate neighbors (left or right, whichever exists at that moment) into...
CF 1468J - Road Reform
CF 1468J - Road Reform Rating: 1800 Tags: dsu, graphs, greedy Solve time: 9m 15s Verified: yes Solution Problem Understanding The problem presents a country with n cities connected by m bidirectional roads. Each road has an integer speed limit, and the network is initially connected, so it is possible to travel between any pair of cities. The government wants to perform two actions: reduce the number of roads to...
CF 1468B - Bakery
CF 1468B - Bakery Rating: 2900 Tags: data structures, dsu Solve time: 2m 17s Verified: no Solution Problem Understanding We are asked to compute a measure of stale bread at a bakery over multiple days. Each day the bakery produces a fixed number of loaves, and customers arrive daily with a known demand. Bread is sold in a first-in-last-out fashion: freshly baked loaves are sold first, and older unsold loaves...
CF 1575J - Jeopardy of Dropped Balls
CF 1575J - Jeopardy of Dropped Balls Rating: 1500 Tags: binary search, brute force, dsu, implementation Solve time: 56s Verified: yes Solution Problem Understanding We have an n × m grid. Every cell stores one of three directions. A value of 1 means a ball moves one cell to the right. A value of 2 means the ball moves one cell downward. A value of 3 means the ball moves...
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 1725K - Kingdom of Criticism
CF 1725K - Kingdom of Criticism Rating: 2500 Tags: data structures, dsu Solve time: 2m 48s Verified: yes Solution Problem Understanding We are managing a kingdom with a line of buildings, each with an integer height. Residents occasionally issue criticisms targeting all buildings with heights in a specific interval [l, r], where r-l is always odd. The kingdom's construction team must then adjust building heights so no building lies in...
CF 1765A - Access Levels
CF 1765A - Access Levels Rating: 2400 Tags: bitmasks, dsu, flows, graph matchings Solve time: 2m 29s Verified: no Solution Problem Understanding Each document is described by which developers should be able to open it. So every column of the input matrix is a subset of developers: those rows where the value is one form the “approved set” for that document. We are allowed to compress this binary matrix into...
CF 1877F - Lexichromatography
CF 1877F - Lexichromatography Rating: 2500 Tags: combinatorics, dsu Solve time: 3m 17s Verified: no Solution Problem Understanding We are asked to count the number of ways to colour an array of integers either blue or red, subject to two constraints. The first is a lexicographical constraint: if we extract the blue elements as a subsequence and the red elements as another subsequence, the blue sequence must be strictly smaller...
CF 1876D - Lexichromatography
CF 1876D - Lexichromatography Rating: 2500 Tags: combinatorics, dfs and similar, dsu, graphs, two pointers Solve time: 2m 10s Verified: no Solution Problem Understanding We are given an array of integers, and our task is to assign each element one of two colors, blue or red. The goal is to count all colorings that satisfy two conditions. First, if we take the subsequence of all blue elements and the subsequence...
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 1940A - Parallel Universes
CF 1940A - Parallel Universes Rating: - Tags: *special, constructive algorithms, dfs and similar, dsu, graphs Solve time: 54s Verified: no Solution This prompt is missing the actual problem statement of CF 1940A - Parallel Universes , so a correct editorial cannot be written yet without guessing the task. Right now we only know: problem code: 1940A title: Parallel Universes tags: graphs / DSU / constructive but no input/output definition...
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 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 2041J - Bottle Arrangement
CF 2041J - Bottle Arrangement Rating: 2700 Tags: data structures, divide and conquer, dp, dsu, greedy Solve time: 2m 2s Verified: no Solution The failure you show is not enough to diagnose the algorithm itself. Let's trace what happened: The input contains 5 test cases : 5 2 2 1 5 2 3 4 5 1 5 2 1 4 2 3 5 4 1 1 5 4 10 4...
CF 2068E - Porto Vs. Benfica
CF 2068E - Porto Vs. Benfica Rating: 2800 Tags: data structures, dfs and similar, dsu, graphs, shortest paths Solve time: 1m 44s Verified: no Solution Problem Understanding We are given a large undirected, unweighted graph representing a road network. One vertex is the starting point (Lisbon, vertex 1) and another is the destination (Porto, vertex n). A group of travelers always wants to reach the destination using as few edges...
CF 2172J - Sliding Tiles
CF 2172J - Sliding Tiles Rating: 2300 Tags: data structures, divide and conquer, dsu Solve time: 1m 46s Verified: no Solution Problem Understanding We are given an $n \times n$ grid with tiles stacked in columns and vertical bars between adjacent columns. Each column $i$ starts with $a_i$ tiles stacked from the bottom. Each bar between column $i$ and $i+1$ has a height $h_i$, which blocks movement of tiles in...
CF 2222F - Building Tree
CF 2222F - Building Tree Rating: - Tags: data structures, divide and conquer, dsu, graphs, implementation Solve time: 1m 37s Verified: no Solution Problem Understanding We start with a weighted undirected graph on n vertices. The twist is that distance between two nodes is not the usual shortest path sum. Instead, if you take any path and look at the set of edge weights used on that path, the cost...
CF 46F - Hercule Poirot Problem
CF 46F - Hercule Poirot Problem Rating: 2300 Tags: dsu, graphs Solve time: 1m 1s Verified: yes Solution Problem Understanding We are given a house with a certain number of rooms connected by doors, and each door has a unique key. There are several residents in the house, each initially in some room with some keys. We also know the positions and key holdings of every resident at a later...