An interesting problem

Правка en3, от stould, 2015-12-04 07:29:55

Hello guys. I'm trying to solve the problem Circuits from spoj. After think a lot, I found a greedy solution to solve this problem, follow

'Supposing all components have no cycles yet'
1) Separate all components
2) Get the component with less vertices and the component with most vertices.
3) Merge this two componentes this way:
A = first component
B = second component
From A, get the two vertices (may be the same vertex) that have the greater distante.
From B, the same thing.
4) Create a link (connect the vertices from A and B) between this four vertices:
graph[A[0]].push_back(B[0]);
graph[B[0]].push_back(A[0]);
graph[A[1]].push_back(B[1]);
graph[B[1]].push_back(A[1]);
5) Find the formed cycle (and all vertices on the cycle)
6) For each node in the cycle:
If the node have all the neighbors in the cycle, I just remove it.
7) Generate again the componentes, and the process repeats until I find nodes without a cycle.

I guess this algorithm above work fine (at least I tested in several cases). But it is very complicated.

Does anyone agree with this solution or I can do something easier ? Sorry for the long text and my poor english.

Теги graph, greedy

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en9 Английский stould 2015-12-07 06:14:13 37
en8 Английский stould 2015-12-04 19:07:10 52 Reverted to en4
en7 Английский stould 2015-12-04 19:02:59 72
en6 Английский stould 2015-12-04 19:02:05 36
en5 Английский stould 2015-12-04 19:00:59 16
en4 Английский stould 2015-12-04 07:30:50 0 (published)
en3 Английский stould 2015-12-04 07:29:55 9 Tiny change: 'ind nodes in cycle.\n~' -> 'ind nodes without a cycle.\n~'
en2 Английский stould 2015-12-04 07:28:57 7 Tiny change: 'ind nodes without cycle.\n~' -> 'ind nodes in cycle.\n~' (saved to drafts)
en1 Английский stould 2015-12-04 07:25:55 1253 Initial revision (published)