C++-蓝桥杯-分考场[2017真题][搜索][无向图染色]

着实精妙!

女少口阿!女少口阿!

 1 #include <iostream>
 2 #define N 100//思路:可以抽象为无向图染色问题。相邻顶点不能染相同颜色,问至少要用多少种颜色。 
 3 using namespace std;
 4 int n,m,a,b,ans,next[N][N],map[N][N];
 5 void dfs(int x,int col){
 6     if(col>=ans)return;
 7     if(x==n+1){if(col<ans)ans=col;return;}
 8     for(int i=1,k;i<=col;i++){
 9         for(k=0;next[k][i]&&!map[x][next[k][i]];k++);
10         if(!next[k][i]){
11             next[k][i]=x;
12             dfs(x+1,col);
13             next[k][i]=0;
14         }
15     }
16     next[0][col+1]=x;
17     dfs(x+1,col+1);
18     next[0][col+1]=0;
19 }
20 int main(){
21     for(cin>>n>>m,ans=n;m--;cin>>a>>b,map[a][b]=map[b][a]=1);
22     dfs(1,0),cout<<ans<<endl;
23     return 0;
24 }

 

posted @ 2020-03-12 20:38  墨鳌  阅读(350)  评论(0编辑  收藏  举报