This commit is contained in:
Nathan Wang 2020-06-03 16:10:44 -07:00
commit 82e00e4028
7 changed files with 154 additions and 24 deletions

View file

@ -0,0 +1,5 @@
# Silver - Two Pointers
- CPH 8.1
See 14.1 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a

View file

@ -0,0 +1 @@
# Silver - Binary Search

View file

@ -1,14 +1,131 @@
# Silver - Graphs
Author: Siyong Huang
## Overview
- Prerequisites
- Depth First Search (DFS)
- Flood Fill
- Graph Two-Coloring
- Cycle Detection
## Prerequisites
- [Graph Theory](https://csacademy.com/lesson/introduction_to_graphs)
- [Graph Representations](https://csacademy.com/lesson/graph_representation)
- Note: DFS is most commonly implemented with adjacency lists
## Depth First Search (DFS)
*Depth First Search*, more commonly DFS, is a fundamental graph algorithm that traverses an entire connected component. The rest of this document describe various applications of DFS.
### Tutorial
- Recommended:
- [CSAcademy BFS](https://csacademy.com/lesson/depth_first_search/)
- Additional:
- CPH Chapter 12
- [cp-algo DFS](https://cp-algorithms.com/graph/depth-first-search.html)
### Problems
- [Mootube, Silver (Easy)](http://usaco.org/index.php?page=viewproblem2&cpid=788)
- [Closing the Barn, Silver (Easy)](http://usaco.org/index.php?page=viewproblem2&cpid=644)
- [Moocast, Silver (Easy)](http://usaco.org/index.php?page=viewproblem2&cpid=668)
- [Pails (Normal)](http://usaco.org/index.php?page=viewproblem2&cpid=620)
- [Milk Visits (Normal)](http://www.usaco.org/index.php?page=viewproblem2&cpid=968)
## Flood Fill
*Flood Fill* refers to finding the number of connected components in a graph, frequently on a grid.
See Ch 10 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a
### Tutorial
- Recommended:
- Ch 10 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a
- Depth First Search (DFS)
### Problems
- [Ice Perimeter (Easy)](http://usaco.org/index.php?page=viewproblem2&cpid=895)
- [Switching on the Lights (Moderate)](http://www.usaco.org/index.php?page=viewproblem2&cpid=570)
- [Build Gates (Moderate)](http://www.usaco.org/index.php?page=viewproblem2&cpid=596)
- [Why Did the Cow Cross the Road III, Silver (Moderate)](http://usaco.org/index.php?page=viewproblem2&cpid=716)
- [Multiplayer Moo (Hard)](http://usaco.org/index.php?page=viewproblem2&cpid=836)
## Graph Two-Coloring
*Graph two-colorings* is assigning a boolean value to each node of the graph, dictated by the edge configuration
The most common example of a two-colored graph is a *bipartite graph*, in which each edge connects two nodes of opposite colors
### Tutorial
The idea is that we can arbitrarily label a node and then run DFS. Every time we visit a new (unvisited) node, we set its color based on the edge rule. When we visit a previously visited node, check to see whether its color matches the edge rule. For example, an implementation of coloring a bipartite graph is shown below.
```cpp
bool is_bipartite = true;
void dfs(int node)
{
visited[node] = true;
for(int u:adj_list[node])
if(visited[u])
{
if(color[u] == color[node])
is_bipartite = false;
}
else
{
color[u] = !color[node];
dfs(u);
}
}
```
- Additional:
- [Bipartite Graphs: cp-alg bipartite check](https://cp-algorithms.com/graph/bipartite-check.html)
- Note: CP Algorithm uses bfs, but dfs accomplishes the same task
### Problems
- [The Great Revegetation (Normal)](http://usaco.org/index.php?page=viewproblem2&cpid=920)
## Cycle Detection
A *cycle* is a non-empty path of distinct edges that start and end at the same node.
*Cycle detection* determines properties of cycles in a graph, such as counting the number of cycles in a graph or determining whether a node is in a cycle. For most silver-level cycle problems, each node has only one out-degree, meaning that it's adjacency list is of size 1. If this is not the case, the problem generalizes to *Strongly Connected Components*, a platinum level concept.
### Tutorial
The following sample code counts the number of cycles in a graph where each node points to one other node. The "stack" contains nodes that can reach the current node. If the current node points to a node v on the stack (on_stack[v] is true), then we know that a cycle has been created. However, if the current node points to a node v that has been previously visited but is not on the stack, then we know that the current chain of nodes points into a cycle that has already been considered.
```cpp
//Each node points to next_node[node]
bool visited[MAXN], on_stack[MAXN];
int number_of_cycles = 0;
void dfs(int n)
{
visited[n] = on_stack[n] = true;
int u = next_node[n];
if(on_stack[u])
number_of_cycles++;
else if(!visited[u])
dfs(u);
on_stack[n] = false;
}
int main()
{
//read input, etc
for(int i = 1;i <= N;i++)
if(!visited[i])
dfs(i);
}
```
### Problems
- [Codeforces 1020B. Badge (Very Easy)](https://codeforces.com/contest/1020/problem/B)
- Try to solve the problem in O(N)!
- [The Bovine Shuffle (Normal)](http://usaco.org/index.php?page=viewproblem2&cpid=764)
- [Swapity Swapity Swap (Very Hard)](http://www.usaco.org/index.php?page=viewproblem2&cpid=1014)
- Graphs
- [CSAcademy Lessons](https://csacademy.com/lessons/)
- CPH 11, 12
- Terminology
- ex. bipartite graphs??

View file

@ -1,13 +1,10 @@
# Silver - Sorting
See 8 & 12 & 14.1 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a
- Comparators
- CPH 3
- std::sort / Collections.sort
- coord compress
## Binary Search
See 8 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a
- CPH 3
- std::sort / Collections.sort
- coord compress
## Two Pointers
- CPH 8.1
See 12 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a

View file

@ -1,5 +1,7 @@
# Gold - Minimum Spanning Tree
Standard Problems:
- [Kattis Minimum Spanning Tree](https://open.kattis.com/problems/minspantree)
- [CSES Road Reparation](https://cses.fi/problemset/task/1675)
- equivalent to above
@ -14,7 +16,7 @@
- [cp-algo 1](https://cp-algorithms.com/graph/mst_kruskal.html)
- [cp-algo 2](https://cp-algorithms.com/graph/mst_kruskal_with_dsu.html)
- Requires "Disjoint Set Union" (DSU) data structure
- [CSAcademy Disjoint-Set](https://csacademy.com/lessons)
- [CSAcademy Disjoint-Set](https://csacademy.com/lesson/disjoint_data_sets)
- DSU Complexity Proofs (optional of course)
- [log\*n](https://en.wikipedia.org/wiki/Proof_of_O(log*n)\_time_complexity\_of_union%E2%80%93find)
- [a(m,n)](https://dl.acm.org/doi/pdf/10.1145/321879.321884)
@ -28,7 +30,7 @@
- also special ...
- DSU Problems
- [Mootube](http://www.usaco.org/index.php?page=viewproblem2&cpid=789)
- same as [CSES Road Construction](https://cses.fi/problemset/task/1676)
- same as [CSES Road Construction](https://cses.fi/problemset/task/1676)
- [Closing the Farm](http://www.usaco.org/index.php?page=viewproblem2&cpid=646)
- [Favorite Colors](http://www.usaco.org/index.php?page=viewproblem2&cpid=1042)
- fairly tricky

View file

@ -22,7 +22,7 @@ The above approach can be generalized. Suppose that you want to find the $K$ obj
### Problem
You're given a graph with $N\le 50$ vertices and at most $\binom{N}{2}$ edges, and you want to find the $K$-th ($K\le 10^4$) smallest spanning tree.
Given a graph with $N\le 50$ vertices and at most $\binom{N}{2}$ edges, find the $K$-th ($K\le 10^4$) smallest spanning tree.
### Solution
@ -114,6 +114,10 @@ Note that none of these options result in a robot of lower cost since we assumed
Since there exists exactly one way to get from the cheapest robot to every possible robot, we can just use a priority queue.
<details>
<summary>My Solution</summary>
```cpp
#include <bits/stdc++.h>
using namespace std;
@ -160,6 +164,8 @@ int main() {
}
```
</details>
## Other Problems
* [Baltic OI 2019 - Olympiads](https://boi2019.eio.ee/tasks/)

View file

@ -9,15 +9,17 @@ Links:
* [zscoder](https://codeforces.com/blog/entry/47821)
* [Kuroni](https://codeforces.com/blog/entry/77298)
From the latter link:
From the latter link (modified):
Slope trick is a way to represent a function that satisfies the following conditions:
* It can be divided into multiple sections, where each section is a linear function (usually) with an integer slope.
* It is a convex/concave function. In other words, the slope of each section is non-decreasing or non-increasing when scanning the function from left to right.
> Slope trick is a way to represent a function that satisfies the following conditions:
>
> * It can be divided into multiple sections, where each section is a linear function (usually) with an integer slope.
> * It is a convex/concave function. In other words, the slope of each section is non-decreasing or non-increasing when scanning the function from left to right.
It's generally applicable as a DP optimization.
This document assumes some familiarity with at least one of the links above.
## A Simple Example
[CF Buy Low Sell High](https://codeforces.com/contest/866/problem/D)