Здравствуйте, есть код для нахождения хроматического числа. До 4 вершин работает правильно, но с 5 и выше находит не правильно, в данном примере необходимо 3 цвета, а он находит 2. Помогите исправить:

<span class="co2" style="color: #339900;">#include "stdafx.h"</span> <span class="co2" style="color: #339900;">#include <iostream></span> <span class="kw2" style="color: #0000ff;">using</span> <span class="kw2" style="color: #0000ff;">namespace</span> std<span class="sy4" style="color: teal;">;</span> <span class="kw4" style="color: #0000ff;">int</span> main<span class="br0" style="color: green;">(</span><span class="br0" style="color: green;">)</span> <span class="br0" style="color: green;">{</span> setlocale<span class="br0" style="color: green;">(</span>LC_ALL, <span class="st0" style="color: #ff0000;">"Russian"</span><span class="br0" style="color: green;">)</span><span class="sy4" style="color: teal;">;</span> <span class="kw4" style="color: #0000ff;">const</span> <span class="kw4" style="color: #0000ff;">int</span> n <span class="sy1" style="color: #000080;">=</span> <span class="nu0" style="color: #0000dd;">5</span><span class="sy4" style="color: teal;">;</span> <span class="kw4" style="color: #0000ff;">int</span> i, j<span class="sy4" style="color: teal;">;</span> <span class="kw4" style="color: #0000ff;">int</span> mi<span class="br0" style="color: green;">[</span>n<span class="br0" style="color: green;">]</span><span class="br0" style="color: green;">[</span>n<span class="br0" style="color: green;">]</span> <span class="sy1" style="color: #000080;">=</span> <span class="br0" style="color: green;">{</span> <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">0</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">1</span>, <span class="nu0" style="color: #0000dd;">0</span> <span class="br0" style="color: green;">}<cut></span><span class="sy4" style="color: teal;">;</span> <span class="kw3" style="color: #0000dd;">cout</span> <span class="sy1" style="color: #000080;"><<</span> <span class="st0" style="color: #ff0000;">"Вывод матрицы: "</span> <span class="sy1" style="color: #000080;"><<</span> endl<span class="sy4" style="color: teal;">;</span> <span class="kw1" style="color: #0000ff;">for</span> <span class="br0" style="color: green;">(</span>i <span class="sy1" style="color: #000080;">=</span> <span class="nu0" style="color: #0000dd;">0</span><span class="sy4" style="color: teal;">;</span> i <span class="sy1" style="color: #000080;"><</span> n<span class="sy4" style="color: teal;">;</span> <span class="sy2" style="color: #000040;">++</span>i<span class="br0" style="color: green;">)</span> <span class="br0" style="color: green;">{</span> <span class="kw1" style="color: #0000ff;">for</span> <span class="br0" style="color: green;">(</span>j <span class="sy1" style="color: #000080;">=</span> <span class="nu0" style="color: #0000dd;">0</span><span class="sy4" style="color: teal;">;</span> j <span class="sy1" style="color: #000080;"><</span> n<span class="sy4" style="color: teal;">;</span> <span class="sy2" style="color: #000040;">++</span>j<span class="br0" style="color: green;">)</span> <span class="br0" style="color: green;">{</span> <span class="kw3" style="color: #0000dd;">cout</span> <span class="sy1" style="color: #000080;"><<</span> mi<span class="br0" style="color: green;">[</span>i<span class="br0" style="color: green;">]</span><span class="br0" style="color: green;">[</span>j<span class="br0" style="color: green;">]</span> <span class="sy1" style="color: #000080;"><<</span> <span class="st0" style="color: #ff0000;">' '</span><span class="sy4" style="color: teal;">;</span> <span class="br0" style="color: green;">}</span> <span class="kw3" style="color: #0000dd;">cout</span> <span class="sy1" style="color: #000080;"><<</span> endl<span class="sy4" style="color: teal;">;</span> <span class="br0" style="color: green;">}</span> <span class="co1" style="color: #666666;">//Хроматическое число:</span> <span class="kw4" style="color: #0000ff;">int</span> col<span class="br0" style="color: green;">[</span>n<span class="br0" style="color: green;">]</span><span class="sy4" style="color: teal;">;</span> <span class="kw1" style="color: #0000ff;">for</span> <span class="br0" style="color: green;">(</span>i <span class="sy1" style="color: #000080;">=</span> <span class="nu0" style="color: #0000dd;">0</span><span class="sy4" style="color: teal;">;</span> i <span class="sy1" style="color: #000080;"><</span> n<span class="sy4" style="color: teal;">;</span> <span class="sy2" style="color: #000040;">++</span>i<span class="br0" style="color: green;">)</span> col<span class="br0" style="color: green;">[</span>i<span class="br0" style="color: green;">]</span> <span class="sy1" style="color: #000080;">=</span> <span class="nu0" style="color: #0000dd;">1</span><span class="sy4" style="color: teal;">;</span> <span class="kw1" style="color: #0000ff;">for</span> <span class="br0" style="color: green;">(</span>i <span class="sy1" style="color: #000080;">=</span> <span class="nu0" style="color: #0000dd;">0</span><span class="sy4" style="color: teal;">;</span> i <span class="sy1" style="color: #000080;"><</span> n<span class="sy4" style="color: teal;">;</span> <span class="sy2" style="color: #000040;">++</span>i<span class="br0" style="color: green;">)</span> <span class="kw1" style="color: #0000ff;">for</span> <span class="br0" style="color: green;">(</span>j <span class="sy1" style="color: #000080;">=</span> i <span class="sy2" style="color: #000040;">+</span> <span class="nu0" style="color: #0000dd;">1</span><span class="sy4" style="color: teal;">;</span> j <span class="sy1" style="color: #000080;"><</span> n <span class="sy2" style="color: #000040;">+</span> <span class="nu0" style="color: #0000dd;">1</span><span class="sy4" style="color: teal;">;</span> <span class="sy2" style="color: #000040;">++</span>j<span class="br0" style="color: green;">)</span> <span class="kw1" style="color: #0000ff;">if</span> <span class="br0" style="color: green;">(</span>mi<span class="br0" style="color: green;">[</span>i<span class="br0" style="color: green;">]</span><span class="br0" style="color: green;">[</span>j<span class="br0" style="color: green;">]</span> <span class="sy1" style="color: #000080;">==</span> <span class="nu0" style="color: #0000dd;">1</span> <span class="sy3" style="color: #000040;">&&</span> col<span class="br0" style="color: green;">[</span>j<span class="br0" style="color: green;">]</span> <span class="sy1" style="color: #000080;">==</span> col<span class="br0" style="color: green;">[</span>i<span class="br0" style="color: green;">]</span><span class="br0" style="color: green;">)</span> col<span class="br0" style="color: green;">[</span>j<span class="br0" style="color: green;">]</span> <span class="sy1" style="color: #000080;">=</span> col<span class="br0" style="color: green;">[</span>i<span class="br0" style="color: green;">]</span> <span class="sy2" style="color: #000040;">+</span> <span class="nu0" style="color: #0000dd;">1</span><span class="sy4" style="color: teal;">;</span> <span class="kw4" style="color: #0000ff;">int</span> max <span class="sy1" style="color: #000080;">=</span> col<span class="br0" style="color: green;">[</span><span class="nu0" style="color: #0000dd;">0</span><span class="br0" style="color: green;">]</span><span class="sy4" style="color: teal;">;</span> <span class="kw1" style="color: #0000ff;">for</span> <span class="br0" style="color: green;">(</span>j <span class="sy1" style="color: #000080;">=</span> <span class="nu0" style="color: #0000dd;">1</span><span class="sy4" style="color: teal;">;</span> j <span class="sy1" style="color: #000080;"><</span> n<span class="sy4" style="color: teal;">;</span> <span class="sy2" style="color: #000040;">++</span>j<span class="br0" style="color: green;">)</span> <span class="kw1" style="color: #0000ff;">if</span> <span class="br0" style="color: green;">(</span>max <span class="sy1" style="color: #000080;"><</span> col<span class="br0" style="color: green;">[</span>j<span class="br0" style="color: green;">]</span><span class="br0" style="color: green;">)</span> max <span class="sy1" style="color: #000080;">=</span> col<span class="br0" style="color: green;">[</span>j<span class="br0" style="color: green;">]</span><span class="sy4" style="color: teal;">;</span> <span class="kw3" style="color: #0000dd;">cout</span> <span class="sy1" style="color: #000080;"><<</span> <span class="st0" style="color: #ff0000;">"<span class="es1" style="color: #000099; font-weight: bold;">\n</span>Хроматическое число графа равно: "</span> <span class="sy1" style="color: #000080;"><<</span> max<span class="sy1" style="color: #000080;"><<</span>endl<span class="sy4" style="color: teal;">;</span> <span class="kw1" style="color: #0000ff;">return</span> <span class="nu0" style="color: #0000dd;">0</span><span class="sy4" style="color: teal;">;</span> <span class="br0" style="color: green;">}</span>
24.12.16
1 ответ

Ответы

Обращайтесь в чат, обсудим условия.
Но вообще то, хроматическое число графа из Вашего примера и есть 2.
24.12.16

Виталий

Читать ответы

Андрей Соловьев

Читать ответы

Алексей

Читать ответы
Посмотреть всех экспертов из раздела Технологии > C/C++
Пользуйтесь нашим приложением Доступно на Google Play Загрузите в App Store