By Vladosiya, history, 22 months ago, translation,

1702A - Round Down the Price

Idea: MikeMirzayanov

Tutorial
Solution

1702B - Polycarp Writes a String from Memory

Idea: MikeMirzayanov

Tutorial
Solution

1702C - Train and Queries

Idea: MikeMirzayanov

Tutorial
Solution

1702D - Not a Cheap String

Idea: MikeMirzayanov

Tutorial
Solution

1702E - Split Into Two Sets

Idea: MikeMirzayanov

Tutorial
Solution

1702F - Equate Multisets

Idea: MikeMirzayanov

Tutorial
Solution

1702G1 - Passable Paths (easy version)

Idea: MikeMirzayanov

Tutorial
Solution

1702G2 - Passable Paths (hard version)

Idea: MikeMirzayanov

Tutorial
Solution
• +74

| Write comment?
 » 22 months ago, # | ← Rev. 2 →   0 Problem Solved
 » 22 months ago, # |   +3 Sorry, can someone explain me, how to solve problem E with DSU and how these formulas works. I only realized that if two elements should be in different sets, the formula will be to unite(x, y + n), unite(y, x + n). And if in one then unite(x,y), unite (x + n, y + n)
•  » » 22 months ago, # ^ | ← Rev. 2 →   +8 Uniting dominos sharing the same number and checking if there is a set of odd size would suffice.
•  » » 22 months ago, # ^ |   +3 As Editorial said, each number must exist exactly two times, i.e. every node has exactly two edges. Graphical representation of this will be just a set of disjoint cycles. Just use DSU to check if any of the cycles is odd lengthed.
•  » » 22 months ago, # ^ |   +4 0...n-1 ==> blue n...2n-1 ==> red In bipartite graph each edge end (edge: x-y) should have different colors so (Blue, Red) or (Red, Blue). so we unite(x,y+n) or(x+n,y) In Bipartite odd cycle doesn't exist. since cycle must start with vertex x and end with vertex x. Lets say cycle starts with vertex x of red or vice versa and start assigning colors alternatively then if it is Even cycle ends with vertex x of red [ x(Red) -> y(Blue) -> x(Red) ] Odd cycle end with vertex x of blue [ x(Red) -> w(Blue) -> z(Red)-> x(Blue)] so if x(Red) and x(Blue) which is (x,x+n) belongs to same component then odd cycle exists. Hence answer is "NO". 
•  » » » 22 months ago, # ^ |   0 why can we add an edge between the two sides of the domino, because they must be in the same component?
•  » » » » 22 months ago, # ^ | ← Rev. 2 →   +1 First, if any number appears more than $3$ times, print NO. For $n$ dominoes entered, it contains $2n$ numbers. According to Pigeonhole Principle, every number appears inevitably $2$ times. If we think of numbers as vertices (graph theory) and dominoes as edges (graph theory), the graph $G$ will be constructed by many cycle (graph theory).Consider two dominoes $a=\lbrace 1, 3 \rbrace$ and $b=\lbrace 1, 4 \rbrace$. Because they have a common number $1$, they must be placed in different sets. Without loss of generality, we can think of $a$ as the outgoing edge and $b$ as the incoming edge. It's like we dyed the edges in Red and Blue. two different colors. In a circle (graph theory's circle), bijection from edge to point can be constructed. So we can transform the edge dyeing problem into the vertex dyeing problem.
•  » » 16 months ago, # ^ |   0 I solved using DSU.https://mirror.codeforces.com/contest/1702/submission/189937922The reasoning: If a pair {a, b} exists, then we know that another pair that contains {b, c} must be in a different set. We can visualize this by creating 2n nodes (one node if the ith pair is in the first set, and another node if the ith pair is in the second set). In my code, I used Node "a" to denote that a is in the first set, and Node "a + n" to denote that a is in the second set.We can create 2 edges per pair. Connect {a, b + n} and {a + n, b} for every pair {a, b}. This is because if we know that "a" is in the first set, then b must be in the second set, and vice-versa.It's easy to see that a solution only exists if Node a is not connected to Node a + n.
•  » » » 9 months ago, # ^ |   0 thank you very much that was very helpful
 » 22 months ago, # |   +6 Video Solutions for the complete problemset.
•  » » 22 months ago, # ^ |   0 Very helpful thank you!
 » 22 months ago, # |   +1 Problem C had anti hash test cases got accepted during contest but TLE after the hacking phase :)
•  » » 22 months ago, # ^ |   0 same problem
•  » » 22 months ago, # ^ |   0 same problem
 » 22 months ago, # | ← Rev. 2 →   0 We can use priority queue to solve problem F yet the implementation is the same.
•  » » 22 months ago, # ^ |   0 Yes
 » 22 months ago, # | ← Rev. 2 →   -6 Analysis of the third problem: just to start, we need to store the start and end positions of each individual number. And then we compare if the first position of a[j] is less than the last position of b[j] then the answer is YES, otherwise the answer is NO: ll n, k; cin >> n >> k; map < ll , ll > first; map < ll , ll > last; for (int i = 1; i <= n; i++) { ll u; cin >> u; if (first[u] == 0) first[u] = i; last[u] = i; } while (k--) { ll a, b; cin >> a >> b; if (first[a] == 0 || first[b] == 0) { cout << "NO\n"; continue; } if (first[a] < last[b]) { cout << "YES\n"; } else { cout << "NO\n"; } }
•  » » 22 months ago, # ^ |   0 hey, I did the same but I used unordered_map and it showed tle but when I used map only got AC why?
•  » » » 22 months ago, # ^ | ← Rev. 2 →   0 unordered_maps are ridiculously slow in C++, and can easily be hacked to TLE. Someone must have hacked an unordered_map solution, and that hack would have made it into the test cases. map on the other hand is kinda fast (sometimes even faster than unordered_map), and will give AC because the log(n) factor doesn't impact the solution much. use map, not unordered_map, or you will be hacked
•  » » » » 22 months ago, # ^ |   0 Or you can use unordered_map with a custom hash function that uses chrono::steady_clock::now().time_since_epoch().count()
•  » » » » » 22 months ago, # ^ |   0 Well typically this is wrapped in a mt19937 rng, but yeah you can.
•  » » » » » » 22 months ago, # ^ |   0 thanks
•  » » » » 22 months ago, # ^ |   0 Yeah, initially I too used unordered maps/sets a lot and got unexpected TLEs which is never fun. Although, in some rare cases, we don't have log(n) space. Here is a custom hash that I use when using unordered maps/sets is the only way. Hope this helps. Custom Hashstruct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } };  Usageunordered_set _set; unordered_map _map; 
 » 22 months ago, # |   0 Implemented Q3 as it is in the tutorial. But still getting TLE for Python Soulution. Any improvements?https://mirror.codeforces.com/contest/1702/submission/163751004
 » 22 months ago, # | ← Rev. 2 →   +3 Problem F solved using 3 approaches:Using PriorityQueue+Editorial idea: https://mirror.codeforces.com/contest/1702/submission/163692721Storing counts of b prefixes: https://mirror.codeforces.com/contest/1702/submission/163692721
 » 22 months ago, # |   0 I solved G2 using Heavy-light Decomposition during contest. After finding both ends of the path, we can do a lazy range addition of 1 on the path between them in the HLD. Now every node in the query must have the value 1. Again range add -1 to reset the HLD.https://mirror.codeforces.com/contest/1702/submission/163628298
•  » » 22 months ago, # ^ |   0 i used hld too, but i've added 1 to every node in the query and then calculated sum on path so, if it equals to k, the answer is yes, otherwise no
•  » » 22 months ago, # ^ |   +4 Once you find the both ends of the path you actually don't need HLD though. If a node $w$ lies on the path between $u$ and $v$ then $dist(u,w)+dist(w,v)=dist(u,v)$ so just checking this for every node in each query gonna suffice.
 » 22 months ago, # |   +1 Can someone explain the dfs function of Tutorial of G1? I understand that first we precalculate each node's depth and choose the deepest node as the start point of our dfs as it will be one of the end points of our path, but I can't understand the dfs working.Thanks...
•  » » 22 months ago, # ^ |   0 The idea like this: Denote the set of selected vertexes as S. And we already get the start point which has deepest depth and denote it as root. And in a DFS from the root, we say a vertex is a leaf if it is selected and none of its children vertexes is selected. If the selected vertexes can build a path, we should have only one leaf if we DFS from the root. So just calculate how many leaves we have. You can check whether this submit is easier to understand: https://mirror.codeforces.com/contest/1702/submission/163945410
 » 22 months ago, # | ← Rev. 4 →   0 Problem E: Codevoid solve(){ int n; cin >> n; unordered_mapmp1,mp2; bool flag = true; rep(i,n){ int a,b; cin >> a >> b; if(a==b) flag =false; if(flag){ if(mp1.find(a)==mp1.end() && mp1.find(b)==mp1.end()){ mp1[a]=0, mp1[b]=0; } else if(mp2.find(a)==mp2.end() && mp2.find(b)==mp2.end()){ mp2[a]=0, mp2[b]=0; } else flag = false; } } if(flag){ py; } else pn; } Which case is my solution missing??
•  » » 22 months ago, # ^ |   0 1 6 1 2 2 3 4 5 3 4 5 6 6 1 Your answer is NO.Now if we swap the order of input to: 1 6 1 2 2 3 3 4 4 5 5 6 6 1 Then, now your answer is YES.
 » 22 months ago, # |   0 solved g1 later since I didn't have time to do in the contest. just bashed it with lca 163805347
•  » » 22 months ago, # ^ |   0 Same
 » 22 months ago, # |   0 Can somone please explain to me why when using unordered_map I get TLE and when changed to a normal map it just get accepted.
 » 22 months ago, # | ← Rev. 3 →   0 I solved Problem G2 without LCA. https://mirror.codeforces.com/contest/1702/submission/164039469164039469
 » 22 months ago, # |   +1 I wanted to mention that F appeared in a recent AtCoder contest: https://atcoder.jp/contests/abc254/tasks/abc254_h
 » 22 months ago, # |   0 Hello,In problem E, is it obligatory that the two sets must have equal size. If yes, where is problem statement this is written? Thank you :)
•  » » 22 months ago, # ^ | ← Rev. 4 →   0 There are $n$ dominos with value from $1$ to $n$.If each number appear exactly twice, both sets must contain all number from $1$ to $n$, thus have the size of $\frac{n}{2}$ Edit : the size is $n$, not $\frac{n}{2}$, my bad
•  » » » 22 months ago, # ^ |   0 Each number from 1 to n appears twice. So, the size of each set is n.
 » 22 months ago, # |   0 My solution to G2 with dfs and range query data structure(BIT for example):First get the pre-order sequence of the tree, store the time stamp when you enter/exit each node.For each query, find the node X with max depth, and node Y with min depth.As described in the solution, X must be one end of the path. Let's enumerate the other end.We put +1 on the timestamp you enter each node, and range_sum(X,Y) gives us the number of points covered by path X-Y.Now we enumerate each node Z in the set, skip it if it's on path X-Y(can be determined by timestamp), otherwise see if range_sum(X,Y) + range_sum(Y,Z) = |S| + 1.
 » 22 months ago, # |   0 In problem F, why do we have to take the largest number from array b? The solutions works even if we take random number from b.
•  » » 22 months ago, # ^ |   0 I would assume that it would be easier to just start from the largest number. Do you have a proof as to why it would still work even if we just take a random number from b?
•  » » 22 months ago, # ^ |   0 I think you start with a larger number, because in the worst case that you divide it, you could still use it for smaller numbers. That's much more efficient than taking smaller numbers, because you don't know if you'll be able to use them again.
•  » » » 21 month(s) ago, # ^ | ← Rev. 2 →   0 tgp07 Can You help where my approach went wrong in problem F ? My basic idea was to reduce every number in array A and B to their largest odd factors (by dividing them 2 as long as possible) and store the values in two different multisets. First multiset contains all reduced values of array A. The second multiset contains all the reduced values present in array B. Then I would simply iterate over the second multiset and for each value in this multiset I will check if there is a matching value in first multiset. If yes then I will delete that value from first multiset else I will further reduce this element further by 2 and again try to match with first multiset as long as this element is > 0. >br> After iterating over all values of 2nd multiset, if first multiset is empty then my answer is YES, otherwise it is NO.UPDATE : I figured out the issue, which was due to continue statement in one of my while loops. The wrong submission here
•  » » » » 21 month(s) ago, # ^ |   0 Nice
 » 22 months ago, # |   0 Can u explain why Graph solution for Problem E works?
 » 21 month(s) ago, # |   0 Average round
 » 17 months ago, # |   0 can someone tell at which test case my code for E fails 186018543
•  » » 5 months ago, # ^ |   0 did you find out why?
•  » » » 5 months ago, # ^ |   0 no
 » 15 months ago, # |   0 In problem F,once we make each element odd in multiset a, then I think it is not necessary to start with largest element in multiset b, we can start with smallest element too since we use only second operation in multiset b.
 » 5 months ago, # |   0 Can anyone be kind enough to see my solution for problem E ones. https://mirror.codeforces.com/contest/1702/submission/237575015 My logic was to check if there is any domino which has both side the same number, if so then output "NO"; else made 2 sets and inserting the number on dominoes after checking if any of the number already exist in any one of them. If so, then output "NO"; else "YES";
•  » » 6 weeks ago, # ^ | ← Rev. 2 →   0 Try this test case 1 6 1 6 3 4 2 3 1 2 5 6 4 5  1 6 -> S1: 1,6 — S2: empty 3 4 -> S1: 1, 3, 4, 6 — S2: empty 2 3 -> S1: 1, 3, 4, 6 — S2: 2 3 here when it checks 1 2, it will find 1 in S1 and 2 in S2, so it will output NO while the correct answer is YES. That's because the solution tries to build the 2 sets based on the order of the dominos in the input which is not optimal every time.