### AliShahali1382's blog

By AliShahali1382, history, 2 months ago, Hi, Codeforces!

1529A - Eshag Loves Big Arrays

problem idea and solution: AmShZ

editorial
official solution

1529B - Sifid and Strange Subsequences

problem idea and solution: -zeus-

editorial
official solution

1528A - Parsa's Humongous Tree

problem idea and solution: shokouf, AmShZ

editorial
official solution

1528B - Kavi on Pairing Duty

problem idea and solution: alireza_kaviani

editorial
official solution

1528C - Trees of Tranquillity

problem idea and solution: AliShahali1382

editorial
official solution
better implementation of official solution: AaParsa
python solution: Tet
another O(n.log) solution with seg-tree: N.N_2004

1528D - It's a bird! No, it's a plane! No, it's AaParsa!

problem idea and solution: AmShZ, AliShahali1382

editorial
official solution
a different O(n^3) solution: Atreus

1528E - Mashtali and Hagh Trees

problem idea and solution: AliShahali1382

Thanks to Kininarimasu for the editorial.

editorial
official solution

1528F - AmShZ Farm

problem idea and solution: AmShZ, AliShahali1382

editorial
official solution Tutorial of Codeforces Round #722 (Div. 1) Tutorial of Codeforces Round #722 (Div. 2)  Comments (174)
 » Auto comment: topic has been updated by AliShahali1382 (previous revision, new revision, compare).
 » Woow !! Fast editorial Thanks :)
 » 2 months ago, # | ← Rev. 3 →   There might be an error in the n log n solution with segtree (div1. problem C).
•  » » fixed, thanks!
 » I hope all the future rounds will be as good as this one
•  » » I hope all future roundsTo be continued... :(
 » Nice contest with nice problems! Thanks
 » Genius problems. Nice job.
•  » » » 2 months ago, # | ← Rev. 2 →   Bad tests in D? The $\log n$ part in my $\mathcal{O}(nm \log n)$ code triggers only $0.025 N^3$ times in large tests: 117257732EDIT: dorijanlendvaj found a large input where it triggers $0.5 N^3$ times, that's $20$ times better than the original test data!
•  » » we wanted to fail high-constant O(n^3.log)(like seg-trees) and let easily optimized O(n^3) or O(n^3.log) with heaps to pass.
 » 2 months ago, # | ← Rev. 2 → Via 1529B — Sifid and Strange Subsequences
•  » » This is what happens when you don't help in making statements. :(
 » 2 months ago, # | ← Rev. 3 →   Dfs Based Solution of C Parsa's Humongous Tree without DP based on the fact that during path traversal on a tree from root , A particular node is reached only once.We return a pair {a,b} from every node.A has the best solution from that node to all nodes below it when left_mini is assigned to that node.B has the best solution from that node to all nodes below it when right_maxi is assigned to that node.We store best possible values of the current node from all children node i.e. one with maxi on all possible calls and other with mini and then return it.In the end when recursive calls end print the maximum of the pair of values returned from the root.Solution
•  » » What you are describing is the same DP on a tree. The official solution also uses DFS.
•  » » » U don't need to save results in a array for Dp in this DFSBoth best case possibilities are calculated and return from there.
 » Where is the editorial on F?..
•  » » I've added it but it doesn't seem to appear ... will probably get fixed soon
 » 2 months ago, # | ← Rev. 3 →   For Div2 D- "x≤n: In this case, due to the lemma mentioned above all the segments must have the same length, thus their length must be a divisor of n, in this case they can be paired in D(n) ways; where D(n) is the number of divisors of n.".The equality should not be present I think. It should be only x
•  » » If 1 is paired with n, each number from n+1 to 2n must belong in a segmemt with length n, where the length of a segment is defined as the number of elements inside it. The pairing is unique as well ...
 » Spoiler •  » » The sequence x[i]-2*x[i-1] starts 1,0,1,-1,2,... and is http://oeis.org/A051950
 » I have done Question b in two way 1st submission : 117264950 2nd submission : 117264953 I can't find why my second submission failed I did the same thing but just in another manner, can anyone help me out please
•  » » It took me a lot of time but I found out what's wrong with your code. Actually, you are failing edge case when seq is [-1] or  because in your second program you'll be having cnt = 1 and since there is only 1 item in the array it will not enter the while loop, and bool will evaluate to true and it will increase it by 1 resulting in ans = 2. Luckily your former code worked for these edge cases.
•  » » » First thank you very much for your efforts Could you please help me this submission , it is failing on 10000th test case 117240464
•  » » » » I have submitted your code with a minor change, it got ac. You just had to account for the special case when n equals 1.
 » 2 months ago, # | ← Rev. 2 →   i am sorry for every one get wrong answer in problem B test 15 i put it in hacks :( can you give contriubution for it
 » Can anyone explain the DP applied in Kavi on the Pairing Duty problem in simple words?
 » In Div 1 Problem D:"To handle the "waiting" concept, we can add n fake edges, i-th of which is from the i-th vertex to the (i+1 mod n) -th vertex with weight equal to one." What is a vertex here? Does this sentence mean the cities? But waiting for 1 second doesn't move you to the next city, so that can't be. Or can it? Is the assumption, that "if we can reach city X from some other city Y, then we could've waited for 1s in Y and then we would've reached X+1"? That seems also to be the explanation for "It can be proved that doing dijkstra in the new graph is sufficient if we guarantee that the first used edge is not fake."Well, It seems I answered my own question here while writing it down. Debugging-Rubber-Ducky for the win. But since I really was confused by this and I don't feel that this step is trivial, I'm gonna leave this comment here.
•  » » "It can be proved that doing dijkstra in the new graph is sufficient if we guarantee that the first used edge is not fake.We can map waiting for x seconds and then using an edge to go to u from v to using a cannon and then using x fake edges to go to u from v."
•  » » » Oh, yes you are right. I'm just thick as a brick and didn't get that sentence before. Thanks for the heads up! :)
 » Can someone explain how to find Stirling numbers of the 2nd kind in O(k log k) (for F)?
•  » » 2 months ago, # ^ | ← Rev. 2 →   From wikipedia page \begin{gather*} S(n, k) = \frac{1}{k!}\sum_{i=0}^k (-1)^i \binom{k}{i} (k-i)^n \end{gather*}You can rewrite it as \begin{gather*} S(n, k) = \sum_{i=0}^k \underbrace{\left((-1)^i\cdot \frac{1}{i!}\right)}_{a_i} \cdot \underbrace{\left(\frac{1}{(k-i)!}\cdot(k-i)^n\right)}_{b_{k-i}} \end{gather*}So you can calculate arrays $a$ and $b$, and stirling numbers are just convolution of these arrays:\begin{gather*} S(n, k) = \sum_{i=0}^k a_i \cdot b_{k-i} \end{gather*}
 » I am getting TLE for Div 2 C Mysolution. I have done almost the same as Editorial (using memorization of recursion). Please anyone help me with debugging. Thank you
•  » » 2 months ago, # ^ | ← Rev. 2 →   unordered_map and unordered_set are dangerous in CP
•  » » » Thank you for replying. I want to know What is the exact reason not to use unordered_map and unordered_set.
•  » » » »
•  » » » » » Thank You.It helped :)
•  » » » » Use hashed data structure when you are sure that key is not randomized , if it is consecutive then feel free to use it. Ex:- reverse mapping an array to its index when array is permutation. Never use this when array can be randomly any number because test case can be made so make lots of collisions.
•  » » » *in CF not CP
•  » » 2 months ago, # ^ | ← Rev. 2 →   I am getting a TLE too on my Div2C. Can someone help me with debugging too ? Thank You. [My Solution]http://codeforces.com/contest/1529/submission/117308814)
•  » » » » Corrected. Thanks
•  » » » just add fastIO to your submission bc input quite large. Same code but has fastIO
 » 2 months ago, # | ← Rev. 11 →   Since E editorial is not out yet, here is how I solved. Let $dp_i$ be the answer for all trees such that there exists a root and all edges are directed in the same direction from root and the root has at most $2$ children. We transition $dp_i$ = $dp_{i-1} + dp_{i-1}*pdp_{i-2} + dp_{i-1}*(dp_{i-1}+1)/2$ where $pdp_i = \sum_{j=0}^i dp_j$Then let $dp2_i$ be the same as $dp_i$ except the tree must have $2$ children. So $dp2_i = dp_i - dp_{i-1}$.The answer for these cases is $2*(dp_n + dp_{n-1}*pdp_{n-2}*(pdp_{n-2}+1)/2 + pdp_{n-2}*dp_{n-1}*(dp_{n-1}+1)/2 + dp_{n-1}*(dp_{n-1}+1)*(dp_{n-1}+2)/6) - 1$This is because $dp_n$ holds the answer for at most $2$ children and the other section accounts for the rest. We multiply by $2$ to account for both edges directions, and subtract $1$ because a single path is isomorphic.This obviously doesn't handle all cases, but all other cases can be found in the following form. Let $t_{up,k}$ be a tree where the root has $2$ children and the edges are directed up and the longest path is $k$, and let $t_{down,k}$ be a tree where the root has $2$ children and the edges are directed down and the longest path is $k$. Then all other cases are $t_{up,k}$ which exists on some path of length $l$ to and connects to $t_{down, n-k-l}$We can count every other case as $\sum_{i=0}^{n-1} (dp_i-1)*dp2_{n-1-i}$ This works because we pretend the path is always length $1$, then if we do $dp_i*dp2_{n-1-i}$ we handle all cases except for when the $t_{up, k}$ is empty, and that only happens one time.Here is my submission: 117272002
•  » » 2 months ago, # ^ | ← Rev. 2 →   Sorry I understand everything now D: took me too long
•  » » 2 months ago, # ^ | ← Rev. 3 →   Good!
•  » » We can count every other case as ∑n−1i=0(dpi−1)∗dp2n−1−i????
 » humongous amogus sus sus sus sus sus
 » 2 months ago, # | ← Rev. 2 →   In problem B: my solution passed the pretests but giving time limit exceeded in test 4. It runs in O(nlogn) time l. Here is my submission Can someone help me finding what's wrong in my code?
•  » » In your code's solve, second range must be time limit exceeded.the range is $O(n)$.And the min_element also is $O(n)$.so it's $O(n^2)$.
•  » » » 2 months ago, # ^ | ← Rev. 3 →   [Deleted]
 » can anybody please tell in B why its optimal to always choose negative elements
•  » » the time complexity of min_element() is O(n), you are doing it (n-1) times in the worst case. you should think of propagating from the start only.
•  » » because their maximum element is negative and absolute of all differences is greater than or equals to zero that is greater than negative maximum element.
•  » » Since we need to make the absolute difference greater than max and the absolute difference of negative numbers will always be non-negative therefore the max of the negatives will surely be less than the absolute difference.
•  » » 2 months ago, # ^ | ← Rev. 4 →   Suppose we choose more than 1 positive numbers a, b..., |a — b| < max(a, b), that means we can't choose more 1 positive numbers. Suppose all the numbers we choose is non-positive numbers, |ai — aj| >= max(ai, aj) is always true. So we just firstly choose all the non-positive numbers from the array, then calculate the minimum distance between these chosen numbers, we call it mn. And then check the original array if there is a positive number that is no greater than mn.
 » 2 months ago, # | ← Rev. 2 →   For problem 1528B — Kavi on Pairing Duty, the editorial proves only the part where 1 < x < p < q, but shouldn't it also cover the case 1 < q < x < p? The argument is the same, that since [q,p] is not fully contained within [1,x], it should be of the same length as [1,x], but the editorial doesn't mention it.(Edit) Oh never mind. It says below that this case can similarly be proved.
 » can u please look into my code or if you could provide me a testCase. I think atleast my logic should be correct.
 » Problem B Div-2 — It's easy to prove that a strange subsequence can't contain more than one positive element.Can someone please prove this ?
•  » » Assume that it contains two positive elements — say 2 and 5 . Max is 5. Difference is 3 . Similarly, it is easy to see that difference of two positive numbers will always be less than the greater element of those two numbers. Hence taking more than one positive number is not possible
•  » » For two positive elements, we have the property that their absolute difference will be strictly smaller than the larger element. So, you can clearly see that this violates the strange condition
•  » » lets say we have two positive number a,b and without loss of generality lets a>b , let dif:=a-b. dif is also a positive number(since a>b). but dif=a and dif>=b.
 » 2 months ago, # | ← Rev. 3 →   can anyone find error on my code for Div2 C failing on test case 2. I can't find any error ,i checked it more than 10 times ,any help will be really appreciated
 » In div1 C / div2 E , Can someone explain the part 3 , i.e. "Among the vertices inside it, find the maximum number of vertices such that none of them is the ancestor of the other in Keshi's tree." . How are we doing it .
•  » » 2 months ago, # ^ | ← Rev. 2 →   When you give a dfs order of a tree, there will be no pair satisfies $in[u] < in[v], out[u] < out[v]$and u is a ancestor of v if and only if $in[u] < in[v] \leq out[v] \leq out[u]$. so,if neither of u or v is the ancestor of the other, then $[in[u], out[u]), [in[v], out[v])$ will be disjoint interval. and std::set is enough to maintain it.
•  » » » which also means euler tour generates a set of laminar ranges
•  » » » » Sounds like something important. What does it mean tho'?
•  » » » » » all ranges of tin and tout are either completely disjoint or contained within each other
•  » » » 2 months ago, # ^ | ← Rev. 2 →   So, when you are at a leaf you have a set of intervals with you corresponding to the path from $1$ to the that leaf. And you want to know maximum number of non-overlapping segments? Could you explain how you implemented it using std::set? UPD: Never mind. I got the idea. We can maintain only the disjoint intervals
•  » » » » Do you mean to say that in that set we only keep those intervals which will guarantee the max ans? If you do then won't it cause a problem while erasing a certain node? If not then what do you mean?
•  » » » » » Yes, we are maintaining the intervals that would cause maximum answer... The idea is that whenever you add a new segment in the set, then there are only three cases — (1) The new segment doesn't intersect with any (in this case we simply add it) (2) It comes inside some existing segment (so we remove the existing segment and add this one) (3) If this segment covers some additional segments within it (we don't add it).. So, whenever we add a segment it removes $\textbf{atmost 1}$ segment from the set. We can keep track of which segment removes which one, and any change can be rolled back easily.
•  » » » » » » Oh that's brilliant. Thanks
 » Bye Bye Specialist!! Hello Pupil!!
 » In Div 2 C, I think the condition should be $a_u \geq a_v$ for the first case and similarly $a_u \leq a_v$ for the second case. AliShahali1382
•  » » No, the case $a_u = a_v$ is handled in the third (otherwise) part.
•  » » » No it's not handled in the third part. Because beauty of tree will still depend on $a_v$.
•  » » » » Ah, I see what you mean. I should rephrase that part. It's better to compare the number of vertices $p$ such that $a_p > a_v$ and the number of vertices $q$ such that $a_q < a_v$. Thanks for noticing!
 » Very good round. ! Thanks for this round ! Graph and DP is always fun to solve in a contest. !
 » In Div-1 C [Editorial]: What are efficient ways to compute below?Among the vertices inside it, find the maximum number of vertices such that none of them is the ancestor of the other in Keshi's tree.
 » This one of the best contest I have ever given on codeforces.
•  » » Just because you got a high delta?
 » 2 months ago, # | ← Rev. 3 →   Can anyone explan in 2D,when $x\le n$,why each length must be a divisor of n?
•  » » 2 months ago, # ^ | ← Rev. 3 → Here, $1$ pairs up with $x$ and $x <= n$. Let say pattern repeats $p + 1$ times, the bottom right equation in the image is for total length which is $2n-1$. (I have numbered from $1$ to $2n$). $l(p+1) = n$ tells us that $l$ must be divisor of $n$.
•  » » » thans a lot!
 » 2 months ago, # | ← Rev. 2 →   Did anyone try solving Div2-D by solving the complementary problem??The complementary problem was to find the total number of bad pairings, where a pairing is considered to be bad if, for at least two segments $A$ and $B$ among those, the following condition holds: $1)$ $A$ and $B$ don't lie completely inside each other. $2)$ $A$ and $B$ don't have the same length.I was able to find exactly two segments $A$ and $B$, which will follow the above condition(if I am not wrong in computing that xD), but could not reduce it further. Is it even possible this way or is there any other approach to solve it??
 » I referred the editorial for Div2(C)/Div1(A)... But my solution failed after multiple attempts. Can someone please help me out? My solution 117289608
•  » » You forgot to call dfs.Adding dfs(1 , 0) should fix your code...
•  » » 2 months ago, # ^ | ← Rev. 2 →   Clear the global arrays before every test case as well, apart from calling the dfs function.
•  » » » Yeah ... also his maxn had an extra 0.Fixed code
•  » » » » 2 months ago, # ^ | ← Rev. 2 →   Thank you so much!! Understood!
 » "To be continued ..." in Div. 1 C's editorial?
•  » » At the time of posting, the editorial was incomplete and said "To be continued ...", now it has been fixed.I really don't deserve the downvotes.
 » dpforces
 » Can someone describe the data strcuture used for Div1 C? It just displays "to be continued".
•  » » 2 months ago, # ^ | ← Rev. 4 →   I used a segment tree.tin[u] is the entry time of the vertex u and tout[u] is the exit time of the vertex u during dfs.1) To insert a node: First check if any of its ancestor is already inserted. If yes, remove the ancestor. Now, to insert the node, update the value in segment tree at the index tin[u] as tout[u].2) To delete a node: Update the value in segment tree at the index tin[u] as 0.3) To check if any ancestor is present: If current node is u, any ancestor v will satisfy the property tin[v] < tin[u] && tout[v] > tout[u], so to check if any ancestor is already inserted, the range max query on this segment tree will be max_query(1, tin[u] - 1) > tout[u] if an ancestor in inserted.For reference you can look at 117219173.
•  » » » Thanks
•  » » » Won't there be a situation when it is better to remove the current node instead of the ancestor?
•  » » » » Nope, all the subsets which are satisfied by any ancestor will also be satisfied by this vertex, but not vice-versa.
•  » » » » » Wait, so if the current node is itself an ancestor of some of the previous nodes in the path, then does that mean it is the current node that will be removed?
•  » » » » » » "It's easy to see that the vertices in the data structure always form a path from root to some vertex v in Soroush's tree." so v (the current node) will never be an ancestor of any node currently in the data structure.
•  » » » » » » » No, I meant what happens if the current node (v) is an ancestor of some node in the data structure according to Keshi's tree?
•  » » » » » » » » 2 months ago, # ^ | ← Rev. 3 →   Then we just ignore it and won't insert it.
•  » » » » » » » » » Thanks, I will try to implement it with this thing in mind.
•  » » » A bit of a late reply, I am currently trying to wrap my head around this segtree solution implementation, but I have a question: how does the code actually take into consideration the case when u is an ancestor of a previously visited node v (by "ancestor" I mean an ancestor in Keshi's tree)? What I mean is that, in the DFS function in Soroush's tree, how does the code take into consideration the case when the current node u is an ancestor (in Keshi's tree) of a previously visited node v by the DFS?
 » In div2 D, lets say n=4(8points) And lets say we pair 1 with 4 What are the rest of the pairs? Can anyone list them?
•  » » 2 months ago, # ^ | ← Rev. 2 →   You can't pair 1 with 4 that's invalid. If u draw on paper u can see why. You can pair 1 with 3 and 2 with 4 followed by 5 with 7 and 6 with 8.
•  » » » But when we calculate the divisors of n, we get 1,2 and 4, right?
•  » » » » You don't include n amongst divisors. (I think maybe this wasn't mentioned in editorial)for n = 4 these are the valid equal length pairings. (1,2) (3,4) (5,6) (7,8) for divisor = 1 (1,3) (2,4) (5,7) (6,8) for divisor = 2 which is 3 — 1 = 2.
•  » » » » » 2 months ago, # ^ | ← Rev. 3 →   resolved
•  » » » » » » 2 months ago, # ^ | ← Rev. 2 →   One minute it is correct my bad.. actually the way I have implemented is a bit differentthere is one more pair (1,5) (2,6) (3,7) (4,8)You may want to edit your comment. I am sorry for the mistake.
•  » » » » » » » these are actually those equal length pairings which do not have anything lying inside them
•  » » » » » » » But this is when x>n In the other case, when x<=n, we still cannot take x=n right?
•  » » » » » » » » 2 months ago, # ^ | ← Rev. 4 →   According to the tutorial's method, author discuss the problem for 2 cases x <= n and x > n, and as we can see, in the former, we can only take length at most n — 1,also in the solution, d(n) is proper divisor of n. But from my point of view, talk in x <= n + 1 and x > n + 1 is more natural, from then on, length is <= n now, d(n) is all divisors of n, including itself, the formula has finally become $dp_n = \sum_{i = 1} ^ {n - 1} dp_i + d(n)$. also it is no need to initialize $dp_0 = 1$ (which has no useful purpose) anymore.
•  » » » » » » » » yes we can't take when x = n.the one case which is contributing to total number of divisors is from the x > n case and nothing lying within it.If you see my code I have added + 1 separately in dp[i] = dp[i-1] + 1 + count[even divisors of 2*n] and from the number of even divisors I have subtracted -1;strangely the net sum ends up being dp[i] = dp[i-1] + divisors.even divisors of 2 * n is same as odd divisors of n
 » Weak test case in problem 1529-B. If we take array a as {0,1} answer should be 2.My code is giving output 1 but it still got accepted.
 » I really loved the problems!
 » Sorry to correct one possible little mistakes about the editorial of problem 1528A. In case 3(p=q),to maximize the beauty of the tree, au must be rv or uv.For example, if au=3,lv=0,rv=7, p=q=4, av=4,5,9,5,1,2,0,-1, the original beauty is 21.If change au into rv, the beauty will be 35.(In a word,I mean " The beauty of the tree doesn't depend on av" is not very rigorous.) However,thanks for your editorials again! They have helped me a lot!
•  » » Fixed, thanks for noticing.
 » Can someone tell, how to solve Div2C/Div1A if we need to minimise the sum instead of maximising?
•  » » Just a thought: We can define $dp_{v , j}$ as the minimum beauty of $v$'s subtree if $a_v$ is equal to $j$. This is too slow though, because I don't see any fact like the one in the maximizing version in here :(
•  » » » I think that would work, if we have small ranges, but how to do if we have large ranges as in the problem?
•  » » » » That's why I said it's too slow. Maybe we can prove that $dp_{v , j}$ is convex or maybe not ...But it should be a fun problem if it turns out to be convex :D
 » I am get TLE in python Is it because of default dict? https://codeforces.com/contest/1529/submission/117314236
 » how would we solve the div2 problem A if we were asked to find the max no of operations instead of max no of elements to be deleted?
•  » » 2 months ago, # ^ | ← Rev. 3 →   The algorithm I provided deletes $1$ element at a time, thus does the maximum number of operations. It's worth noting that the answer to both of the problems is the same.
•  » » » got it.Thank you
 » Getting TLE on test case 4 of Div2 C. It follows the editorial explanation. Any help would be appreciated. Here's my submission.
•  » » Add cin.tie(0) , ios::sync_with_stdio(0); to your main function and it should be fine.
•  » » » Thanks a lot. It works now. But why does it not work within the time constraints? Most of the n <= 1e5 cases work fine without fast I/O in O(nlogn) sorting time. Why does this solution, which is supposed to be O(n), fail to run in the required time?
•  » » » » It's not as simple as that, the sum of $n$ over all test cases can be as large as $2 \cdot 10^5$, there can be approximately $2 \cdot 10^5 \cdot (5 + 9 + 9)$ characters in the input which is quite a lot.
•  » » » » » I'm sorry I did not understand, could you elaborate?
•  » » » » » » I said that you might need to read more than 4600000 characters from the input (4.5e6). cin itself is slow so you can't read that many characters in time. So you have to add that magical line to your code to make it work faster.This is because by default, before you call the function cin, the call cout << flush is done. Adding cin.tie(0) unties cin from cout and stops this.
•  » » » » » » » Thanks for the detailed explanation.
 » 2 months ago, # | ← Rev. 2 →   Can anybody please help me what is wrong with this code it is showing TLE test set 4 of question 3 Please help Thank you in advanceHere is my code: https://codeforces.com/contest/1528/submission/117327246
•  » » You forgot to add cin.tie(0) , ios::sync_with_stdio(0); to your main function.
•  » » » Ty bro it works now.... :)
 » 2 months ago, # | ← Rev. 3 →   First time to solve C without fst though I used O(n(logn)^2) algorithm.
 » In problem b's editorial solution won't there be abs() in this statement: flag &= (a[i] — a[i — 1] >= mn); something like this: flag &= (abs(a[i] — a[i — 1]) >= mn); I'm confused, please help. Thanks in advance :)
 » Can someone explain to me for 1528A — Parsa's Humongous Tree why every vertex needs to be either lu or ru. I'm finding it difficult to understand. Thanks
•  » » It doesn't, but we proved that an optimal assignment exists that each $a_v$ is either $l_v$ or $r_v$.
 » what does it mean by ans += a[i] != mn; in the 2nd for loop?
•  » » If $a_i \neq mn$: $ans += 1$
 » Can someone tell me why this code times out? (https://codeforces.com/contest/1529/submission/117339337)The problem is 1528A — Parsa's Humongous Tree. Thanks!
•  » » Please read the comments above ... at least two people have already asked this.You forgot to use ios::sync_with_stdio(0), cin.tie(0);.
•  » » 2 months ago, # ^ | ← Rev. 3 →   1  3  0 0 2 The answer should be 2 as at max you can collect 2 zeroes this is because if we create array as [0 0 2] then abs(a1-a2) = 0 which is not greater than the maximum of array which is 2while your code gives 3
 » In problem Div1C: "Now to check whether any vertex u in the subtree of vertex v exists in S,..."You don't need this check at all to AC. This is because of the condition that $p[i] < i$ for all $i$ in both trees. Assume that $x$ is a parent of $y$ in any tree, then that ensures that $x  » Here's a solution of problem B in linear complexity; 117351012  » In 1528A — Parsa's Humongous Tree, i used a bit different approach in dp which seems quite similar to me with the one provided in editorial . But it is not passing the pretests and i can't figure out what is wrong with my approach. I am new to dp. Can someone help me out?Code using my own approach: my approach Code using editorial's approach: editorial approach  » 2 months ago, # | ← Rev. 3 → In problem C official solution, because of the way the trees are given in the input (each vertex in both trees has higher label than its ancestors), there's actually no need to check the case where$res = -2$. For any pair of vertices$u, v$, if$u$is ancestor of$v$in the first tree$(u < v)$,$v$can't be ancestor of$u$in the second tree as it would imply$u > v$. So, you always insert the current vertex, you just need to check if there's any ancestor of it and if there is, just remove it :)Here's the submission 117369367 with the corresponding assert.  » 2 months ago, # | ← Rev. 2 → Div 2 Problem C (Tree Dp Explanation) Link : https://youtu.be/uR1C56nmNiY  » 2 months ago, # | ← Rev. 4 → In the editorial solution of 1528A, the proof claims we can get a better result by changing av to lv or rv because the contribution from its children increase in this process. But at the same time it can reduce the contribution from its parent right? How do we say that the net result of changing av is an increase in beauty? •  » » In the editorial solution of 1528A, the proof claims we can get a better result by changing av to lv or rv because the contribution from its children increase in this process. No, not$v$'s children but the vertices adjacent to$v$, this includes$v$'s parent as well. •  » » » Got it!! Thanks  » 2 months ago, # | ← Rev. 2 → https://codeforces.com/contest/1529/submission/117382997 This is my solution for problem C. It is showing TLE. Can anyone debug my solution,Pease. •  » » There's nothing wrong with your code. The input is quite large so you have to add cin.tie(0) , ios::sync_with_stdio(0); before reading input to do it faster. •  » » » Thank you so much.  » Well, the editorial of the 1528C - Деревья примирения has some mistakes.In the sixth to last line,$p\geq st_v0$should be changed to$p\geq st_v$."is less than$st_v$" should be changed to "is less than$ft_v$".  » Q2 could've been solved using binary search as well which would reduce the time complexity. https://del.dog/binarysearchsolution.txt here is my solution using binary seach.  » Still could not understand the code for B. Can anyone explain the code solution for B?  » Video Editorial for div-2 Problem D. Editorial link . Give me a feedback in the comment section.  » Here is a stupid solution for Div.1 C:The problem is as such: choose a maximum subset of a path in Soroush's tree such that if$u$and$v$are in that subset neither$u$nor$v$is an ancestor of the other in Keshi's tree.The problem can be solved as such: dfs on Soroush's tree, take node$u$if none of its subtree in Keshi's tree is taken. If$u$has ancestor$p$in Keshi's tree that was already taken, greedily take$u$in place of$v$, this works because a lower node is an ancestor of fewer nodes, in another word: picking$u$instead of$p$offers the chance to pick$v$where$v$is a child of$p$and a sibling of$u$.To solve this we'll need two kinds of queries: 1. on a path from root to$u$find if a node is already taken, 2. in the subtree of$u$find if a node is already taken. Both queries can be done by using a an approach similar to here.Similar to the editorial we store$st$and$ft$for each node, notice that for the range$[0, st_u]$a node exists exactly once if it is on the path from root to$u$and may exist twice otherwise, thus we just need to focus on nodes that occurred a single time. To add a node we update the segment tree at$st_u$and$ft_u$to be$u$, to remove a node we set them to$0$.Since at most one ancestor is taken at a time, we can implement an xor segment tree on the dfs order and query the range$[0, st_u]$; undesirable nodes cancel themselves out, all other nodes will be$0$except for (potentially) one node:$p$which we will then remove and take$u$instead. To solve 2, I just implemented an or segment tree (max would work as well) to find if a node exists in subtree.my solution, not pretty  » 2 months ago, # | ← Rev. 3 → For problem F, how do you go from the sum with binomial coefficients to the sum with Stirling numbers? Also, should the sum be from CNT=1 to n? •  » » 2 months ago, # ^ | ← Rev. 3 → I got it.We can interpret$\sum_{r=1}^n \binom{n}{r} \cdot n^{n-r} \cdot r^k$as the number of functional graphs whose vertex set is$V = A\cup B\cup Z$such that$|A|=n$,$|B|=k$, and$Z=\{z\}$with$f(z)=z$,$f(V)\subset A\cup Z$(no element of$B$has any incoming edges), Let$C=\{a\in A:f(a)=z\}$be the elements in$A$that map to$z$. Then,$|C|=r$and$f(A\setminus C)\subset A$(the elements in$A$that don't map to$z$all map to some element in$A$),$f(B)\subset C$. This interpretation is correct because there are$\binom{n}{r}$ways to choose the subset$C$, then$n^{n-r}$ways to choose how$A\setminus C$maps to$A$, and$r^k$ways to choose how$B$maps to$C$.We can count these functional graphs in another way. First, let$i=|f(B)|$. Choose how to map$B$to$f(B)$by first partitioning$B$into$i$sets. There are$\begin{Bmatrix}k\\i\end{Bmatrix}$ways to partition$B$into$i$sets and$i!$ways to order these sets. There are$\binom{n}{i}$ways to choose a subset of$A$to be$f(B)$. There are$(n+1)^{n-i}$ways to choose how$A\setminus f(B)$maps to$A\cup Z\$.