This commit is contained in:
Benjamin Qi 2020-06-22 22:17:59 -04:00
parent 84cc6bcebd
commit af73a84163
20 changed files with 177 additions and 134 deletions

View file

@ -7,8 +7,9 @@ description: Introduces C++ containers that are frequently used in competitive p
## Additional Resources
- CPH 4 (Data Structures)
- [PAPS 3.1, 3.5, 6.1](https://www.csc.kth.se/~jsannemo/slask/main.pdf)
- CPH 4 (Data Structures)
- [PAPS 3.1, 3.5, 6.1](https://www.csc.kth.se/~jsannemo/slask/main.pdf)
- [CPC.2](https://github.com/SuprDewd/T-414-AFLV/tree/master/02_data_structures)
# Data Structures

View file

@ -15,29 +15,10 @@ prerequisites:
Do roughly the first half of the Sorting and Searching section in the [CSES Problem Set](https://cses.fi/problemset/)
(actually go through these and check ...)
### General
**Stack:**
- UVa 00514 - Rails
- UVa 00732 - Anagram by Stack
- UVa 01062 - Containers
**Queue/Deque:**
- UVa 10172 - The Lonesome Cargo
- UVa 10901 - Ferry Loading III
- UVa 11034 - Ferry Loading IV
**General:**
- [Beautiful Triplets](https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/practice-problems/algorithm/mancunian-and-beautiful-triplets-30968257/) [](54)
- [Polycarp's Phone Book](http://codeforces.com/contest/860/problem/B) [](56)
- [Jury Marks](http://codeforces.com/contest/831/problem/C) [](67)
- [Mahmoud & Ehab & Function](http://codeforces.com/contest/862/problem/E) [](74)
- [Karen & Cards](http://codeforces.com/contest/815/problem/D) [](86)
- [Tournament](http://codeforces.com/contest/878/problem/C) [](106)
## Other Resources
- CPH 4
- [CPC.2](https://github.com/SuprDewd/T-414-AFLV/tree/master/02_data_structures)
- [Polycarp's Phone Book](http://codeforces.com/contest/860/problem/B) [](56)
- [Jury Marks](http://codeforces.com/contest/831/problem/C) [](67)
- [Mahmoud & Ehab & Function](http://codeforces.com/contest/862/problem/E) [](74)
- [Karen & Cards](http://codeforces.com/contest/815/problem/D) [](86)
- [Tournament](http://codeforces.com/contest/878/problem/C) [](106)

View file

@ -7,7 +7,7 @@ prerequisites:
description: Finding connected components in a graph that is respresented by a grid.
---
## Flood Fill](https://en.wikipedia.org/wiki/Flood_fill)
## [Flood Fill](https://en.wikipedia.org/wiki/Flood_fill)
- [CSES Counting Rooms](https://cses.fi/problemset/task/1192)
- [CSES Labyrinth](https://cses.fi/problemset/task/1193)
@ -15,7 +15,7 @@ description: Finding connected components in a graph that is respresented by a g
### Tutorial
- Recommended:
- Ch 10 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a
- Intro to USACO Ch 10
### Problems

View file

@ -18,24 +18,28 @@ description: Introduces sorting and binary searching on a sorted array.
There are many sorting algorithms, here are some sources to learn about the popular ones:
- [Bubble Sort](https://www.hackerrank.com/challenges/ctci-bubble-sort/problem)
- [Out of Sorts (Silver)](http://www.usaco.org/index.php?page=viewproblem2&cpid=834)
- hard!
- [Quicksort](https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/tutorial/)
- [Mergesort](https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/tutorial/)
- [Bubble Sort](https://www.hackerrank.com/challenges/ctci-bubble-sort/problem)
- O(N^2)
- [Out of Sorts (Silver)](http://www.usaco.org/index.php?page=viewproblem2&cpid=834)
- hard!
- [HackerEarth Quicksort](https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/tutorial/)
- expected $O(N\log N)$
- [HackerEarth Mergesort](https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/tutorial/)
- $O(N\log N)$
## Library Sorting
- C++:
- C++
- [std::sort Documentation](https://en.cppreference.com/w/cpp/algorithm/sort)
- [C++ Tricks (First Two Related To Sorting)](https://codeforces.com/blog/entry/74684)
- [std::stable\_sort documentation](http://www.cplusplus.com/reference/algorithm/stable_sort/)
- Java:
- [Golovanov399 - C++ Tricks](https://codeforces.com/blog/entry/74684)
- first two related to sorting
- Java
- [Arrays.sort Documentation](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(java.lang.Object[]))
- [Breaking Java Arrays.sort()](https://codeforces.com/blog/entry/4827)
- no longer works, but see [this one](https://codeforces.com/contest/1324/hacks/625031/test) instead.
- to avoid getting hacked, [shuffle](https://pastebin.com/k6gCRJDv) the array beforehand.
- Python:
- Python
- [Sorted Documentation](https://docs.python.org/3/howto/sorting.html)
## Binary Search

View file

@ -8,13 +8,13 @@ prerequisites:
description: More information about sorting with custom comparators in C++. Some overlap with the previous module.
---
## Additional Resources
## Additional Reading
- [fushar (C++)](http://fusharblog.com/3-ways-to-define-comparison-functions-in-cpp/)
## An Example with Graphs
## Example: [Wormhole Sort (Silver)](http://www.usaco.org/index.php?page=viewproblem2&cpid=992)
Consider a graph with weighted edges (pairs of values), such as in the problem [Wormhole Sort (Silver)](http://www.usaco.org/index.php?page=viewproblem2&cpid=992). There are multiple ways to solve this problem, but all of them start by sorting the edges in nondecreasing order of weight. If we only stored the edge weights and sorted them, we would have a sorted list of edge weights, but it would be impossible to tell which weights corresponded to which edges. However, if we create a class representing the edges and define a custom comparator to sort them by weight, we can sort the edges in ascending order while also keeping track of their endpoints.
There are multiple ways to solve this problem, but all of them start by sorting the edges in nondecreasing order of weight. If we only stored the edge weights and sorted them, we would have a sorted list of edge weights, but it would be impossible to tell which weights corresponded to which edges. However, if we create a class representing the edges and define a custom comparator to sort them by weight, we can sort the edges in ascending order while also keeping track of their endpoints.
## Comparators for Sorting
@ -22,7 +22,7 @@ There are 2 main ways to have a custom comparator in c++.
### Overloading Operator
[Why const T&?](https://stackoverflow.com/questions/11805322/why-should-i-use-const-t-instead-of-const-t-or-t)
[StackOverflow: Why const T&?](https://stackoverflow.com/questions/11805322/why-should-i-use-const-t-instead-of-const-t-or-t)
- Pro:
- This is the easiest to implement
@ -283,7 +283,7 @@ int main() {
}
```
There is no need to create a custom comparator.
For wormhole sort, there is no need to create a custom comparator:
```cpp
#include <bits/stdc++.h>

View file

@ -22,8 +22,8 @@ Essentially, the comparator determines whether object $x$ belongs to the left of
In addition to returning the correct answer, comparators should also satisfy the following conditions:
- The function must be consistent with respect to reversing the order of the arguments: if $x \neq y$ and `compare(x, y)}`is positive, then `compare(y, x)` should be negative and vice versa
- The function must be transitive. If `compare(x, y)` is true and `compare(y, z)` is true, then `compare(x, z)}` should also be true. If the first two compare functions both return `false`, the third must also return `false`.
- The function must be consistent with respect to reversing the order of the arguments: if $x \neq y$ and `compare(x, y)`is positive, then `compare(y, x)` should be negative and vice versa
- The function must be transitive. If `compare(x, y)` is true and `compare(y, z)` is true, then `compare(x, z)` should also be true. If the first two compare functions both return `false`, the third must also return `false`.
A generic way of implementing a custom comparator is to define a function. For our example, we'll use a `struct` of a Person that contains a person's height and weight, and sort in ascending order by height. A `struct` is essentially a user-defined data structure:
@ -327,11 +327,12 @@ Foo(1,2) Foo(3,2) Foo(6,6) Foo(6,9) Foo(8,7) Foo(8,9) Foo(9,7) Foo(9,8)
## Problems
- [Lifeguards](http://usaco.org/index.php?page=viewproblem2&cpid=786)
- [Rental Service](http://usaco.org/index.php?page=viewproblem2&cpid=787)
- [Mountains](http://usaco.org/index.php?page=viewproblem2&cpid=896)
- [Mooyo Mooyo](http://www.usaco.org/index.php?page=viewproblem2&cpid=860)
- Not a sorting problem, but you can use sorting to simulate gravity nicely.
- Write a custom comparator (read below) which puts zeroes at the front and use `stable_sort` to keep the relative order of other elements the same.
- [Meetings](http://www.usaco.org/index.php?page=viewproblem2&cpid=967)
- hard!
- [Lifeguards](http://usaco.org/index.php?page=viewproblem2&cpid=786)
- sort endpoints of intervals
- [Rental Service](http://usaco.org/index.php?page=viewproblem2&cpid=787)
- [Mountains](http://usaco.org/index.php?page=viewproblem2&cpid=896)
- [Mooyo Mooyo](http://www.usaco.org/index.php?page=viewproblem2&cpid=860)
- Not a sorting problem, but you can use sorting to simulate gravity.
- Write a custom comparator which puts zeroes at the front and use `stable_sort` to keep the relative order of other elements the same.
- [Meetings](http://www.usaco.org/index.php?page=viewproblem2&cpid=967)
- hard!

View file

@ -152,4 +152,17 @@ pq.add(6); // [7, 6, 5]
## Problems
??
(actually go through these and check ...)
### Stack
- UVa 00514 - Rails
- UVa 00732 - Anagram by Stack
- UVa 01062 - Containers
### Queue / Deque
- UVa 10172 - The Lonesome Cargo
- UVa 10901 - Ferry Loading III
- UVa 11034 - Ferry Loading IV

View file

@ -19,8 +19,14 @@ description: Introduces the problems of finding level ancestors in a tree and co
- CPH 18.1, 18.3
- [cp-algorithms: Lowest Common Ancestor](https://cp-algorithms.com/)
- [Binary Jumping w/ O(N) Memory](https://codeforces.com/blog/entry/74847)
- you shouldn't actually need this, but maybe interesting
<optional-content title="Improvements">
- [CF: $O(\log N)$ queries and $O(N)$ memory](https://codeforces.com/blog/entry/74847)
- [Wikipedia: $O(1)$ queries and $O(N)$ preprocessing time](https://en.wikipedia.org/wiki/Level_ancestor_problem#Ladder_algorithm)
- though explanation is not the greatest
</optional-content>
### Problems
@ -29,21 +35,18 @@ description: Introduces the problems of finding level ancestors in a tree and co
- [Planets Queries II](https://cses.fi/problemset/task/1160)
- USACO
- Gold
- [USACO Gold Milk Visits](http://www.usaco.org/index.php?page=viewproblem2&cpid=970)
- [USACO Gold Cow Land](http://www.usaco.org/index.php?page=viewproblem2&cpid=921)
- [Cow Land](http://www.usaco.org/index.php?page=viewproblem2&cpid=921)
- LCA + BIT
- Plat
- [USACO Plat Newbarns](http://www.usaco.org/index.php?page=viewproblem2&cpid=817)
- [Max Flow](http://www.usaco.org/index.php?page=viewproblem2&cpid=576)
- [Disruption](http://www.usaco.org/index.php?page=viewproblem2&cpid=842)
- [Newbarns](http://www.usaco.org/index.php?page=viewproblem2&cpid=817)
- online tree diameter
- Copy of [CF Brain Network "Hard"](https://codeforces.com/contest/690/problem/C3)
- [Max Flow](http://www.usaco.org/index.php?page=viewproblem2&cpid=576)
- [Promote](http://www.usaco.org/index.php?page=viewproblem2&cpid=696)
- Subtree + BIT
- [Disrupt](http://www.usaco.org/index.php?page=viewproblem2&cpid=842)
- HLD is possible, but just do binary jumps
- [Tree Boxes](http://www.usaco.org/index.php?page=viewproblem2&cpid=948)
- interactive!!
- [Gathering](http://www.usaco.org/index.php?page=viewproblem2&cpid=866)
- test cases added after contest ...
- [Exercise](http://www.usaco.org/index.php?page=viewproblem2&cpid=901)
- tricky
- Other

View file

@ -5,9 +5,11 @@ author: Michael Cao
prerequisites:
- Recursion
- Silver - Prefix Sums
description: A very important concept which emerges in the Gold division and extends to the IOI. (improve?)
description: Speeding up naive solutions with memoization.
---
This is a very important concept which emerges in the Gold division and extends to the IOI. (improve?)
<info-block title="Contest Tip">
Usually, at least one problem from every gold division contest involves some sort of DP.

View file

@ -22,15 +22,19 @@ Don't just dive into trying to figure out a DP state and transitions -- make som
</info-block>
* [CSES - Tree Matching](https://cses.fi/problemset/task/1130)
* [Subtree](https://atcoder.jp/contests/dp/tasks/dp_v)
* [Independent Set](https://atcoder.jp/contests/dp/tasks/dp_p)
* [Gold - Barn Painting](http://www.usaco.org/index.php?page=viewproblem2&cpid=766)
* Similar to Independent Set
* [Gold - Delegation](http://usaco.org/index.php?page=viewproblem2&cpid=1019)
* Pretty much a greedy problem.
* [BOI - Cat In A Tree](https://cses.fi/file/a904421da451fbe1b60f96a27256832a011825dfd097ee40184d15878e837018/)
* Observations first.
* [COCI - Dzumbus](https://oj.uz/problem/view/COCI19_dzumbus)
* Solve child subtree by subtree. Runtime may be faster than you think.
* [Ostap and Tree](https://codeforces.com/problemset/problem/735/E)
- USACO
- [Gold - Barn Painting](http://www.usaco.org/index.php?page=viewproblem2&cpid=766)
- Similar to Independent Set
- [Gold - Delegation](http://usaco.org/index.php?page=viewproblem2&cpid=1019)
- Pretty much a greedy problem.
- [Plat - Delegation](http://www.usaco.org/index.php?page=viewproblem2&cpid=1020)
- Binary search on answer!
- Other
- [CSES - Tree Matching](https://cses.fi/problemset/task/1130)
- [Subtree](https://atcoder.jp/contests/dp/tasks/dp_v)
- [Independent Set](https://atcoder.jp/contests/dp/tasks/dp_p)
- [BOI - Cat In A Tree](https://cses.fi/file/a904421da451fbe1b60f96a27256832a011825dfd097ee40184d15878e837018/)
- Observations first.
- [COCI - Dzumbus](https://oj.uz/problem/view/COCI19_dzumbus)
- Solve child subtree by subtree. Runtime may be faster than you think.
- [Ostap and Tree](https://codeforces.com/problemset/problem/735/E)

View file

@ -1,10 +1,10 @@
---
id: data-structures-gold
title: "Data Structures (Gold)"
id: ds-gold
title: "Data Structures"
author: Michael Cao
prerequisites:
- Silver - Data Structures
description: Monotonic stack and sliding window.
description: "Monotonic stack and sliding window."
---
## Additional Reading
@ -15,7 +15,7 @@ description: Monotonic stack and sliding window.
Consider the following problem:
> Given an array, $a$, of $N (1 \le N \le 10^5)$ integers, for every index $i$, find the rightmost index $j$ such that $j < i$ and $a_i > a_j$.
> Given an array, $a$, of $N$ ($1 \le N \le 10^5$) integers, for every index $i$, find the rightmost index $j$ such that $j < i$ and $a_i > a_j$.
To solve this problem, let's store a stack of pairs of `<value, index>` and iterate over the array from left to right. For some index $i$, we will compute $ans_i$, the rightmost index for $i$, as follows:
@ -58,7 +58,7 @@ To compute the sum in the range, instead of using a set, we can store a variable
<optional-content title="Better Memory with Two Passes">
- [Plat - Train Tracking](http://www.usaco.org/current/data/sol_train_platinum_open18.html)
- [Plat - Train Tracking](http://www.usaco.org/index.php?page=viewproblem2&cpid=841)
- Quite difficult.
</optional-content>

View file

@ -9,16 +9,18 @@ description: Divisibility and Modular Arithmetic
## Additional Resources
- [PAPS 16](https://www.csc.kth.se/~jsannemo/slask/main.pdf)
- CPH 21.1 (Primes and factors), 21.2 (Modular arithmetic)
- [CF CodeNCode](https://codeforces.com/blog/entry/77137)
- [PAPS 16](https://www.csc.kth.se/~jsannemo/slask/main.pdf)
- [CF CodeNCode: Number Theory Course](https://codeforces.com/blog/entry/77137)
## Prime Factorization
A number $a$ is called a **divisor** or a **factor** of a number $b$ if $b$ is divisible by $a$, which means that there exists some integer $k$ such that $b = ka$. Conventionally, $1$ and $n$ are considered divisors of $n$. A number $n > 1$ is **prime** if its only divisors are $1$ and $n$. Numbers greater than \(1\) that are not prime are **composite**.
Every number has a unique **prime factorization**: a way of decomposing it into a product of primes, as follows:
\[ n = {p_1}^{a_1} {p_2}^{a_2} \cdots {p_k}^{a_k} \]
$$
n = {p_1}^{a_1} {p_2}^{a_2} \cdots {p_k}^{a_k}
$$
where the $p_i$ are distinct primes and the $a_i$ are positive integers.
Now, we will discuss how to find the prime factorization of an integer.
@ -60,11 +62,15 @@ The **least common multiple (LCM)** of two integers $a$ and $b$ is the smallest
The LCM can easily be calculated from the following property with the GCD:
$$\operatorname{lcm}(a, b) = \frac{a \cdot b}{\gcd(a, b)}$$
$$
\operatorname{lcm}(a, b) = \frac{a \cdot b}{\gcd(a, b)}.
$$
If we want to take the GCD or LCM of more than two elements, we can do so two at a time, in any order. For example,
$$\gcd(a_1, a_2, a_3, a_4) = \gcd(a_1, \gcd(a_2, \gcd(a_3, a_4)))$$
$$
\gcd(a_1, a_2, a_3, a_4) = \gcd(a_1, \gcd(a_2, \gcd(a_3, a_4))).
$$
## Modular Arithmetic
@ -84,10 +90,11 @@ $$
### Modular Exponentiation
**Modular Exponentiation** can be used to efficently compute $x ^ n \mod m$. To do this, let's break down $x ^ n$ into binary components. For example, $5 ^ 10$ = $5 ^ {1010_2}$ = $5 ^ 8 \cdot 5 ^ 4$. Then, if we know $x ^ y$ for all $y$ which are powers of two ($x ^ 1$, $x ^ 2$, $x ^ 4$, $\dots$ , $x ^ {\lfloor{\log_2n} \rfloor}$, we can compute $x ^ n$ in $\mathcal{O}(\log n)$. Finally, since $x ^ y$ for some $y \neq 1$ equals $2 \cdot x ^ {y - 1}$, and $x$ otherwise, we can compute these sums efficently. To deal with $m$, observe that modulo doesn't affect multiplications, so we can directly implement the above "binary exponentiation" algorithm while adding a line to take results$\mod m$.
**Modular Exponentiation** can be used to efficently compute $x ^ n \mod m$. To do this, let's break down $x ^ n$ into binary components. For example, $5 ^ {10}$ = $5 ^ {1010_2}$ = $5 ^ 8 \cdot 5 ^ 4$. Then, if we know $x ^ y$ for all $y$ which are powers of two ($x ^ 1$, $x ^ 2$, $x ^ 4$, $\dots$ , $x ^ {\lfloor{\log_2n} \rfloor}$, we can compute $x ^ n$ in $\mathcal{O}(\log n)$. Finally, since $x ^ y$ for some $y \neq 1$ equals $2 \cdot x ^ {y - 1}$, and $x$ otherwise, we can compute these sums efficently. To deal with $m$, observe that modulo doesn't affect multiplications, so we can directly implement the above "binary exponentiation" algorithm while adding a line to take results $\pmod m$.
Here is C++ code to compute $x ^ n \mod m$:
Here is C++ code to compute $x ^ n \pmod m$:
(not tested)
```cpp
long long binpow(long long x, long long n, long long m) {
x %= m;
@ -114,6 +121,6 @@ Under a prime moduli, division exists.
- First consider the case where there are only two lines with the same class.
- Requires fast modular exponentiation for full credit.
- [Exercise](http://www.usaco.org/index.php?page=viewproblem2&cpid=1043)
- Prime factorization
- Prime factorize $K$.
- Other
- [CF VK Cup 2012 Wildcard Round 1 C](https://codeforces.com/problemset/problem/162/C)

View file

@ -9,7 +9,10 @@ description: ?
## Additional Reading
- CPH 18.4 - Merging Data Structures
- CPH 18.4 - Merging Data Structures
- CF Blogs
- [Arpa](https://codeforces.com/blog/entry/44351)
- [tuwuna](https://codeforces.com/blog/entry/67696)
# Merging Sets
@ -20,7 +23,7 @@ For each node, let's store a set containing only that node, and we want to merge
However, with just a few lines of code, we can significantly speed this up.
```cpp
if(a.size() < b.size()){ //for two sets a and b
swap(a,b);
swap(a,b);
}
```
In other words, by merging the smaller set into the larger one, the runtime complexity becomes $O(N\log N).$
@ -43,15 +46,13 @@ Prove that if you instead merge sets that have size equal to the depths of the s
## Problems
- [Favorite Colors (USACO Gold)](http://www.usaco.org/index.php?page=viewproblem2&cpid=1042)
- Merge Adjacency Lists
- Promotion Counting
- Merge Indexed Sets
## Small to Large (Offline)
Sample codes: [DSU on Tree code](https://codeforces.com/blog/entry/44351), [explanation of code](https://codeforces.com/blog/entry/67696)
- USACO Disrupt again!
- [CSES Distinct Colors](https://cses.fi/problemset/task/1139)
- [CF Lomsat gelral](https://codeforces.com/contest/600/problem/E)
- USACO
- [Gold - Favorite Colors](http://www.usaco.org/index.php?page=viewproblem2&cpid=1042)
- merge adjacency lists
- not required to get AC
- [Plat - Disruption](http://www.usaco.org/index.php?page=viewproblem2&cpid=842)
- [Plat - Promotion Counting](http://www.usaco.org/index.php?page=viewproblem2&cpid=696)
- merge indexed sets
- Other
- [CSES Distinct Colors](https://cses.fi/problemset/task/1139)
- [CF Lomsat gelral](https://codeforces.com/contest/600/problem/E)

View file

@ -19,7 +19,8 @@ First we'll consider the special case when $\ominus$ denotes `min`.
- CPH 9.1
- [PAPS 11.2.2](https://www.csc.kth.se/~jsannemo/slask/main.pdf)
- [cp-algorithms RMQ](https://cp-algorithms.com/sequences/rmq.html)
- [cp-algo RMQ](https://cp-algorithms.com/sequences/rmq.html)
- [cp-algo Sparse Table](https://cp-algorithms.com/data_structures/sparse-table.html)
<optional-content title="Preprocessing in O(N) Time">
@ -47,8 +48,8 @@ Actually, this can be adjusted to answer queries online in $O(1)$ time each. See
A data structure known as **sqrt-tree** can speed up preprocessing time to $O(N\log \log N)$.
* [CF Blog Pt 1](http://codeforces.com/blog/entry/57046)
* [CF Blog Pt 2](http://codeforces.com/blog/entry/59092)
- [CF Blog Pt 1](http://codeforces.com/blog/entry/57046)
- [CF Blog Pt 2](http://codeforces.com/blog/entry/59092)
</optional-content>

View file

@ -5,13 +5,18 @@ author: Benjamin Qi, Michael Cao
prerequisites:
- Gold - Breadth First Search
- Gold - Introduction to Dynamic Programming
description: "?"
---
To review, a **directed** graph consists of edges that can only be traversed in one direction. Additionally, a **acyclic** graph defines a graph which does not contain cycles, meaning you are unable to traverse across one or more edges and return to the node you started on. Putting these definitions together, a **directed acyclic** graph, sometimes abbreviated as DAG, is a graph which has edges which can only be traversed in one direction and does not contain cycles.
## Topological Sort
- [CSES Course Schedule](https://cses.fi/problemset/task/1679)
A [topological sort](https://en.wikipedia.org/wiki/Topological_sorting) of a directed acyclic graph is a linear ordering of its vertices such that for every directed edge $u\to v$ from vertex $u$ to vertex $v$, $u$ comes before $v$ in the ordering.
## Tutorial
### Tutorial
- CPH 16.1, 16.2
- DFS
@ -22,29 +27,39 @@ A [topological sort](https://en.wikipedia.org/wiki/Topological_sorting) of a dir
Consider [Khan's Algorithm](https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm) for topological sorting.
### Problems
- [Milking Order](http://www.usaco.org/index.php?page=viewproblem2&cpid=838)
- Binary search and check if a valid topological sort exists.
- [CSES Course Schedule II](https://cses.fi/problemset/task/1757)
- Tricky!
## Dynamic Programming
- [PAPS 9.1](https://www.csc.kth.se/~jsannemo/slask/main.pdf)
One useful property of directed acyclic graphs is, as the name suggests, that there exists no cycles. If we consider each node in the graph as a state, we can perform dynamic programming on the graph if we process the states in an order that guarantees for every edge, $u\to v$ that $u$ is processed before $v$. Fortunately, this is the exact definition of a topological sort!
Let's consider a classical problem (see Longest Flight Route) where we must find the longest path in a Directed Acyclic Graph. Let `dp[curr] = longest path ending at the node curr`. Then, if we process states in topological order, the transition is relatively straightforward: `dp[curr] = max of all dp[prev] where prev represents a node with an edge going into the current node` (word better?). To reiterate, since the states a processed in topological order, we can guarantee that all possible `dp[prev]` are computed before we compute `dp[curr]`.
Let's consider the classical problem [CSES Longest Flight Route](https://cses.fi/problemset/task/1680), where we must find the longest path in a DAG.
However, not all problems clearly give you directed acyclic graphs (see [Cave Paintings](http://usaco.org/index.php?page=viewproblem2&cpid=996)). An important step in many problems is to reduce the statement into a directed acyclic graph. See the editorial of the linked problem for more information.
<spoiler title="Solution">
Let `dp[curr] = longest path ending at the node curr`. Then, if we process states in topological order, the transition is relatively straightforward: `dp[curr] = max of all dp[prev] where prev represents a node with an edge going into the current node` (word better?). To reiterate, since the states a processed in topological order, we can guarantee that all possible `dp[prev]` are computed before we compute `dp[curr]`.
</spoiler>
## Example Problems
However, not all problems clearly give you directed acyclic graphs (ex. [Plat - Cave Paintings](http://usaco.org/index.php?page=viewproblem2&cpid=996)). An important step in many problems is to reduce the statement into a directed acyclic graph. See the editorial of the linked problem for more information.
- [CSES Course Schedule](https://cses.fi/problemset/task/1679)
- [CSES Longest Flight Route](https://cses.fi/problemset/task/1680)
- [CSES Game Routes](https://cses.fi/problemset/task/1681)
(Ben - this last paragraph doesn't seem very helpful.)
## Problems
- [CSES Game Routes](https://cses.fi/problemset/task/1681)
- counting paths on DAG
- [Quantum](https://open.kattis.com/contests/acpc17open/problems/quantumsuperposition)
- enumerating paths on DAG
- [USACO Gold - Timeline](http://www.usaco.org/index.php?page=viewproblem2&cpid=1017)
- not explicitly given, but graph is a DAG
## Problems
- USACO Gold
- [Timeline](http://www.usaco.org/index.php?page=viewproblem2&cpid=1017)
- Dynamic Programming on DAG.
- [Milking Order](http://www.usaco.org/index.php?page=viewproblem2&cpid=838)
- Binary search and check if a valid topological sort exists.
- Other
- [Minimal Labels](http://codeforces.com/contest/825/problem/E) [](53)
- [Quantum](https://open.kattis.com/contests/acpc17open/problems/quantumsuperposition) [](84)

View file

@ -8,9 +8,17 @@ prerequisites:
description: Subtree updates and queries and another way to compute lowest common ancestors.
---
- CPH 18.2
RMQ LCA?
- [CSES: Subtree Queries](https://cses.fi/problemset/task/1137)
https://cses.fi/problemset/task/1138: path queries
https://cses.fi/problemset/task/1137: subtree queries
## Tutorial
- CPH 18.2
- [cp-algorithms - LCA with Sparse Table](https://cp-algorithms.com/graph/lca.html)
## Problems
- [CSES: Path Queries](https://cses.fi/problemset/task/1138)
- USACO
- [Gold - Milk Visits](http://www.usaco.org/index.php?page=viewproblem2&cpid=970)
- [Plat - Promotion Counting](http://www.usaco.org/index.php?page=viewproblem2&cpid=696)
- [Plat - Snow-Cow](http://www.usaco.org/index.php?page=viewproblem2&cpid=973)

View file

@ -15,6 +15,7 @@ description: ?
### Problems
- [Ciel the Commander](https://codeforces.com/problemset/problem/321/C)
- [USACO Plat - At Large](http://www.usaco.org/index.php?page=viewproblem2&cpid=793)
- very tricky
- [DMOJ Bob Equilibrium](https://dmoj.ca/problem/dmopc19c7p6)
- tight time limit
- [USACO Plat - At Large](http://www.usaco.org/index.php?page=viewproblem2&cpid=793)
- *very* tricky, requires several observations

View file

@ -17,4 +17,4 @@ description: Path and subtree updates and queries.
### Problems
??
- [Disrupt](http://www.usaco.org/index.php?page=viewproblem2&cpid=842)

View file

@ -23,13 +23,14 @@ prerequisites:
### Problems
- [USACO Plat Balance](http://www.usaco.org/index.php?page=viewproblem2&cpid=864)
- [USACO Plat Falling](http://www.usaco.org/index.php?page=viewproblem2&cpid=998)
- [USACO Plat - Balance Beam](http://www.usaco.org/index.php?page=viewproblem2&cpid=864)
- quite easy (but make sure to avoid precision issues)
- [USACO Plat - Falling Portals](http://www.usaco.org/index.php?page=viewproblem2&cpid=998)
- [USACO Old Gold - Fencing](http://www.usaco.org/index.php?page=viewproblem2&cpid=534)
- [USACO Old Gold - Cow Curling](http://www.usaco.org/index.php?page=viewproblem2&cpid=382)
- [Kattis Fence Orthogonality](https://open.kattis.com/problems/fenceortho)
- [AGC 44 Random Pawn](https://atcoder.jp/contests/agc044/tasks/agc044_e)
- Generalization of "Balance"
- Generalization of "Balance Beam"
Minkowski addition?

View file

@ -50,10 +50,10 @@ const ModuleOrdering = {
},
{
name: "Data Structures",
items: {
items: [
"stacks-queues",
"maps-sets",
}
]
},
"binary-search",
"2P",
@ -69,7 +69,7 @@ const ModuleOrdering = {
},
],
"gold": [
"data-structures-gold",
"ds-gold",
"dp",
"intro-nt",
{