我想查找圖形是否可以2色或不更多。雙方或非雙方。如何檢查圖形是否可着色?
這裏是我在C++中的代碼我正在使用威爾士鮑威爾算法,但代碼中有些錯誤可能是我錯過了一些角落案例或一些邏輯錯誤。
輸入n =沒有。頂點,m =否。基於0的索引
#include <iostream>
#include <algorithm>
using namespace std;
pair <int,int> s[1001];
int comp(pair <int,int> s1, pair <int,int> s2)
{
if(s1.first>s2.first)
return 0;
else
return 1;
}
int main()
{
int n,i,j,k,flag=0;
bool a[1001][1001]={false};
int s1[1001]={0};
int s3[1001]={0};
for(i=0;i<1001;i++)
{
s[i].first=0;
s[i].second=i;
//s1[i]=0;
}
long long m;
cin>>n>>m;
while(m--)
{
int x,y;
cin>>x>>y;
if(x==y)
continue;
a[x][y]=true;
a[y][x]=true;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i][j]==true)
s[i].first++;
sort(s,s+n,comp);
int color=1,p=0,z,f;
for(i=0;i<n;i++)
{
k = s[n-i-1].second;
if(s1[k]==0)
{
s1[k]=color;
p=0;
s3[p++]=k;
for(j=n-1;j>=0;j--)
{
f=0;
if(s1[s[j].second]==0)
{
for(z=0;z<p;z++)
{
if(a[s3[z]][s[j].second]==false || s3[z]==s[j].second)
continue;
else
{
f=1;
break;
}
}
if(f==1)
continue;
else
{
s3[z]=s[j].second;
p++;
s1[s[j].second]=color;
}
}
}
color++;
}
if(color==3)
break;
}
for(i=0;i<n;i++)
if(s1[i]==0)
{
flag=1;
break;
}
if(flag==1)
cout<<"NO\n";
else
cout<<"YES\n";
return 0;
}
請解釋您是如何知道這是錯誤的? – Guenther
它來自現場比賽,所以我不能在這裏討論這個問題。 –
如果你不能討論它,你會期待我們如何幫助你? – SurvivalMachine