Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

# | User | Rating |
---|---|---|

1 | Benq | 3813 |

2 | tourist | 3768 |

3 | maroonrk | 3570 |

4 | Radewoosh | 3535 |

5 | fantasy | 3526 |

6 | jiangly | 3523 |

7 | Um_nik | 3522 |

8 | orzdevinwang | 3441 |

9 | cnnfls_csy | 3427 |

10 | zh0ukangyang | 3423 |

# | User | Contrib. |
---|---|---|

1 | awoo | 180 |

2 | -is-this-fft- | 179 |

3 | nor | 169 |

4 | Um_nik | 167 |

5 | SecondThread | 164 |

6 | maroonrk | 162 |

6 | adamant | 162 |

8 | kostka | 161 |

9 | YouKn0wWho | 158 |

10 | errorgorn | 155 |

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial of Codeforces Round #746 (Div. 2)

↑

↓

Codeforces (c) Copyright 2010-2023 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jan/27/2023 13:39:04 (g1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Wow, that was quick.

The contest was awesome, Even system testing was fast :DIn problem D, you can do binary search directly on the bfs order (search for the shortest prefix having value same as the value of whole array) which will save 1 query I think.

Also, Dfs order of edges without Euler tour traversal will work in 11 queries, But we didn't want to make the problem harder so we let ETT solutions pass.

I think D can be solved in around $$$1 + 1.25 * log(N)$$$ querys for a general graph, by doing ternary search over the sets of nodes, even if edges from the graph are hidden. Sadly that was 1 extra query and i couln't solved it that way. Anyways, awesome problemset!

_runtimeTerror_ Bakry I do understand why ETT works, but could you please explain why your approaches work?

UPD:Already realized what's happening when using bfs/dfs. Just accepted it, 130744806 is my solution using bfs. Thanks!yes i did this solution i think that the euler tour solution is much harder to realize though

found it (Euler tour traversal)

I used a centroid decomposition solution, which passed in 11 queries for all system tests. However, there is a construction that makes it use $$$1 + log_{\sqrt{3}}n \approx 13$$$ queries. The construction is basically the pattern shown below, recursively expanded.

130697675

Could you elaborate on it being log in base sqrt(3)? I also did centroid decomposition

Basically, this construction makes it so that the size of the tree only decreases by a factor of 3 every 2 queries.

In the tree above, 1 is a set of 334 nodes, 2 is a set of 333 nodes, and 3 is a set of 332 nodes. The solution edge is somewhere in set #3.

In the first query, the algorithm would query all of the nodes in set #1 and determine that the solution edge is either in set #2 or set #3.

In the second query, the algorithm would query set #2 and determine that the solution edge is somewhere in set #3.

We can then recursively construct the same tree structure in set #3.

As you can see, we have used up 2 queries, but only decreased the size of the tree by a factor of 3. This means the algorithm will use $$$1 + log_{\sqrt{3}}n$$$ queries, which is just barely over the limit.

I was able to hack your submission using a very similar test case.

Thanks a lot!

how are you always sure to reduce the tree by a factor of 3 at least?

I was also doing sort of centroid decomposition. If you have the time, you can try to hack my submission too. It passed all the hacks so far.

I just hacked your submission. I'm pretty sure that any solution involving centroid decomposition can be hacked. Thanks for letting me know and making the test set stronger.

Thanks, I agree.

Thanks for the quick editorial

It was a miracle. compared to current speed of posting of editorials.

B is Great!!!

Loved problem E.

Can you please explain how to do E in detail?

I can't understand so as to how to find the lower boundary ?

How will you construct a mask for this and check for odd and even cases?

Let ith bit is most important bit, that means, for all bits greater than ith bits, xor = AND = 0. So what you want is, when we are assuming ith bit is most important bit, can we somehow have only bits greater than equal to i, in each number of array and remove their lower bits. This can be achieved by taking bitwise AND of all numbers with a number which has bits>=i as set and bits <i as unset. All you need now is, in this modified array max length subarray with xor = 0.

130719800

Thanks for the code

What was the intuition to B? like I got to know that what will work but what is the line of thinking (approach) to solve this problem ?

My approach: We cannot move an element if

`i-x < 0 and i+x > n-1`

(`i`

is the index of the element), so its index must same as in the sorted array. We can swap the rest of the elements as we want.My Solution

can you tell me what should be the output for** ~~~~~ n= 5 x=3 arr[]= 5 4 3 2 1** ~~~~~

and how?

YES

Explanation`5 4 3 2 1`

`->`

`2 4 3 5 1`

`->`

`2 1 3 5 4`

`->`

`4 1 3 5 2`

`->`

`4 2 3 5 1`

`->`

`5 2 3 4 1`

`->`

`1 2 3 4 5`

There may be a shorter explanation.

You could just sort the edges and do binary search on D

fucking great idea,sad to realize this after contest...

What is the reason for sorting?

none really. I just tried a few test cases and it seemed to work

You could do it on the bfs order.

sorting mustn`t work in this task... guess tests are week maybe

cute pfp

nice contest. fast systest and editorial :D

Thanks for the fast editorial. For problem C, may I ask that how do we find the two required edges to break the tree into 3 parts with xor sum of each component equal to x in O(n) or O(n lg n) time? I have been trying to solve this problem but I am stuck at the implementation.

I saw the update in the editorial. Thanks for explaining it!

I Think, in problem A, ans should be "H / (x + y)" multiple by 2

we've corrected it in the editorial

Thank you for a great round and fast editorial!

Thanks for fast editorial!

Are there any provements for C that the deepest subtree erase is always right?

Problem D could've been solved relatively naively due to the limit being $$$n < 10^3$$$, ie. without any consideration in which order to process edges. Simply maintain the set $$$E$$$ of suspected edges and traverse graph in any way you like (in my case it was DFS) until you enumerate $$$|E| / 2$$$ suspected edges (for the binary search part; edge set will be denoted as $$$E'$$$). Collect all the visited nodes into a single query, and if this query gives the same result as the initial query for the whole graph, set $$$E = E'$$$. Otherwise, $$$E = E \setminus E'$$$.

Of course, each time we print a large portion of the tree just because we enumerate zounds of "unsuspected" edges. What I mean to say is that there was no need for consideration of Euler tours nor to care about efficient implementation.

Btw, this makes IO load equal to $$$O (n log n)$$$ rather than amortized $$$O(n)$$$ as in editorial.

Also, I liked this problem a lot :)

Could you please explain why is it O(N log N)?

Most cases you will enumerate lots of edges that are not in set $$$E$$$, because we queried about them already. In other words, almost every query will be about a set of O(n) vertices. There are O(log n) queries, thus O(n log n) vertices printed in all the queries.

For example, imagine a path of length 512. First you enumerate ~256 vertices. If the sought-after edge is not there, in the next query, if you start dfs from the same source, you need to query 256 + 128 = 384 vertices rather than just 128 (because first 256 vertices enumerate only edges not in $$$E$$$, and you need to print a whole connected component). Etc.

Though I checked my submission 130706195 and it seems that during the round I mitigated this problem, because I stored the visited component as a set of suspected edges from this component (the $$$qry$$$ variable). And instead of querying vertices from the whole component, I print only those adjacent to at least one $$$qry$$$ edge. It seems that this trick makes all queries to contain at most $$$n + 2n - 2$$$ vertices in total = $$$O(n)$$$.

Anyway, $$$O(n log n)$$$ should pass as well (ie. if you stored vertices from the component rather than edges).

In problem A ans=(h/x+y)*2

we've corrected it in the editorial

Could anyone please explain me that why does this submission 130717510 to D fail because I guess that this approach was fully correct. Moreover can anyone explain me why does this submission pass 130725042. I cannot find any valid reason for this and am doubting this to be a case of bad test cases

Your AC solution fails on

5

1 2 4

2 3 1

1 4 1

1 5 1

That's exactly what I was saying weak tests

Can anybody disprove the following?

We can split into 3 components if we can find a subtree (not the tree itself) that has $$$xor$$$ equal to the $$$xor$$$ of the whole tree.

I am getting wrong on the test 10, with this idea. Solution.

Assuming that other nodes count is > 1, you guarantee that you can split them into 2 components with equal xor value, but this value is not necessarily = the whole tree xor.

1

3 69

2 2 3

1 2

1 3

Ooh, yes. Thanks.

Now this is a great round. A-C were creative and easy to code, D required some implementation, E was just beautiful and no "fancy" algorithms appeared until F2, the hardest problem.

A perfect example of how div2 rounds should be prepared.

rel-a-table

Systest for C seems to be weak. I wrote something nonsensical (count all edges that can split the graph into zero and the target xor value). But it can be uphacked with this simple test case:

For problem D maybe my solution can be hacked because my basic idea was finding maximum edge at first by 1 query. After that split the tree to 2 trees so that new smaller trees have 1 common node and their union is current tree. Also while choosing this 2 trees we are minimizing their size difference. Now after splitting we can easily ask one query about them and determine which one includes max edge and continue to process until our tree has only 2 nodes. This works quite well but I can't create worst case scenario If you know how to do it please share your ideas. my code

My friend hacked my solution post-contest with randomly generated trees.

My solution selects half of the remaining nodes by starting from a random node and dfs, and repeat if necessary.

Hacked with a tree, where each node has $$$9$$$ children. $$$n=820$$$, it has $$$3$$$ layers, and in the worst case it is $$$4$$$ question to handle each of them. $$$3*4+1>12$$$. I think a similar construction works, where each node has $$$5$$$ children.

How can

Ebe solved in $$$O(NlogN)$$$? According to editorial, it looks like the time complexity should be $$$O(N^2 logN)$$$I only know a solution in Nlog(max A), I think the editorial meant that it's solved by iterating k giving total complexity N(logN ?)logmaxA. One observation you can do is that you can split the array into intervals [x, y], where for all positions k is set, and then find tho positions which are furthest apart with the same xor (in the interval [x-1, y]), but with the same parity of indices.

In problem E, shouldn't be $$$r-l+1$$$ is even, instead of odd?

Edited. Thanks!

As always, here are the video solutions to the first three problems : solutions

It's not solution but I want to share idea about 1592D - Hemose in ICPC ?. You should know that tree is bipartite graph. Let's say one partition is array

a, and other partition is arrayb. Request answer for 1,2,...,n. Let's say it'sr. Now, repeat following:abigger thanb, just swap them.b(array of vertices) into two halvescandd, or closest size. Because in bipartite graph each edge goes from one partition to other, we get that edges connectingaandcare different to edges connectingaandd, and together they make whole set of edges betweenaandb.a+c.r, then say new partitions areaandd, otherwise new partitions areaandc.Do this until we get 1 vertex in both partitions. I didn't proof how many requests it does in worst case, but I guess it's around 17 (case 500 size of both partitions). So it's a little bit more than we can afford. Sad. But very easy to implement. 130691922

For some reason, if I remove isolated vertices each time before the check for swap, then it get wrong answer, but it should be

request limit exceedif idea above is correct. Something is wrong. 130827040I found bug. Proof above is wrong.

Why?Everything right what is stated above, the hole is in understanding of what you asking by query. If you read carefully statement, you're asking maximum Dist(u,v) but it doesn't forbid to walk through vertices not in set. I was thinking about paths only over those edges which connect vertices we choose but it's wrong. Simplest counter case of proof is graph:

First partition is 1 3 5, second partition is 2 4. It asks about vertices 1 2 4, but there is path from 2 to 4 via 3 which has two edges of weight 10, and thus it thinks that we need only 1 2 and 4. but 4 is isolated now, so it answers 1 2 which is wrong.

The contest was fun, but i think the editorial is kinda bad. For example, on E, what is "which can be solved easily in O(NlogN)" supposed to mean? If you're not gonna explain the solution at least put code so people can learn what to do.

me: thinks I can pass 1000 this round

codeforces: nope, here's your 999

Can someone break my idea for D? Im finding the centroid of the tree and doing a knapsack to find the value closest to n/2 you can get by summing subtrees of the children of the centroid and do a query with those subtrees + the centroid. If the value you get by doing it is less than the answer, you remove all those children from the tree (i.e mark them) and repeat that algorithm until i have 2 nodes.

Code: 130722106

why we multiple by 2 in problem a?

Because we're using two weapons. Think of the case where h = 10 and the two weapons are 5 and 4

bro i don't understand all equations for problem a Can you explain them?

Assuming you understood the first part of the editorial:

The ammount of times we can be 100% confident we can use both weapons will be h/(x+y) (the ammount of times we can fit both of them in h).

That would cost (h/(x+y))*2 operations and leave h-(h/(x+y))*(x+y) hp remaining (each time we take x+y hp off, and we will do it h/(x+y) times). We can notice that this is the remainder on the division of h by (x+y), or h mod(x+y) or in c++ h%(x+y).

Since the hp remaning after that will be smaller than x+y, you will either use x or x and y if it is bigger than x.

If you still cant get it, do some cases by hand and/or read the editorial and this comment again

Hemose Bakry there is unproved part for C. "If you can partition the tree into m components".. how do I prove this?

Can you please help in proving if these (below) are the components with every one of them with xor=x, we can have another set of 3 legal components?

you cant partition into those components in the first place because you cant get that by removing edges

In problem C, Can anyone explain how do we search for the 2 edges in the tree?

Video explaining the idea

Basically once we fix the first edge, I explain how all edges fall under 2 categories:

1)An ancestor to its parent and

2)A vertex from a disjoint subtree of an ancestor to its child Then I explain how we can check for these 2 cases in the DFS.

Or you can also just count number of edges (say

`cnt`

) s.t the subtree corresponding to these edges have xor =`0`

or`xr`

(=xor of whole tree)`ans = (cnt >=2 ? "YES" : "NO")`

My submission: 130710063

F1 and F2 are one of the best F problems I have seen before: It's really difficult to come up with the correct ideas, but the solutions are quite short.

In the editorial of F2, perhaps "if" is spelled wrong in the last paragraph:(

"if and only if" is sometimes shortened to "iff". Link.

Oh, I have known that know. Sorry for my poor English:(

Can someone give me a solution to D problem in simpler words, I just can't understand it from the editorial.

check this URL to understand what is euler tour https://www.geeksforgeeks.org/euler-tour-tree, and we generate an euler tour array from the input tree, then use binary search to find the edge with maximum dist

Why does binary search in the Euler Tour yields the right answer?

Let's define Dist(u,v) as the greatest common divisor of the weights of all edges on the path from node u to node v.means the maximum dist in this tree is simply a maximum weight edge from u to v, because a >= gcd(a,b) for all positive integers

I understand that. What I don't get is: how do you know that when you choose the midpoint in the binary search, you're not "breaking an edge"? In other words, how do you know that all the edges you have to consider will be either in the left or the right part of subarray you're considering?

PS: I can see why that won't happen in the first 2 splits, but I can't see why it'll never happen.

there is no way to break an edge since we only do binary search on nodes, in one search we decide to search the node's left or node's right (here left/right means left/right in euler tour array)

1 2 3 2 4 2 1 5 6 7 6 5 8 5 1

Assume the answer is edge 5-6. Let's say that the first binsearch split gives you:

6 7 6 5 8 5 1

Now, the second split will query either

"6 7 6" or "5 8 5 1"

and neither has the answer (5-6).

in the second split we will split the arr into (6,7,6,5) and (5,8,5,1), then ask whether the left part has the maximum.

here's my code

Why does the second split will split into (6,7,6,5) and (5,8,5,1) rather than (6,7,6) and (5,8,5,1)?

Thanks for the explanation got it.

Nice Contest and finally I have become a PUPIL.

I was able to solve both A and B within 45 mins for the first time and got stuck at C without knowing trees .

Can anyone explain E please?

nice bro i was stuck on B for almost the whole contest lol had to solve C and D and I found them much easier than B :(

In D can't we just take half the edges every time?

We can't do this in simple way. Because edges are defined by vertices we ask. So if we want to check edges (1,2) and (3,4) just two edges, then answer to our question will also may have edge (2,3) if (2,3) is connected in initial graph.

r57shell, can you explain why the binary search on the Euler Tour will never "leave edges behind"? Like, edges whose one endpoint is to the left of the midpoint and the other endpoint is to the right. I understand that that won't happen on the first or second split, but it's not clear why that won't happen as the binary search progresses.

Explanation turned out to be longIt's a bit blurry what exactly meant in editorial to binary search on the Euler Tour. I'll define Euler Tour as path from the start to the end visiting every edge, some of vertices would be there multiple times. Then, range of binary search will represent segment of this path! To get this segment we can cut part from the left, and cut part from the right. We need to show single fact, that there is no pair of vertices from this segment of path which is also connected by edge that is not in segment of path we have. You could wave hands, and give some intuition why it's true, but shorter would be proof by contradiction: suppose there is (u, v) pair which is not in our segment of path but it is connected. Then, think of any segment of Euler Path which has vertex u and vertex v in it, and you should get it. Without loss of generality u is parent of v, then either we start from u and visit some of children of u, and then at some point go back to u and walk across (u,v). Either we start from v, visit some children of v and go back to u over (v,u). Contradiction with fact (u,v) or (v,u) not in the segment.

But when you go through (u, v) the first time, you add "u v" to the Euler Tour. The second time you go through (u, v), you add "v u". Assuming the answer is (u, v), isn't it possible that one of the binary search splits will put "u v" in separate segments ("u | v"), pick the segment that contains "v u" (since that is the answer) and later on another binary search split will put "v u" in separate segments ("v | u") and now the answer won't be in either segment?

Maybe you tried to answer that, but I still can't see why that wouldn't happen...

Ah, you should not do that. You should cut segment by half of its edges, it's not the same as to cut array of vertices into two halves. You either take edge u->v or not take. If in first choice is M edges, then in other is N-M edges where N is total edges (not vertices).

Lets say we did simpe dfs and got edges as {1,2} {2,3} {1,4} {4,5} {1,6} {6,7}

Now lets query for first three edges

query({1,2}{2,3}{1,4}) === query(1,2,3,4)

To ask a query about a set of k nodes v1,v2,…,vk (2≤k≤n2≤k≤n, 1≤vi≤n1≤vi≤n, all vi are distinct)we get the max gcd and max gcd edge was actually there in our query edge set. But what if we query last three edges

Query ({4,5} {1,6} {6,7}) === query (4,5,1,7)

Note that result of this query we will get, would still be max gcd, coz 1 and 4 is present in the query and so the edge {1,4} . However, our edge set for this query didn’t have this edge.

So, we need to use edge set formed using Euler tour.

For F2, I don't think we need to search for all the values of k in: 0<=k<=K, because using the second operation once saves us one coin (with type one operation we will use 3 coins and with type 2 we will use 2 coins). The only corner case is by using the second operation we might change a[n][m] from 0 to 1, but even in that case, it can be rectified with just one additional coin which was our profit from the last type 2 operation we made.

Thus using the maximum number of type 2 operations always works :)

Thanks for an amazing contest and a really amazing set of problems. Liked the E problem especially.

I hope you would do this change in F2's tutorial so that it becomes slightly easier for the viewers to understand and code :)

finally a specialist =)

in problem E how do you check the condition that pref[r] ^ pref[l-1]==0 fast?

maintain lastSeen[pref[x]] in an array

The Observations in the Div2 C question is same as this question — https://codeforces.com/problemset/problem/1516/B

My explanation for

Problem E. Commented source code, see 130804026.Let's look at $$$2, 3, 5, 3, 7, 5, 1, 4$$$. I will write the numbers in binary in columns:

What the task now translates to is: Find the longest row of continuous $$$1$$$s with the following property: The length of the sequence is even and each horizontal segment above your sequence contains an even number of $$$1$$$s. Examples are:

Only the first and the last example are valid. The last one is the longest valid example. How do we find this longest sequence? We will need to iterate over each bit (row) and each value (column). Let's assume we iterate over row $$$k$$$

. What we now want is a prefix-xor-sum for all values above our row $$$k$$$ (those are calledimportantin the editorial).We also color the values in the xor-sum alternating in 2 colors. We will need this to achieve the even length of the sequence. Now we iterate row $$$k$$$ with $$$r$$$ from left to right and for each $$$r$$$ we keep the value $$$l$$$ which denotes the last position with a $$$0$$$ (We handle position 0 as also having value $$$0$$$, so $$$l$$$ starts with 0):

We now want to to find the smallest $$$l_{ans}$$$ such that $$$l \leq l_{ans} \leq r$$$ and that the xor-sum of all values in $$$(l_{ans},r]$$$ is equal to $$$0$$$. This is equivalent to finding $$$l_{ans}$$$ such that the xor-sums of $$$(0, l_{ans}]$$$ and $$$(0, r]$$$ are equal. To achieve the parity in the length of the segment we also need, that $$$r$$$ and $$$l_{ans}$$$ have the same color:

How do we find this $$$l_{ans}$$$? We could create a map that saves for each possible xor-sum value and both colors all the positions this xor-sum appears at and then do a binary search. This would get as a $$$\log(N)$$$ for the binary search and a $$$\log(a_i)$$$ for the map and this will TLE 130742189. We can replace the map with just a vector with $$$2^{20}$$$ values. This will get accepted, but just barely 130742292! Another improvement is, we do not need the binary search. While iterating $$$r$$$ we can keep for each possible xor-sum the earliest appearance in our $$$1$$$ s-segment. This way we get rid of the second $$$\log(N)$$$-factor and this solution gets accepted easily 130804026. The final complexity is $$$O(N \log a_i)$$$, for iterating both $$$k$$$ and $$$r$$$.

Why in C we do we need to go to the deepest subtree? Why can't we take any subtree whose xor is x?

data1

5 5

5 5 4 4 4

1 2

2 3

3 4

4 5

xor value: [4 1 4 0 4]

XOR value of subtree 3 is 4.

But if you delete the edge (2,3) first, it is impossible to partition the tree into three components with equal XOR values.

C is really nice, the m-2 idea is sneaky

I'm trying to solve problem D in the following way:

After getting max value by querying N nodes, query for N/2 nodes.

If the ans is less than max, then remove all the edges which are made up these nodes at both the ends.

If the ans is max, then keep all the edges which are made up these nodes at both the ends, remove other edges.

With the help of remaining edges, again figure out N/4 nodes, and query it. Keep going this way, and in the end, only one edge will remain.

Any issue with this approach?

The solution is failing : https://codeforces.com/contest/1592/submission/130812435 . Is there a logical bug or an implementation bug?

In this way, you cannot guarantee every time you make a query, you really half the size of the set of possible edges that could becomes the answer. I believe that some of the pretests block this approach but I'm not sure .

In the F1 problem , the editorial makes a new grid using parity of sum of (i,j) , (i+1,j) , (i,j+1) and (i+1,j+1) and solves for this one...is this a common technique?

To get a feeling, you can look at it the other way first. If you want to flip $$$(x,y)$$$ using rectangles containing $$$(1,1)$$$ without flipping other cells, how do you achieve this? You achieve this, by doing operations on $$$(x-1,y-1)$$$, $$$(x-1,y)$$$, $$$(x,y-1)$$$ and $$$(x,y)$$$. If you got several bits you want to flip, then you can add those operations. You notice, doing this operation twice on a cell will keep everything the same. So we can handle the operations as "activate" and "deactivate" the operation on some $$$(a,b)$$$. The editorial now turns this around, by transforming each 4-operations flip into a single cell. This is easily possible, because we found a sequence of operations, that flip

exactly one cell.I guess you can find similarities to [Lights Out](https://en.wikipedia.org/wiki/Lights_Out_(game)) (I'm really sorry, I somehow can't link this. Guess because there are brackets in the link maybe...?) although the transformation is not so easy, because there is no simple sequence to swap exactly one cell in this case.

You could also try and learn more about linear algebra to get a better feeling for this.

So yes, I'd say it is a useful technique to group some operations with special properties and then transform the problem into that group-view.

Someone please help me find the mistake in my code for problem 2c....it is giving TLE on 3rd test case (https://codeforces.com/contest/1592/submission/131055533)

His all submissions in this contest are shrinked deliberately. I know it is rule violation. 130676197

Can anybody please share the implementation for problem c and possibly explain how exactly are we going to do what has been mentioned as the last step in the editorial?

problem A why we get 2 * (H / x + y) huh ?

Can you post the solution to issue 1652A in c++?