BledDest's blog

By BledDest, 14 months ago, translation, In English

2112A - Race

Idea: BledDest

Tutorial
Solution (Neon)

2112B - Shrinking Array

Idea: BledDest

Tutorial
Solution (adedalic)

2112C - Coloring Game

Idea: BledDest

Tutorial
Solution (Neon)

2112D - Reachability and Tree

Idea: adedalic

Tutorial
Solution (adedalic)

2112E - Tree Colorings

Idea: BledDest

Tutorial
Solution in M sqrt M (Neon)
Solution in M log M (BledDest)

2112F - Variables and Operations

Idea: BledDest

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +152
  • Vote: I do not like it

| Write comment?
»
14 months ago, hide # |
 
Vote: I like it +23 Vote: I do not like it

Tutorial for F shows the one for A.

Very nice contest, thanks!

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

rating changes?

»
14 months ago, hide # |
Rev. 3  
Vote: I like it +36 Vote: I do not like it

I have another interesting solution for E since I couldn't come up with that dp idea. My solution brute forces and generates all the possible answers for n (vertices count) and doing this for all n <= 24 seems enough and if you can implement it carefully it will pass the time limit. Code.

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

did anyone else not have his rating change, because I did and when I checked it said that it was unrated for me but I am under 2100 and I participated as rated

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    How to check ? I remember I did participate as rated, but I cannot find me in common standings, so strange ...

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

The problems are very nice, especially for B,E and F :)

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

325787868 what was wrong with my logic?

»
14 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

Thank you for a wonderful contest, D was beautiful. I was wondering how to count number of good pairs given a directed tree as in D

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I think this works :

    Given the directed tree, you can count for each node, the number of in-edges (to the node) times the number of out-edges (from the node). Summing it altogether gives the number of paths of length 2.

    And if it's equal to 1, then it works, else it doesn't work.

    Proof : If it's equal to 1, then there cannot exist a path of length >= 3, because for example if you have a->b->c->d, then you have clearly more than 2 paths of length 2 (a->b->c, b->c->d). So there is exactly one path of length >= 2, so it works. And if not, it clearly doesn't work.

    • »
      »
      »
      14 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Thank you for this. I am sorry I was not clear but my query was regarding any type of directed tree. Is there an efficient way of counting number of good pairs.

      • »
        »
        »
        »
        14 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        I have thought about it, and I think this works :

        You do a DP on the tree (you can root it in any way).

        For each node, you look at its subtree and you try to calculate the following three values :

        • $$$end[node]$$$ -> number of paths that end at the node;

        • $$$start[node]$$$ -> number of paths that start at the node;

        • $$$tot[node]$$$ -> the total number of paths;

        Now, you can calculate these values recursively :

        Suppose you need to calculate the three values of a node $$$r$$$, and you already know the values for his $$$k$$$ children.

        Separate these children into two categories :

        • $$$a_1, a_2, \ldots, a_n$$$ such that there is an oriented edge from $$$a_i$$$ to $$$r$$$;
        • $$$b_1, b_2, \ldots, b_m$$$ such that there is an oriented edge from $$$r$$$ to $$$b_i$$$.

        Then,

        $$$\boxed{end[r] = \sum_{i=1}^n (end[a_i] + 1)}$$$
        $$$\boxed{start[r] = \sum_{i=1}^m (start[b_i] + 1)}$$$

        For $$$tot[r]$$$, you should count all the $$$tot[a_i]$$$ and $$$tot[b_i]$$$ and $$$end[r]$$$ and $$$start[r]$$$, and you should count all paths that originate from a subtree of $$$a_i$$$ to a subtree of $$$b_i$$$.

        This gives

        $$$\sum_{i=1}^n \sum_{j=1}^m (end[a_i] + 1)(start[b_j] + 1) = (\sum_{i=1}^n (end[a_i] + 1)) (\sum_{j=1}^m (start[b_j] + 1)) $$$

        Finally you have

        $$$\boxed{tot[r] = \sum_{i=1}^n tot[a_i] + \sum_{j=1}^m tot[b_j] + end[r] + start[r] + (\sum_{i=1}^n (end[a_i] + 1)) (\sum_{j=1}^m (start[b_j] + 1))}$$$

        Hopefully it works, and my complexity is $$$O(n)$$$ !

        • »
          »
          »
          »
          »
          14 months ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          Can you describe what are you trying to calculate exactly? Because if you're counting the number of paths then it's just $$$\sum_{v}{start[v]}$$$ because every path has a starting vertex.

          • »
            »
            »
            »
            »
            »
            14 months ago, hide # ^ |
             
            Vote: I like it 0 Vote: I do not like it

            When I say $$$start[v]$$$, $$$end[v]$$$, $$$tot[v]$$$, I only consider the number of paths in the subtree of $$$v$$$, not in the whole tree !

            • »
              »
              »
              »
              »
              »
              »
              14 months ago, hide # ^ |
               
              Vote: I like it 0 Vote: I do not like it

              Why not to calculate $$$start[v]$$$ as the number of paths in the whole tree starting at $$$v$$$?

              You can just calculate $$$start(v) = \sum\limits_{v \to to}{(1 + start(to))}$$$ recursively without any problems since there are no cycles in the graphs.

              • »
                »
                »
                »
                »
                »
                »
                »
                14 months ago, hide # ^ |
                 
                Vote: I like it 0 Vote: I do not like it

                This is smart; I wasn't smart enough to find this short smart idea ...

    • »
      »
      »
      14 months ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      Can someone help me debug the solution for D problem

      My Approach is to first assign the direction layer by layer using BFS. If one layer has upward direction of edges then the next layer would have downward direction and so on. this would ensure we have exactly (n — 1) pairs reachable.

      Next after this i am finding out the node having degree 2 and has one of the child as leaf node and then reverse its direction to make the 2 length path.

      Submission Link :- https://codeforces.me/contest/2112/submission/326094031

      • »
        »
        »
        »
        14 months ago, hide # ^ |
        Rev. 4  
        Vote: I like it 0 Vote: I do not like it

        your code fails for this kind of cases it says "NO" but its possible, see the image for the edges

        TestCase :- 
        1
        9
        4 1
        4 2
        4 3
        4 5
        6 5
        6 7
        6 8
        6 9  
        

        Screenshot-2025-06-27-022112

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it +4 Vote: I do not like it
    My code
»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Forgot to use long long… lesson learned.

»
14 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

I wonder how to use two pointers to improve the time complexity of $$$C$$$ to $$$O(n$$$$$$2$$$$$$).$$$

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

32595261 Hey can anyone tell why this is failing. I modify the for loop to seperate the logic to check for 2 consecutive elements and 3 consecutive elements and it starts working. I wanted an example testcase for which this code wont work. Thanks!

»
14 months ago, hide # |
 
Vote: I like it +7 Vote: I do not like it

When the rates update

»
14 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Edi not linked on contest page

»
14 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

So when will rating update?

»
14 months ago, hide # |
Rev. 2  
Vote: I like it +2 Vote: I do not like it

Does anyone could say something about rating changes? If it's been a problem or it's in process, etc. We would like to know something please

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone explain me this part better for E?

Note that if a vertex is not green, its entire subtree must be colored the same color: for example, if the vertex is blue and it has a green or yellow descendant, the root is unreachable from that descendant without passing through that blue vertex. At the same time, the green vertices do not impose any additional constraints.

why is this tree illegal? (green)-(blue)-(green)

This is also the third example of the pretest 1, but I don't understand why this coloring has to be excluded

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    just understood this, both green and yellow are reachable without blue means green-green, yellow-yellow and yellow-green

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I have the same issue... Why it is not considering that case??

    • »
      »
      »
      14 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      If we satisfied second condition with (green)-(blue)-(green), then third condition does not works... Because "all yellow and green vertices" should be reachable from each other without blue vertices. it's not (green)-(green) or (green)-(yellow)-(green).

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone explain why we need to add dp[x-2] i.e. dp[m] = dp[m/x] + dp[x-2] in E.

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    dp[x] means number of vertices needed to form x colorings now we are dealing in subtrees so there are 2 more options of coloring them entirely blue or yellow which don't exist in dp[x] so if we find the number of vertices which can be colored in x-2 ways we can do the other two colorings and get x

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

when will rating change

»
14 months ago, hide # |
 
Vote: I like it -11 Vote: I do not like it

Cana anybody tell that why this is not working for problem C

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
    int t;
    cin >> t;
    while (t--)
    {
        ll n,ans=0;
        cin >> n;
        ll a[n];
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        sort(a,a+n);
        for (int i = 0; i < n; i++)
        {
            for (int j = i+1; j < n; j++)
            {
                auto it=lower_bound(a,a+n,a[i]+a[j]);
                if (it==a+n)
                {
                    ans+=(n-1-j);
                }
                else{
                    int index=it-a;
                    index--;
                    if (index>j && a[i]+a[j]+a[index]>a[n-1])
                    {
                        ans+=(index-j);
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

This is my submission id , it is giving error in test case 1 of example 3
https://codeforces.me/contest/2112/submission/326045388

I would be grateful if anyone help me with these
  • »
    »
    14 months ago, hide # ^ |
    Rev. 5  
    Vote: I like it 0 Vote: I do not like it

    I think from what I can tell you have not considered the case when the sum of the three elements chosen by Alice is less than the max element which can be chosen by Bob. For example if you have an input of the form below it will fail because Bob will just color the biggest element blue :(

    1

    5

    1 1 1 2 5

    For the above test case your code says 1 but the actual answer is 0 since Bob can always choose 5 to color blue and win regardless of Alice's choice.

    • »
      »
      »
      14 months ago, hide # ^ |
      Rev. 4  
      Vote: I like it 0 Vote: I do not like it

      Nvm I realized my counter example actually works for your case my bad idk what went wrong.

      Edit: if (index>j && a[i]+a[j]+a[index]>a[n-1]) { ans+=(index-j); }

      This does not work because a[i]+a[j]+a[index]>a[n-1] is not necessarily true for the indexes which are between j and index that you add to the ans variable. For example with the list 27 32 35 37 45 96, When you consider i=0, j=1 you get that a[0]+a[1]+a[4]>a5 and you consider (0,1,2), (0,1,3), (0,1,4) all as valid options while only (0,1,4) index triplet works.

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In D I have a doubt!!!!!, I have No; in test 2 case 1507 (hidden case), the thing is as per editorial, the only sufficient condition for n good pairs is to have a vertex with 2 components, but I think we need a vertex with 2 components and one of those component should be 1 degree (connected to only 1 component). I am checking that and if not found output 1.

Because as per my logic, if lets say I have a component of 2 degrees connected to each other, no matter what dirc I change, I will always create more paths than n. So I don't know why.

Can someone give me a test case where this is true!!!!!

My code for ref, if needed here

»
14 months ago, hide # |
Rev. 2  
Vote: I like it +3 Vote: I do not like it

I am absolutely stunned by problem E and its solution

A very beautiful problem indeed.

Does anyone have more problems that uses similar arguments?

»
14 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

Does dp[m/x] + dp[x-2] imply that no subtree has more than 2 children? I understand why we can use dp[x-2], but doesn’t dp[m/x] represent the minimum vertices in a single tree with that many colorings? Why can’t the optimal tree have many children whose coloring product equals m/x? Does dp[m/x] work in this case? Edit: never mind, I believe it is because dp[x-2] accounts for the last subtree and dp[m/x] accounts for the root + remaining subtrees. Please correct me if I am wrong.

»
14 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Nice problems, thanks!

I want to share a bit different approach on problem E.

  1. Let's say that C(T) is number of ways to color tree T.
  2. Let's say that Cs(n) is set of all C(T) among trees T with exactly n vertices.

Let's find Cs(n) for all n's that can be the answer. It can be calculated using dynamic programming like this:

const int MAXM = 500000; // the maximum value of `m` from the statement

// k is size of the first subtree of root
for (int k = 1; k < n; k++) {
    // iterate over the number of colorings of tree without the first subtree
    for (int x : Cs[n - k]) {
        // iterate over the number of colorings of the first subtree
        for (int y : Cs[k]) {
            // add two colorings: all-yellow and all-blue
            y += 2;
            // we don't need too large numbers of colorings
            if (static_cast<long long>(x) * y <= MAXM) {
                Cs[n].insert(x * y);
            }
        }
    }
}

And then if you print the sizes of Cs[n], then you can notice that it stops growing after n = 25. So you don't need to iterate for more than that. Then you can simply find the smallest n for any number of colorings.

I don't know the complexity of this solution, but it is fast enough to pass the testcases with 546 ms. The submission can be found here: 326206231.

»
14 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

Some observations for problem E:

m must be a an odd number. why?

Let cnt(v) denote the no. of colorings of the root v of a subtree given that it is green.

If it’s children are u1,u2,…,uk then they have cnt(ui) + 2 total colourings. +2 for all blue and all yellow cases.

Since all subtrees are independent, cnt(v) = (cnt(u1)+2)(cnt(u2)+2)…(cnt(uk)+2)

Consider the base case of a leaf node, it has 3 possible colorings(i.e. odd).

and any answer that is formed from these base cases is a product of odd numbers which is also odd. So, m must be odd.

If m is a power of 3 then the tree with smallest number of nodes is the tree with a root attached to n leaves where n is the power of 3, so n+1 in total.

Using a linear chain we can create all odd number of colorings starting from 1. //Edit : please consider the following to be void for now.

For any given m, find it’s prime factorisation and write it such that the product shows all factors distinctly. E.g. 75 = 5*5*3.(not 5^2*3)

Then the optimal tree is the root connected to n linear chains whose no. of possible colorings are equal to the prime factors(we can have any prime no. using a chain). where n is the total no. of prime factors, considering recurring equal factors to be distinct.

You can simply determine the no. of nodes in each children chain and form the total answer. I guess the dp solution achieves this in an easier way.

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Can you please briefly explain how to calculate the number of nodes in each child chain and how it contributes to the final answer?

    • »
      »
      »
      14 months ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      For a chain of length n, it has 2n-1 colourings. If component chains are of length n1,n2,n3...nk Then for the final tree ans is m =(2n1+1)(2n2+1)...(2nk+1). I have written 2ni+1 instead of 2ni-1 because the 2 cases of all blue and all yellow for component chains are added too.

      Please up-vote.

      • »
        »
        »
        »
        14 months ago, hide # ^ |
        Rev. 2  
        Vote: I like it 0 Vote: I do not like it

        So, according to you if m itself is a prime number then the optimal tree will be the root connected to 1 linear chain and the number of total nodes in that tree will be (m / 2). Correct me if i'm wrong.

        If that's what you are saying then i guess you are wrong. Cause for m = 11, the ans is 4 not (m / 2). And the tree will be {(1,2), (2,3), (2,4)} which is not a linear chain in my knowledge.

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Your guess about the tree being a bunch of linear chains connected to the root isn't correct. You can see this with m = 11, 13, 17, or 29. This is because as soon as you have a tree that isnt a chain (like with m=9) you can construct trees by attaching those non-chain trees to a common root. For example with m=29, you use the optimal tree for m=27 but with an extra node above the root

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For problem E, I'm confused on the line of the editorial "if a vertex is not green, its entire subtree must be colored the same color". If I am coloring a tree with only green and blue (i.e., not coloring any node yellow so there are no yellow-green paths), can't I have green nodes in the subtree of a blue node?

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Ah never mind, after seeing the clarification and re-reading the problem statement, I realize that all green nodes must be reachable from each other without passing through blue nodes from the third condition.

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone help me to clear the doubt for tutorial A? Suppose a,x,y=1,2,3 then is there any position for Bob to get the prize always? Even though he chooses 4 as position he will never be able to get the prize at x=2 because a is at 1. The questions asks for a guaranteed position for Bob not that position where he may win conditionally. I may be wrong can anyone help me with this?

Shouldn't the correct condition should be: ~~~~~ int dist = min(abs(a — x), abs(a — y)); int side = (x — a) * (y — a); if (dist > 1 && side > 0) cout << "YES" << endl; else cout << "NO" << endl; ~~~~~ At least a distance of 1 so,that Bob can take that position as Alice and Bob cannot be at same position.

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Bob starts at position 2. "Bob can choose any integer point, except for a (in particular, he can choose to start in point x, point y, or any other point, but not a)."

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem D seemed almost to be a copy of a previous problem https://codeforces.me/problemset/problem/1144/F but anyway Thanks for contest !

»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I suggest to improve the description of the problem F. 'The variable $$$a_{x_i}$$$ gets assigned' or 'The $$$x_i$$$-th variable gets assigned' not 'the variable $$$x_i$$$ gets assigned'. BledDest