Presentation is loading. Please wait.

Presentation is loading. Please wait.

Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

Similar presentations


Presentation on theme: "Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >"— Presentation transcript:

1 Last Meeting Of The Year

2 Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c > 0; c /= 10) digsum += c % 10; int M = digsum; for(int d1 = 2; d1 * d1 <= a; ++d1) if(a % d1 == 0) M = max(M, digsum + getdp(d1) + getdp(a / d1)); return DP[a] = M; } int main() { int n; cin >> n; cout << getdp(n) << "\n"; return 0; }

3 Problem I have N circles and K demands. I want to color the N circles. o Demand i states that circle A_i and B_i must be colored the same color. I want to use as many colors as possible cause I'm fabulous How many different colors can I use? Solution o Union Find! o Circles that must be the same color are in the same set. o For each demand, merge A_i and B_i. o At the end, count how many sets there are.

4 The Union-Find Algorithm Maintain a collection of disjoint sets. o Each element belongs to a set. o Each set has a "representative" element. What you can do with Union-Find o FIND(X)  Find which set element X belongs to.  Returns representative of X's set. o UNION(X, Y)  Merge the two sets containing elements X and Y.  If X and Y are already in the same set, nothing happens. o O(inverse-ackermann(n)) amortized runtime  In other words, very fast!! (almost constant time)

5 How It Works Each set is stored as a tree. o The root is the representative element of its set. Every element has a parent. o The parent of a root is itself. o Initially, each element is in its own set. FIND(X) returns the root of X. if (parent[X] == X) return X; else return FIND(parent[X]); UNION(A,B) merges A and B's trees. o Put A's root under B's root. parent[FIND(A)] = FIND(B); http://research.cs.vt.edu/AVresearch/UF/

6 Example Solution int[] par; int FIND(int x) { if (par[x] == x) { return x; } return FIND(par[x]); } void UNION(int x, int y) { x = FIND(x); y = FIND(y); par[x] = y; // if x == y, this does nothing! }

7 Example Solution Continued for(int i = 0; i < N; ++i) par[i] = i; // initially, all circles are disjoint. for(int i = 0; i < K; ++i) { int a, b;... UNION(a, b); // a and b must be in the same set. } int cnt = 0; // number of sets is just number of roots. for(int i = 0; i < N; ++i) { if (par[i] == i) { // check if i is a root. cnt++; } return cnt;

8 How to make it fast Union Find needs optimizations to become fast. Optimization 1 - Path Compression o Once you find an element's root, set that root as its parent. Optimization 2 - Union By Rank o The rank of a set is the height of its tree. o When merging sets A and B, put B under A if B is shorter than A. o This keeps the trees shorter These two optimizations combined make the overall average runtime nearly constant.

9 UNION-FIND With Optimization int[] par, rank; int FIND(int x) { if (par[x] == x) return x; return par[x] = FIND(par[x]); } void UNION(int a, int b) { a = FIND(a); b = FIND(b); if (a == b) return; if (rank[a] > rank[b]) swap(a, b); par[a] = b; rank[b] = max(rank[b], rank[a] + 1); }

10 Applications Union-Find is used in Kruskal's algorithm for Minimum-Spanning Trees. Union-Find can be used in place of DFS when searching for connected components or cycles. Union-Find can be used in many other problems. o Very common in USACO Silver and Gold contests. o Also on Codeforces Path-Compression can be extended to other trees sometimes.

11 POTW There is a river, represented by a grid with width W o All squares (x,y) with 0 <= y < W are part of the river. In the i-th minute, Andy throws a rock into the river, which fills a unit square with coordinate (x_i, y_i). [0 <= y_i < W] Andy wants to block off flow from west to east. How many rocks will he throw before this goal is first achieved? o Water cannot flow over stones or between stones o It can, however, flow east to west if necessary

12 Beautiful POTW Diagram

13 Input Format and Constraints Input Format o Line 1: Two integers, W, T  The width of the river  The number of minutes Andy spends throwing rocks. o Lines 2...T+1: Two integers x_i, y_i. Output Format o Line 1: The number of rocks Andy throws before blocking off the flow, or - 1 if it cannot be done. Constraints o 15 points: W, T < 10. |x_i| < 10 o 35 points: W, T < 1000. |x_i| < 1000 o 50 points: W, T < 100000, |x_i| < 10^9

14 Sample Case Input: 3 5 1 2 1 3 1 2 0 -1 1 Output: 4 widthwidth


Download ppt "Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >"

Similar presentations


Ads by Google