### vovuh's blog

By vovuh, history, 2 years ago,

1176A - Divide it!

Idea: vovuh

Tutorial
Solution

1176B - Merge it!

Idea: MikeMirzayanov

Tutorial
Solution

1176C - Lose it!

Idea: vovuh

Tutorial
Solution

1176D - Recover it!

Idea: MikeMirzayanov

Tutorial
Solution

1176E - Cover it!

Idea: MikeMirzayanov

Tutorial
Solution

1176F - Destroy it!

Idea: BledDest

Tutorial
Solution

• +46

 » 2 years ago, # |   +1 Problem E was little tricky, learned a lot. Thanks a lot for great contest!!
•  » » 2 years ago, # ^ |   +1 can u explain problem E? I haven't understood the tutorial. thanks in advance!!
•  » » » 2 years ago, # ^ |   +1 Maintain a adjacency list, and then arrange the vertices by number of other vertices it is connected to(v[current_vertex].size) in a multiset or priority_queue (descending order). Pop to the priority_queue and mark the vertex as visited and push it in ans_vector. Iterate all the vertices to which it is connected as also mark them true but dont put these vertices in ans. (Because you need to maintain different colour). Do this until all vertices are visited.Ex: For a tree with 3 vertices and 2 edge: 1-2 and 2-3. Vertex 2 will have max size(2), push it in ans_vector and also mark 1,2 and 3 as true.Now you have an answer vector. If the size if less than equal n/2 print it otherwise print those which not not in ans_vector . This is because the set of vertices which are in ans_vector and which aren't both satisfies the condition of given problem (expect for size one)My submission: https://codeforces.com/contest/1176/submission/55381513 Though i was not able to do it in contest.
•  » » » » 2 years ago, # ^ |   +1 thanks!!
•  » » » » 2 years ago, # ^ |   0 If someone is trying this approach, try this case. You will understand why pure greedy approach won't work.19 81 32 33 44 54 69 74 99 8 Thats why we have to use either the found set or compliment of it.
•  » » » » » 2 years ago, # ^ |   0 Thanks for your testcase. I thought greedy was possible but I was wrong.
•  » » » » » 17 months ago, # ^ |   0 Thanks a lot for this test case. I was thinking greedy will work
•  » » » » » » 15 months ago, # ^ | ← Rev. 2 →   0 What do you exactly mean by greedy here, I just ran a dfs on the graph starting with coloring 1 with 0, its uncolored neighbors with 1 and so on, got ac link , then printing each all colored 0 or1 based on their sizeAm i missing something,
•  » » » » » » » 12 months ago, # ^ |   0 I meant to say that I was thinking greedy will work, but actually it won't work. And by greedy, I mean select the edge with maximum number of edges.
•  » » » » » » » 5 months ago, # ^ |   0 hey, I tried the same method as yours but the time taken by your code is much less than mine. I even modified my code to match yours but still, it's much slower can you help me to figure out why it's much slower than yours
•  » » » » 21 month(s) ago, # ^ |   0 please explain the condition when the ans.size() > n/2 if i understood it correctly you want the answer to be all the other remaining vertices. Why is it so?
•  » » » » » 21 month(s) ago, # ^ |   0 This is because the vertices not present in ans vector will also satisfy the given problem statement.Consider coloring the vector in answer vector by color 1 and rest by color 2. Now reverse the colors. You can better understand by drawing some examples.
•  » » » » » » 21 month(s) ago, # ^ |   0 thanks for help.
•  » » » » » » 21 month(s) ago, # ^ |   0 please tell why i am getting a TLE on test 2, when i am using a priority queue. PS: the solution is not correct but i just want to know why TLE,it should give a WA
•  » » » » » » 21 month(s) ago, # ^ |   0
•  » » » » 14 months ago, # ^ |   0 Can you elaborate this If the size if less than equal n/2 print it otherwise print those which not not in ans_vector . with a test case. Since we are sorting in descending order why there is a need to do so?
•  » » » » » 14 months ago, # ^ |   0
•  » » » » » » 14 months ago, # ^ |   +3 Thanks
•  » » » » 8 months ago, # ^ |   0 For the future readers. Here it's not necessary to select nodes with highest priority based on their cardinality since we don't have to optimize the number of selected nodes. Here is my solution. my java solution
•  » » » 2 years ago, # ^ |   +1 Alternately, you can try the approach given in tutorial.Use BFS. Since BFS transverses the graph layer-wise, you can 'color' the alternate layers with color 0 and 1. Ex:For a tree with 3 vertices and 2 edge: 1-2 and 2-3. Color vertices 1 and 3 with color 0 and 2 with color 1. You would get two answers, the vertices those on even layers and the other on odd. Print the one with minimum size.This is a more cool approach :)
•  » » » » 2 years ago, # ^ |   +1 thanks for the explanation.very clear
 » 2 years ago, # | ← Rev. 2 →   0 Anyone having solution of E using DSU? or give some idea?
•  » » 23 months ago, # ^ |   0 DSU is giving TLE in Python , In fact every approach is giving TLE in python 3
•  » » 20 months ago, # ^ |   0 Check this 68882087 if you're interested.
 » 2 years ago, # |   0 "you can just simulate the process, performing actions from third to first."__why in that order ?
•  » » 2 years ago, # ^ | ← Rev. 2 →   0 Every time you perform operations 2 and 3, you multiply the number with 2 or 4. Therefore, each application of 2 and 3 will make the number divisible by 2, thus forcing you to perform operation 1. If you perform 3 and 2 first until you cannot anymore, then the only possible operation you need to perform is 1.Of course, you can perform the operations in any order you want (eg. 1, 3, 2, 1, 2, etc) as long as you can still perform one.
•  » » » 2 years ago, # ^ |   0 why 3*cnt5 not 4*cnt5 in problem A? please answer.
•  » » » » 2 years ago, # ^ | ← Rev. 3 →   0 for each power of 5 you have to do: apply once operation 3 (/5, * 4) apply twice operation 1 (divide twice by 2 since you multiplied with 4 before) so 3 operations
•  » » » » » 2 years ago, # ^ |   0 thanks
 » 2 years ago, # |   0 How would you do problem C with DP and binary search? Thanks!
 » 2 years ago, # |   +1 anyone, please explain the problem D in Graph (dfs and similar) approach. Thanks
•  » » 21 month(s) ago, # ^ |   0 https://codeforces.com/contest/1176/submission/68009898 I have done it using graphs. If you are further interested in knowing the method ask :)
•  » » » 16 months ago, # ^ |   0 yes i am interested plz tell
 » 2 years ago, # |   0 How to solve problem F with DP ?? Thanks!
 » 2 years ago, # |   0 Won't the solution to the problem D have a worst time complexity of O(n^2) ?
•  » » 2 years ago, # ^ | ← Rev. 2 →   0 55473892I guess my submission is clear it has some comments if you did not get the idea I can explain it a little bit more here
 » 2 years ago, # |   0 Can someone please explain problem F I am not able to understand the editorial solution. Thanks in advance.
 » 2 years ago, # |   0 is the graph is bipartite?
•  » » 2 years ago, # ^ | ← Rev. 3 →   0 Not necessarily
•  » » » 2 years ago, # ^ |   0 could you give an example where it is not bipartite ?
 » 2 years ago, # | ← Rev. 2 →   0 C: https://codeforces.com/contest/1176/submission/55357411 (no dp/binarySearch solution)B: https://codeforces.com/contest/1176/submission/55350519 (with stacks)
 » 2 years ago, # |   0 Hello. I'm interested. Do someone implement some algorithms and data structures from scratch during competition or they have been implemented? I mean sieve, for example, in task D.
•  » » 2 years ago, # ^ |   0 in my opinion,one should be familiar with sieve through practice so that one can easily implement it within a minute during contest.
 » 2 years ago, # | ← Rev. 2 →   0 why 3*cnt5 not 4*cnt5 in problem A? please answer.
•  » » 2 years ago, # ^ |   0 you see. Here's three steps to execute: 1.perform the third operation: n = n * 4 / 5 2.perform the first operation: n = n / 2 3.perform the first operation: n = n / 2 after theses three steps the result you got is the n you type divided by 5. so it's 3 * cnt5.
•  » » » 2 years ago, # ^ |   0 thanks
 » 2 years ago, # | ← Rev. 2 →   0 Can anybody explain me problem F, the problem say that "you choose the cards and the exact order they are played", I think that if we have 3 cards 1, 2 ,3, we have to choose card 1, 2 and 3, not 3, 2 ,1 or 1, 3, 2, ... But I read that the solution has sorted the array and chosen maximum if we can, I think in some case, it will wrong, because we have neglected that we have to choose the cards in order they are played. Sorry about bad english
 » 2 years ago, # |   0 Hey! can someone please tell me what is wrong with my code? http://codeforces.com/contest/1176/submission/55369953
 » 2 years ago, # | ← Rev. 2 →   0 Can someone tell me why this python code is giving WA while its c++ version is giving correct result: PROBLEM A: ~~~~~ Your code here... t = int(input()) while (t > 0): t = t — 1 n = int(input()) count = 0 while (n % 5 == 0): count = count + 1 n = 4 * n / 5 while (n % 3 == 0): count = count + 1 n = 2 * n / 3 while (n % 2 == 0): count = count + 1 n = n / 2 if (n == 1): print(count) else: print("-1") n = 0 ~~~~~
 » 2 years ago, # |   0 Can anyone tell me why it is not accepting for problem E [](https://codeforces.com/contest/1176/submission/55372927)
 » 2 years ago, # | ← Rev. 3 →   0 @MikeMirzayanov Can you please tell me why this approach for problem E fails?Basically I am greedily selecting vertices if none of their neighbours have been selected. At the end ,if the selected set has more than ceil(n/2) vertices then i print the complementary setI feel this should work because one of the sets ie the original or the complementary set should have size less than or equal to ceil(n/2) and no two vertices in the original set have edges between them , so they are connected to some vertex in the complementary set (since the graph is connected) . So the complementary set is also a possible answer.
•  » » 2 years ago, # ^ |   0 I just now realized the mistake in my code. I should have checked for the floor(n/2) instead of ceil of n/2. Thanks anyways
 » 2 years ago, # |   0 why 5 4 3 is not a valid answer for D for below test case-- 3 3 5 2 3 2 4
•  » » 2 years ago, # ^ | ← Rev. 2 →   0 In problem statement, "if a[i] is a prime number, then one integer p[a[i]] is appended to array b" if a[i]=5 and here 5 is prime number so 5th no. prime number is 11 which must appended in array b.But 11 is not in given array b that's why 5 4 3 is not a valid answer.
•  » » » 2 years ago, # ^ |   0 yeah got it thanks
 » 2 years ago, # | ← Rev. 4 →   0 For E,I tried "Graph coloring" with G and R such that neighbouring nodes have different colors,through BFS,now count the number of Green and Red,which ever is less than n/2,print that.
 » 2 years ago, # | ← Rev. 2 →   0 PROBLEM C CAN SOMEBODY TELL ME WHY I M GETTING TIME OUT IN #TC 9.Ii know m algo is not efficient but i m not able to get editorial pls help my solution is 55554895 55554895
•  » » 12 months ago, # ^ |   0 It has been quite long but still Have you figured out the solution?
 » 2 years ago, # |   0 Can someone please explain Problem D in detial.
 » 2 years ago, # |   0 Ok I'm a noob, I absolutely suck, and I can't program for my life. Can someone explain problem B in detail to me -_- sorry about it
•  » » 2 years ago, # ^ |   0 Look, when we divide a number by 3, the only possible remainders are 0, 1, and 2.Now consider an array: (3, 4, 7, 1, 1, 2, 4, 11, 12)  remainders --> (0, 1, 1, 1, 1, 2, 1, 2, 0) For this array, let cnt[0] denote total numbers which give remainder 0 when divided by 3, similarly cnt[1] for remainder 1 and cnt[2] for remainder 2.So, cnt[0] = 2, cnt[1] = 5, cnt[2] = 2Now the idea is to add all the numbers which have remainder 1 & 2, so that when they are added the new element is divisible by 3. Since there are only 2 elements with remainder 2 (4 and 11) in this array, we add these to 2 elements with remainder 1 (say, 4 and 7). New array is: (3, 6, 18, 1, 1, 4, 12)Now, cnt[0] = 4, cnt[1] = 3, cnt[2] = 0After this operation the number of elements that are divisible by 3 in this array (cnt[0]) has increased. For the remaining elements (cnt[1]), we add these together. because 1 + 1 + 1 = 3.Finally the array becomes: (3, 6, 18, 6, 12) which has the max number of elements divisible by 3
•  » » » 12 months ago, # ^ |   0 thnx bro great explaination
•  » » 2 years ago, # ^ |   0 Tell me if u don't get it somewhere I will try to explain more.
•  » » » 2 years ago, # ^ |   0 This makes sense to me now. Thank you so much.
 » 2 years ago, # |   0 (Maybe my English is poor)My idea is similar to the tutorial. This codedoes not exceed O(n), isn't it? But it really timed out. Why is this?
•  » » 2 years ago, # ^ |   0 I mean E.
 » 2 years ago, # |   0 what is wrong in this solution of problem D( Recover it!)?
•  » » 2 years ago, # ^ |   0 hey i have a WA on same test as u had. Can u tell me what was the problem? or what was that tricky test?
•  » » » 12 months ago, # ^ | ← Rev. 2 →   0 EDIT: i figured out np
 » 2 years ago, # |   0 For Problem E, apparently just finding a bipartite coloring of the graph will suffice. That is what I did and got AC. My submission (in java) is here: https://codeforces.com/contest/1176/submission/58155189
 » 21 month(s) ago, # |   0 why my solution is giving a TLE. Help Please https://codeforces.com/contest/1176/submission/67014259
 » 15 months ago, # |   0 Problem E can be easily done by colouring nodes red and blue (like in biparite graph) and then print all the nodes with minimum count of colors (**red** if count of red nodes < count of blue nodes) .
 » 12 months ago, # |   0 Can someone explain problem C? I can't understand the editorial.
•  » » 12 months ago, # ^ |   0 iterate over the arraywe can use the current element e.g. we can use a 23 only if we have an exisiting sequence upto 16.so, when we get a 23, decrement 23's count and increment 16's count.the answers is n — (6*cnt[42]) as these are the only valid subsequenceshttps://codeforces.com/contest/1176/submission/92309951
 » 12 months ago, # | ← Rev. 3 →   0 i found my mistake
 » 12 months ago, # |   0 1176E — Cover itCould anybody explain why I am getting TLE my solution https://codeforces.com/contest/1176/submission/92457008Then there is this solution by a friend of mine and he is getting TLE on case 15. Why? https://codeforces.com/contest/1176/submission/56036460