chokudai's blog

By chokudai, history, 5 years ago, In English

We will hold AtCoder Beginner Contest 133.

The point values will be 100-200-300-400-500-600.

We are looking forward to your participation!

  • Vote: I like it
  • +36
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Thanks for organizing!

I hope this will be my last (rated) ABC. :)

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +7 Vote: I do not like it

    Good luck ! I hope that you continue to participate in future too and more importantly provide English Tutorial for the contest :D. It helps others a lot !

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Did ur dreams come true?

»
5 years ago, # |
  Vote: I like it +11 Vote: I do not like it

How to solve F?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    I solved it using Persistent Segment Tree.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    We essentially use LCA with square-root decomposition. A more detailed description of my solution is at https://codeforces.me/blog/entry/68231.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    I solve by sqrt-decomposition of path (something like LCA).

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it +10 Vote: I do not like it

    We can also solve it using BIT maintaining the Eulerian order of the tree.
    Let $$$dis(u,v)$$$ be the sum of $$$d$$$ from $$$u$$$ to $$$v$$$ on the original tree. We have

    $$$Q(x,y,u,v) = dis(u,v) + \sum_{e \in path(u,v),color(e) = x} cst(e) + \sum_{e \in path(u,v),color(e) = x} 1$$$

    Thus, we can solve queries with the same $x$ together. Use two BITs to maintain $$$\sum_{e} cst(e)$$$ and $$$\sum_e 1$$$. Modify it before and clear it after printing answers to them.
    It works in $$$nlogn$$$ time.

    Actually, my solution is the fastest so far:D

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think it can be solved in $$$O(n\ log(n))$$$.

    like this My Submission

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    I had an online solution with LCA, Eulerian tour and prefix sums. The idea seems to be similar to those of 1.618.

    Let's write down an Eulerian tour over edges (counting them with "+" when going down and with "-" when going up). We store prefix sums for all the edges ("large" array), and for edges of each specific color ("small" arrays). When answering the query, we split the path into two parts and consider only paths from LCA to each vertex. To find an answer, we need total path length and the contribution of the specific color. But it's just a sum on a segment (on "large" array or on one of the "small" arrays). Also, to find the bounds of the segment on a "small array", we can store which edges appear in this array and then perform a binary search.

    The code is here.

    This solution is $$$O(N\log N)$$$. It's neither the slowest, nor the fastest :)

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve E?

I solved F with sqrt-decomposition but didn't find any ideas in E

  • »
    »
    5 years ago, # ^ |
    Rev. 5   Vote: I like it +8 Vote: I do not like it

    For E you could use multiplication principle. Start with the root. We have k choices here. Now the first child will have k — 1 choices, second will have k — 2 and so on. But while calling dfs for children keep track of how many siblings of the node you traversed. Because for node x all its siblings wont have an effect on children of x. Just work it out you will get it. One more thing for depths greater than 3 the dependency on the node higher than current nodes by 2 will not be there.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Root the tree arbitarily. If you are at the root. You can assign the color for the root in K ways and its children in (K-1)P(degree[root]) ways. For the other vertices you can assign the colors for its children in (K-2)P(degree[vertex]-1) ways.

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

How to solve Problem D??

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it +14 Vote: I do not like it

    It was a system of linear equation. We had equations in the format. x1+x2=a1 x2 +x3=a2 . . . xn + x1=an But if you add and subtract first n-1 terms alternately. You will always get x1-xn = alternate sum of n-1 terms. Using this and last equation we can get x1 and now x2 and x3 and so on till xn can be ascertained

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    Let's denote the water in dams from 1 to $$$n$$$ as $$$x_1,x_2...x_n$$$ and the water on each mountain as $$$y_1,y_2...y_n$$$.Then the equations read as $$$x_1=(y_1+y_2)/2$$$, $$$x_2=(y_2+y_3)/2$$$, . . . $$$x_n=(y_n+y_1)/2$$$ Now see that $$$x_1-x_2+x_3-x_4+...x_n=y_1$$$ From $$$y_1$$$ we can successively find $$$y_2,y_3$$$ so on by substituting in the original equations.

»
5 years ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

Two things.

  1. Since in the last few ABCs some people have volunteered for editorials it would be nice if you can add the link of such editorials in the announcement.

  2. Was E a case of coincidence?

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I got C wrong, Test case 16, stride_zero_01. I dont get it. Can somebody tell me what is wrong? Thanks

result

  • »
    »
    5 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it
    2 2020
    

    Your answer is 2, but the correct answer should be 0.

    I guess that you can't just swap l and r after l %= 2019 and r %= 2019.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    Swapping l and r if l > r is incorrect. 6 2024 is a countercase: your solution prints 30; the correct answer is 0.

    Adding 2019 to r in this case should fix the error.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    Modulos is circular. After taking modulo of l and r with 2019, if r > l then also answer can be zero. For ex. lets take l = 2 and r = 29 and taking mod with 5 , then after taking modulo with 5 , r = 4 and l = 2 , but here answer will be 0 as r became 4 after 3 or 4 round of modulo circle.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    Your code gives 2 as output for l=1 and r=2023, ans should be 0(i=1,j=2019) The problem is

    Spoiler
  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have a simple solution. If the distance between l and r is greater than 2019 then it will certainly contain a factor of 2019 and hence the answer will be zero. Otherwise we can calculate by brute force.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve Problem F??

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can some explain F in simple words i know about sqrt decomposition but was unable to come up with the solution

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone explain the logic behind E.

  • »
    »
    5 years ago, # ^ |
    Rev. 5   Vote: I like it +5 Vote: I do not like it

    I will try to phase it so that everyone can understand. We will use multiplication principle of counting. Step number one consider any node(preferably 1) as root. We can color it in k ways. Now if I consider its children they cannot be colored in the same color as its parent(as a matter of fact no child can be colored with the same color as its parent). Now for any node, if it has sibling(same parent), it will have a distance of 2 from its sibling. So if a parent can be colored in $$$m$$$ ways, its first child can be colored in at least $$$m - 1$$$ ways(I will come to why more), second child at least $$$(m - 2)$$$, third $$$(m - 3)$$$ ways and so on. If you consider a set of nodes with same parent, the nodes among themselves cannot be colored with same color because they have a distance of 2 within them but their children, will have distance 3 from the other sibling hence, will be independent. To understand, this point consider following connections:

    1 2

    1 3

    2 4

    Here 2 and 3 children of 1 cannot have same color but 3 can have same color as 4 which is 2's child. So how to consider this in multiplication principle? Simple when you call dfs on unvisited children keep track of how many new nodes you found, if you found $$$c$$$ new nodes, then if you call the dfs on the $$$(c + 1)^{th}$$$ new node, you send that information onto it as a function parameter in a variable $$$siblingExplored$$$, so that when you call the child of the node it can get the correct multiplication factor. Let's run our example on an example. n = 5, k = 4 edges: 1 2

    1 3

    3 4

    4 5

    1 can be colored in 4 ways.

    2 can be colored in remaining 3 ways.

    3 can be colored in 2 ways(different from 1 and 2)

    now when we explored 3 we said can be colored in 2 ways, that doesn't mean 4 will be colored in only 1 way, but since we explored 1 child node of parent node 1, we went onto 3, we noted that 2 affects one but we sent that information(of having explored 1 sibling before) onto 3, so the multiplication factor of 4 remained 2 because 2 no longer affects it. And for nodes with height greater than 2 we will not reduce the multiplication factor by 1. Why because even though the number of possible colors are decreasing by 1 but at the same time, the dependecy on parent 3 levels up is also vanishing.

    code
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can anyone solve problem B without brute force or use any nice property. Thanks...

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Official editorials are available here