Competitive Programming Solutions: Graph Paths, DP, and Segment Trees
Problem A: Operations with Inversions
Greedy solution. Skipped for brevity.
Problem B: Optimal Shifts
Greedy solution. Skipped for brevity.
Problem C: Odd Process
Problem Statement: You have n numbers, each with a value a_i. You need to perform k rounds of operations where you can place a number into a bag (each number can only be placed once). ...
Posted on Tue, 29 Sep 2026 16:27:28 +0000 by Pilly
Detecting Universal Sink in Directed Graphs Using Adjacency Matrix
A universal sink in a directed graph is a vertex with in-degree |V|-1 and out-degree 0. Given an adjacency matrix representation, we can determine the existence of such a vertex in O(V) time by simultaneously traversing rows and columns.
func findUniversalSink(matrix [][]int) int {
n := len(matrix)
candidate := 0
for i := 0; i ...
Posted on Tue, 15 Sep 2026 16:48:50 +0000 by jjk2
Minimum Distance to Deliver All Orders in a Tree Network
In a tree-structured neighborhood where the root represents the delivery station, a courier must visit all requested delivery nodes at least once. The goal is to compute, after each new delivery request, the shortest total distance required to deliver all orders so far—without needing to return to the root.
The key insight is that traversing al ...
Posted on Thu, 03 Sep 2026 16:29:39 +0000 by Fritz.fx
Floyd Algorithm for Graph Shortest Path and BFS with A* for Knight Moves
Floyd Algorithm for All-Pairs Shortest Path
Implement Floyd's algorithm to compute shortest paths between all pairs of vertices in an undirected weighted graph. The graph is defined by vertices labeled from 1 to n and m edges with positive weights. For multiple queries, output the shotrest distance betwean two vertices or -1 if no path exists.
...
Posted on Sun, 23 Aug 2026 16:52:46 +0000 by Warmach
Topological Sorting: Detecting DAGs and Resolving Competition Rankings
Topological sorting is a fundamental graph algorithm with critical applications in determining whether a directed graph contains cycles. This technique is extensively used in build systems, course scheduling, and dependency resolution.
Problem A: Topological Sort for Directed Acyclic Graphs
The core challenge involves producing a valid topologi ...
Posted on Thu, 30 Jul 2026 16:35:06 +0000 by dlester
Floyd Algorithm and Its Practical Applications in Graph Problems
Understanding the Floyd-Warshall Algorithm
The Floyd-Warshall algorithm is a classic dynamic programming approach used to compute the shortest paths between all pairs of vertices in a weighted graph. It operates efficiently on dense graphs where the number of edges is close to the square of the number of vertices. With a time complexity of \\(O ...
Posted on Thu, 25 Jun 2026 16:42:24 +0000 by antwonw
Bidirectional Search Strategies: BFS Optimization and Meet-in-the-Middle Techniques
Bidirectional search techniques optimize exhaustive searches by simultaneously exploring from both the initial state and target state, or by splitting the search space into manageable halves. These approaches significantly reduce the branching factor and memory requirements compared to unidirectional methods.
Bidirectional BFS for Shortest Path ...
Posted on Wed, 24 Jun 2026 17:41:35 +0000 by santopernola
Constructing and Utilizing Biconnected Components Graphs
This document explores the concept of biconnected components (BCCs) and their representation using a specialized graph structure called a biconnected components graph, often referred to as a "circle-square tree" or "block-cut tree."
Construction
Similar to vertex-connectivity decomposition (v-dcc), the biconnected components ...
Posted on Tue, 16 Jun 2026 16:52:47 +0000 by zymosan
Finding Shortest Paths with Alternating Edge Colors in Directed Graphs
Given a directed graph with nodes labeled 0 through n-1, where edge are colored either red or blue and may include self-loops and parallel edges.
Each [i, j] pair in red_edges represents a red directed edge from node i to node j. Similarly, each [i, j] pair in blue_edges represents a blue directed edge from node i to node j.
Compute an array re ...
Posted on Wed, 03 Jun 2026 17:06:15 +0000 by sharyn
Optimizing Number Theory and Graph Algorithms for Competitive Programming
Efficient XOR Sum Calculation
Define (f(i) = \oplus_{d|i}d), then compute (\oplus_{i=1}^{n}f(i)) for (n \le 10^{14}).
Approach: Count occurrences of each number via floor division blocks and compute interval XOR sums.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll prefix_xor(ll x) {
if (!x) return 0;
ll re ...
Posted on Mon, 18 May 2026 21:50:34 +0000 by lilRachie