Блог пользователя Danylo99

Автор Danylo99, история, 4 года назад, По-английски

We invite you to participate in CodeChef’s August Long Challenge, this Friday, 7th August, from 15:00 IST onwards.

The contest will be open for 10 days i.e. until 17th August.

Also, if you have some original and engaging problem ideas, and you’re interested in them being used in CodeChef's contests, you can share them here. Joining me on the problem setting panel are:

Prizes:

Top 20 performers in the Indian category and top 10 performers in the Global category will get CodeChef laddus, with which the winners can claim cool CodeChef goodies. First to solve each problem except challenge — 100 laddus. Know more here.

Good Luck!
Hope to see you participating!!
Happy Programming !!

  • Проголосовать: нравится
  • +109
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Hope to see a balanced contest.

»
4 года назад, # |
  Проголосовать: нравится +39 Проголосовать: не нравится

Very nice problemset! Learned a lot ;)

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +21 Проголосовать: не нравится

    How did you solve RGY and PSTONES? (editorial for these problems are not available)

    • »
      »
      »
      4 года назад, # ^ |
      Rev. 6   Проголосовать: нравится +30 Проголосовать: не нравится

      I feel like the cases of RGY are weak, I'll describe my idea which got 100 (without proofs since I don't have any).

      Lets notice that if we had a had a graph where for each component all nodes had even degree and the total number of edges was even, then there must exist a euler cycle of even length in this component. We perform two operations to achieve this:

      1. Let us repeatedly delete some edge(s) from each node with odd degree by some critera 1 (I'll explain the critera in a bit). Now all components must have a euler path.

      2. If any component has odd size, let us delete an edge by criteria 2 then apply operation 1 if needed.

      All my ideas below use the same idea for both critera 1 and critera 2.

      Testing idea (to get AC) — Delete the edge to the first adjacent. Result — AC but 0 points.

      First idea — let's delete the edge to the adjacent one with minimum degree. Intuition was something like it has less chances of messing up some dense component. Result — 20 points.

      Second idea — let's randomly choose the edge to delete from the adjacency list, if at the end the condition isn't satisfied throw away the result and try again. Result — 40 points, TLE on the 100 (couldn't reach constraints).

      Third idea — what if we bias the random towards edges connecting to smaller degree? Tried with inbuilt geometric distribution function (wanted to try it in a contest) and somehow regressed to 20 pts. Submitted a silly idea of randomly decided whether to select minimum degree or random from the adjacency list. Result — 100 pts.

      I think just a normal weighted random favoring lower degree would also get 100, but I'm still not convinced either of them are right. I can't seem to generate any cases that requires more than 10 runs to get a solution, but this idea doesn't seem to have any basis either. Would appreciate if someone could help in either generating a test case that fails it or proving it is correct.

      My submission for anyone who wants to try break it — https://www.codechef.com/viewsolution/36549002

      Info for anyone trying to break it:

      If the assert at line 280 fails it failed to get an answer in 10 rounds, possibly change it to run for 5 seconds if you want to replicate contest settings, 10 rounds takes around a second for a maxtest as of now. Please tell me if you find a case that fails it.

      If the assert at lines 212 or 265 fails the graph constructed by my submission fails to meet the requirements for even length euler tours, I don't think this should happen but do tell me if you find such a case.

    • »
      »
      »
      4 года назад, # ^ |
      Rev. 3   Проголосовать: нравится +8 Проголосовать: не нравится

      As for PSTONES if you got any partial approach > 10 points could you share your approach?

      Best I could achieve was $$$n^{4} \times 2^{2k}$$$ using subtree dp which was only enough to pass subtask 1 (where it runs considerably faster). Basically $$$dp[node][sz][mask]$$$ for each root and check the values of dp[root][sz][mask]. I combined the current mask of a node with that of a child node naively in $$$O(n^{2} \times 2^{k})$$$. Tried for 3-4 hours but couldn't improve this solution. I tried thinking if I could reroot it to remove that extra n factor or somehow combine it using SOS dp or similar to reduce $$$2^{2k}$$$ to $$$2^{k}$$$ but got no ideas. I feel like this is a dead-end since logically this feel s like knapsack on a range of size $$$n \times 2^{k}$$$ with max value $$$n$$$ for each mask, so $$$2^{k}$$$ times.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +10 Проголосовать: не нравится

      For PSTONES, I used Centroid decomposition technique along with DP. Let's assume that a certain node, v is in set of selected nodes. Then root the tree at this node and order the nodes according to preorder dfs visit time. This ordering will give a linear array and will help us to solve using dp. the dp states will be (current_node, size_of_current_set, mask). So the time complexity of applying dp on array of size n will be O(n*n*(2^K)). If we naively do this for all possible v, the time complexity will be very high and it won't pass. But using centroid decomposition we can obtain the result in O(N*N*(2^K)*log(N)) which passes all test cases. There are few implementation details that I did not mention. CODE

    • »
      »
      »
      4 года назад, # ^ |
      Rev. 4   Проголосовать: нравится +2 Проголосовать: не нравится

      For RGY, I first calculated how many edges do I have to mark with colour Red or green. Then I kept calling some form of modified dfs to detect even length cycles from a random node. I keep doing this until I colour atleast the required amount of edges with +1, or -1. I think there should be a better idea than this since my solution worked in 4.6 seconds while I can see some solutions passed in less than 1 seconds. I had to do too many optimizations to pass it under given time constraints. CODE

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +21 Проголосовать: не нравится

      An $$$O(n\lg n)$$$ solution for RGY :

      while there exists an even cycle in the graph, delete it.

      If a graph doesn't have an even cycle, then it can have maximum $$$\frac{3 (n - 1)}{2}$$$ edges. See this

      One obvious way would be to maintain link-cut trees. A link cut tree can support insert, remove and find path between two vertices in an acyclic graph.

      Start with the empty graph and add edges one by one, whenever you get an even cycle (the stack exchange forum linked above also describes how to construct it) remove those edges.

      For PSTONES :

      Counting subtrees of fixed size

      It's easier to calculate $$$F(mask)$$$ = number of sub-trees whose "frontier" is a subset of $$$mask$$$ (as opposed to exactly $$$mask$$$ as in the problem statement)

      To calculate $$$F(mask)$$$, if an edge $$$(u, v)$$$ contains a color not in $$$mask$$$ then, either both nodes must belong to the required subtree or both must not be a part of the subtree. So merge all these edges, and count the number of subtrees in the new tree, this gives $$$F(mask)$$$.

      If the answer is $$$G(mask)$$$ then, $$$F(mask)$$$ is the sum of $$$G(s)$$$ such that $$$s$$$ is a subset of $$$mask$$$. By reversing the operations in SOS dp we can get $$$G(mask)$$$ from $$$F(mask)$$$.

      This solution is $$$\mathcal{O}(n^2 2^k + n2 ^ k k)$$$

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Its very cool to use fwht in this case ... It is much faster if we apply sum cutoff value to number of set bits

        • »
          »
          »
          »
          »
          4 года назад, # ^ |
          Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

          My fwht do you mean, fast walsh hadamard transform? I don't think that it can improve the complexity, the DP has $$$\mathcal{O}(n^2 2 ^ k)$$$ states ($$$2^k$$$ masks, $$$n$$$ subtree sizes and $$$n$$$ roots of subtree)

          • »
            »
            »
            »
            »
            »
            4 года назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            Ohh ... I was saying for reduction of 2**(2k) to k*2**k because i am not that much familiar with SOS DP .

      • »
        »
        »
        »
        4 года назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        My actual solution was to use knapsack DP on trees to assume each node as root ( as we do in DP On Trees ) and i optimized it through fwht ( where in case of very less set bits i did it using brute force and if not done it was giving TLE ) and Heavy child concept by copying the DP of that heavy child thereby saving a large number of iterations ..

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +27 Проголосовать: не нравится

    Intended solution for RGY:

    The general idea is: repeatedly find an even circuit, color its edges with alternating colors, and remove it.

    Let's call our graph $$$G$$$. Let's create another graph $$$G_2$$$ that's initially empty and try to move the edges from $$$G$$$ to $$$G_2$$$ such that:

    • $$$G_2$$$ is always a forest
    • At the end, $$$G$$$ is a matching (every vertex's degree is at most 1.)

    Assume $$$G$$$ is not a matching, then we can find 2 edges $$$(a,b)$$$ and $$$(a,c)$$$ that share an endpoint, $$$a$$$. If $$$a$$$ and $$$b$$$ belong to different components in $$$G_2$$$, move that edge from $$$G$$$ to $$$G_2$$$. Similarly with $$$(b,c)$$$. Otherwise, $$$a$$$, $$$b$$$, and $$$c$$$ all belong to the same component. So now, either $$$(a,b)$$$ closes an even cycle with the path connecting them in $$$G_2$$$, $$$(b,c)$$$ closes an even cycle with the path connecting them in $$$G_2$$$, or both cycles are odd. In that case, their union is even! That is, the circuit from $$$b$$$ to $$$c$$$ then to $$$a$$$ then to $$$b$$$ is even. In all cases, we can find an even circuit and remove it.

    Do this while $$$G$$$ is not a matching. After we're done, $$$G$$$ has at most $$$\frac{n}{2}$$$ edges and $$$G_2$$$ has at most $$$n-1$$$ edges. Since $$$G_2$$$ is always a forest, you can accelerate the operations to amortized $$$O(mlog(n))$$$ with link/cut tree.

    Code

    Challenge: if you remove ALL the even circuits, all the cycles in the graph will be simple. Hence, the number of yellow edges will be at most $$$\frac{4n}{3}-1$$$, so in theory there's a solution with $$$R_{max}=\frac{4}{3}$$$. Try to prove this is actually a tight bound. The problem is, how to reach this bound efficiently? My best solution is $$$O(m+nlog^2(n))$$$ and requires full dynamic connectivity on general graphs.

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Since no editorial / discussion has been posted on codechef for the challenge problem yet, what were the approaches to reduce the cost below around 45 million (~82.5 points)?

My basic idea was bfs-ing from k distinct points (ensuring all components are covered) and trying to choose as many distinct vertices as possible with the largest distance from an end point which haven't been evaluvated yet in each step. This got around 45 million. However even after adding further ideas like applying something similar to the approximate greedy for k centers as the base for a weighted random my score was still around the 45 million range.

Anyone care to share your approach and cost achieved? (possibly the intuition behind it also if you have time and don't mind)