#include<iostream>
using namespace std;
const int n=5;
static int arc[100][100];
static color[100];
int ok(int i)
{
for(int j=0;j<n;j++)
if(arc[i][j]==1&&color[i]==color[j])
return 0;
return 1;
}
void ColorGraph(){
int k=0;
int flag=1;
while(flag==1)
{
k++;
flag=0;
for(int i=0;i<n;i++)
{
if(color[i]==0)
{
color[i]=k;
if(!ok(i))
{
color[i]=0;flag=1;
}
}
}
}
}
int main()
{
cout<<"输入颜色的个数:";
int m;
cin>>m;
cout<<"输入无向图点和边的关系:"<<endl;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
int a;
cin>>a;
arc[i][j]=a;
}
ColorGraph();
cout<<"out color is"<<endl;
for(i=0; i<n; i++)
{
cout<<color[i]<<" ";
}
cout<<endl;
return 0;
}
//输入的数组
//0 1 0 0 0
//1 0 1 1 0
//0 1 0 0 1
//0 1 0 0 1
//0 0 1 1 0
时间复杂度:O(k*n)