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

Автор Neeki, история, 6 месяцев назад, По-английски

I tried a greedy solution for this problem:
1. If there exist a pair of adjacent vertices like $$$(u,v)$$$ such that $$$deg(u)\geq3$$$ and $$$deg(v)\geq3$$$, then remove the edge between $$$u$$$ and $$$v$$$.
2. Then, if there exist a pair of adjacent vertices like $$$(u,v)$$$ such that $$$deg(u)\geq3$$$ and $$$deg(v)\geq2$$$, then remove the edge between $$$u$$$ and $$$v$$$.
3. Then, if there exist a pair of adjacent vertices like $$$(u,v)$$$ such that $$$deg(u)\geq3$$$ and $$$deg(v)\geq1$$$, then remove the edge between $$$u$$$ and $$$v$$$.
At last, I add edges between the leaves of different components .
Why isn't it correct??

submission

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

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

hello, i think the problem is in your code not in your algorithm because, in your code in the section that checks the first 3 steps if deg[u] or deg[i] is less than what it meant to be, you shouldn't break, you should continue, this is why it fails the second test case in test 2, in that test case 14 is connected to nodes 1 , 16 and 18, your code checks the pair 14 and 1, and because it don't met the condition No.1 it breaks out, but if it continue, it will find the pair 14 , 18.

i hope this help (this code i said doesn't work completely too, maybe there are some other problems)