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

Автор flamie, история, 4 года назад, По-русски

Как решить эту задачу? Заранее спасибо. https://codeforces.net/edu/course/2/lesson/8/4/practice/contest/290943/problem/D

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

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

Why can't u start bfs from each node and when u see distance > 2 u stop bfs?

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

Here is my solution which actually passed. The idea is you work with edges and use degrees to calculate, then u subtract pairs who are adjacent, that is u counted wrong pairs and you should search for 3 size cycles and subtract all members of that 3 size cycles. About complexity I don't know why it actually passed, the thing is only cycle checking is confusing, probably it can be easily done with dfs (linearly).

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

Here is my solution which actually passed.

The idea is that you should look at edges and count pairs from both ends.

The only problem we should take care is that we counted pairs who are adjacent, but also 2 edges far away, we should take care of that.

To fix that, we search for all 3 size cycles, and subtract all members of that cycle from the answer and that's it.

Probably 3 size cycle searching can be done in linear time, idk how, I used easy method (can't tell the time complexity of it, probably it is linear as well or closer to linear or test cases are weak), which worked. See code for details.

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

    This will take $$$O(n^2)$$$ if the input graph is a star with center being the node $$$\frac{n}{2}$$$. Half of the nodes will pass $$$j\leq i$$$ condition which will then have the other half elements pass $$$k\leq j$$$ condition ( $$$j$$$ being $$$\frac{n}{2}$$$ in this graph, almost all of the times)

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

      Do you know how to find the number of 3 size cycles in linear time? I was not able to figure it out since I'm weak at graphs and just wrote brute force to verify my idea by getting TLE, but got AC.