Graph Problems

Shortest Path

Dijkstra, Bellman Ford, Floy Warshall

BFS / DFS

Minimum Spanning Tree

Bipartite Graph

 

================================================

 

C - Social Distance on Graph 

F - Merge Set

Virtual vertices technique. 

 

 

F. Timofey and Black-White Tree 

References: 

https://zhuanlan.zhihu.com/p/601326343

https://www.cnblogs.com/onlyblues/p/17077001.html

 

 

Reachable Nodes In Subdivided Graph (Dijkstra)

 

E - Ring MST (Minimum Spanning Tree)

 

Total number of vertices is too big, so think it in this way: essentially the goal is to reduce the number of connected components from N to 1 if possible. For each iteration, think about how many fewer connected components we will have by using the current kind of operation, running GCD

 

C - Keep Graph Connected (Spanning Tree)

Prove that for a given tree, there is always a valid assignment. After this, we just need to find any spanning tree, root it, assign 1 to the root node and do a dfs to make assignments to the rest of N - 1 nodes. 

 

 

To-do

B - A < AP

E - Choose Two and Eat One

E - Count Descendants

F - Pay or Receive

E - Red and Blue Graph (think about the degree of each vertex)

 

E - Swap Places (Bfs on all possible node / color combinations)

 

E - Travel by Car  (Dijkstra, Floyd-Warshall)

 

B - Reversible Cards

Connected components is a tree or not? if tree, how many vertices can we take? if not, how many vertices can we take? 

This problem can be solved using DFS or UnionFind

DFS: https://atcoder.jp/contests/arc111/submissions/25510684

UnionFind: https://atcoder.jp/contests/arc111/submissions/25510737

 

AtCoder D - Connectivity

Given N graph nodes and 2 different sets of edges, forming 2 different graphs.  For each node, find out the number of nodes that are connected to it in both graphs. A node is always connected to itself in either graph, so the answer for each node is at least 1.

 

Solution:

1. dfs on each graph and label all connected components.  For two different nodes n1 and n2, if they are connected in both graphs, it means their labels in each graph are the same, label(n1 in graph 1) == label(n2 in graph 1) && label(n1 in graph 2) == label(n2 in graph2).

2. Sort all nodes' labels in ascending order of graph1 labels then ascending order of graph2 labels, collect all nodes that have the same labels and update answer.

 

 

AtCoder D - Moving Piece

N is up to 5000, so a O(N^2) solution is efficient enough. 

Key observation: a permutation of 1 to N with P[i] != i formes at least 1 cycles, and each cycle is independent with each other. So we can consider each cycle separately. 

1. precompute each cycle's node count and value sum in O(N) time.

2. for each possible starting node S, consider all of the possible ending node E and the maximum sum from S to E. 

If S and E are in a cycle with <= 0 cycle sum, then it is optimal to use the cycle. 

If cycle sum is > 0, then it is optimal to use as many cycles as possible, before ending at node E.  Cycle usage times = (K - moves used from S to E) / Cycle count.

If K < cycle count, then it is a simplified version of the general case, with cycle usage always as 0.

 

Extra challenge(Open question): Can you solve this problem in O(N) time?

 

 

AtCoder D - People on a Line 

 

AtCoder D - RGB Coloring 2

 

D - Wizard in Maze:  Can be solved using 0-1 BFS  

https://cp-algorithms.com/graph/01_bfs.html

 

D - Candidates of No Shortest Paths

a variation on dijkstra. Since N is only up to 100, can you solve it using O(N^3) Floyd-Warshall all pairs shortest path algorithm? 

 

D - Score Attack: a variation on Bellman ford shortest path algorithm. But we only care if there exists a positive cycle on the path from node 1 to N. If a positive cycle is not on one of the paths from 1 to N, we do not care. 

 

D - joisino's travel: Floyd-Warshall + Permutation.  Floyd-Warshall must have the iteration of intermediate nodes as the outer-most loop.

 

D - Restoring Road Network: think about how intermediate nodes are used to compute the shortest path in Floyd-Warshall algorithm.

 

C - Bridge:  Can you solve this problem using the O(N) graph bridge detection algorithm?

 

B - Unplanned Queries:  Since we only care about the parity, can you convert a query ai, bi into something that invovles a fixed root vertex? 

 

D - String Equivalence: N is small, so we can use DFS. The key here is how to only generate valid string?  The condition can be rephrased to:

s[1] = a, And

s[k] <= Max{s[1], s[2],.... s[k - 1]} + 1, for k in [2, N]. 

If the last added character C is the max so far, then recursively dfs with C + 1, otherwise dfs with the current max. 

Why does this work? Kinda hard to see this, But in this problem, walking through the transition from N = 3 to N = 4 helps to identify this pattern: Each time a new character is added in the end, it must be in the range ['a', current max character + 1]. 

 

D - Small Multiple: 0-1 BFS

 

https://www.cnblogs.com/lz87/p/17242229.html (dijkstra, flyod warshall)

A. Timofey and a tree (dfs, constructive)

A. Maze (dfs / bfs. Reverse thinking: We want to have S - K cells connected as the final state.)

D. Valid BFS? (Bfs, Sorting. ) 

Sort adjacency lists based on the given input BFS sequence; Then do bfs on the input tree and compare if the traversal sequence is exactly the same with the input BFS sequence.

C. Anna, Svyatoslav and Maps (APSP, Greedy)

E - MST + 1 (Kruskal's MST) Process all queries at the same time and hypothetically try to add a query
edge when running Kruskal's MST.

C. Peaceful Rooks (Graph Cycle, Union Find)

Couples Holding Hands (Union Find) https://www.cnblogs.com/lz87/p/16037601.html

E - Bishop 2 (0-1 BFS)

2290. Minimum Obstacle Removal to Reach Corner(0-1 BFS)

E - Skiing(Dijkstra with potential function to offset negative weighted edges) https://codeforces.com/blog/entry/99506

The Maze III (Dijkstra)

Checking Existence of Edge Length Limited Paths II (Union Find, Kruskal's MST, Binary Lifting)

E - Road Reduction (Dijkstra generates a spanning tree, not necessarily MST). Related Problems :
882. Reachable Nodes In Subdivided Graph

 

2295. Replace Elements in an Array

 

Minimum Cost to Reach Destination in Time (Similar with Bellman Ford)

 

803. Bricks Falling When Hit (Reverse thinking; Union Find)

 

D - Jumping Takahashi 2 (Binary Search, DFS, BFS, Dijkstra, Floyd-Warshall) https://atcoder.jp/contests/abc257/editorial/4222

 

1632. Rank Transform of a Matrix (Union Find)

 

C - Blue Bird (Dijkstra) (https://codeforces.com/blog/entry/68073)

 

D - 高橋くんと木の直径 (Double sweep to find tree diameter. (It works on weighted non-negative tree edges too))

 

2003. Smallest Missing Genetic Value in Each Subtree

 

1036. Escape a Large Maze (BFS, Hash table)

 

F - Transportation (Kruskal's MST)

 

E - Range Sums (Union Find)

 

E - Subsequence Path

 

E - Hopscotch Addict(Dp ?)

 

C - アットコーダー王国の交通事情(dijkstra, flyod warshall)

 

posted @ 2021-07-27 05:19  Review->Improve  阅读(135)  评论(0)    收藏  举报