• uva 193 Graph Coloring(图染色 dfs回溯)


    Description

    You are to write a program that tries to find an optimal coloring for a given graph. Colors are applied to the nodes of the graph and the only available colors are black and white. The coloring of the graph is called optimal if a maximum of nodes is black. The coloring is restricted by the rule that no two connected nodes may be black.

     figure22

    Figure: An optimal graph with three black nodes

    Input and Output

    The graph is given as a set of nodes denoted by numbers tex2html_wrap_inline33 , tex2html_wrap_inline35 , and a set of undirected edges denoted by pairs of node numbers tex2html_wrap_inline37 , tex2html_wrap_inline39 . The input file contains m graphs. The number m is given on the first line. The first line of each graph contains n and k, the number of nodes and the number of edges, respectively. The following k lines contain the edges given by a pair of node numbers, which are separated by a space.
    
     
    
    The output should consists of 2m lines, two lines for each graph found in the input file. The first line of should contain the maximum number of nodes that can be colored black in the graph. The second line should contain one possible optimal coloring. It is given by the list of black nodes, separated by a blank.

    Sample Input

    1
    6 8
    1 2
    1 3
    2 4
    2 5
    3 4
    3 6
    4 6
    5 6

    Sample Output

    3
    1 4 5

    题意: 一开始题意理解错了。题意是这样的,给出一个图,让你染成黑白两种颜色,但是相连的两点不能都染成黑色,问最多能染色多少的黑色点。

    思路:直接dfs回溯,庆幸的是没有超时。dfs(int u/*u表示当前正在判断的点*/,int num/*num表示黑点的数量*/),u初值为1,num初值为0,然后分为两种情况,第一种情况先判断与当前点u相连的点有没有染成黑色的,如果有的话直接把当前点u染成白色,(即dfs(u+1,num);return;),否则进入第二种情况:将当前点u染成黑色或者白色。

           if(u>=n+1)边界限制,ans取更大值。最后输出的格式注意。

    AC代码:

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 106
    23 #define inf 1e12
    24 int n,m;
    25 int mp[N][N];
    26 int ans;
    27 int color[N];
    28 int res[N];
    29 void dfs(int u/*u表示当前正在判断的点*/,int num/*num表示黑点的数量*/){
    30    if(u>=n+1){
    31       if(ans<num){
    32          ans=num;
    33          for(int i=1;i<=n;i++){
    34            res[i]=color[i];
    35          }
    36       }
    37       return;
    38    }
    39 
    40    for(int i=1;i<=n;i++){
    41       if(mp[u][i] && color[i]){
    42          dfs(u+1,num);//只能染成白色的情况,否则下面的情况能染成黑色或白色
    43          return;
    44       }
    45    }
    46    color[u]=1;
    47    dfs(u+1,num+1);//能染成黑色
    48 
    49    color[u]=0;
    50    dfs(u+1,num);//能染成白色
    51 
    52 }
    53 int main()
    54 {
    55    int t;
    56    scanf("%d",&t);
    57    while(t--){
    58       memset(mp,0,sizeof(mp));
    59       scanf("%d%d",&n,&m);
    60       for(int i=0;i<m;i++){
    61          int a,b;
    62          scanf("%d%d",&a,&b);
    63          mp[a][b]=mp[b][a]=1;
    64       }
    65       ans=0;
    66       memset(color,0,sizeof(color));
    67       dfs(1,0);
    68 
    69       printf("%d
    ",ans);
    70       int w=0;
    71       for(int i=1;i<=n;i++){
    72          if(res[i]){
    73             w++;
    74             printf("%d",i);
    75             if(w!=ans){
    76                printf(" ");
    77             }
    78          }
    79 
    80       }
    81       printf("
    ");
    82 
    83    }
    84     return 0;
    85 }
    View Code

     

  • 相关阅读:
    Hadoopif.for.while 语句
    完全分布模式的四大模块设置
    语法糖定义
    Karaf 依赖equinox and felix,karaf 本Apache的很多项目作为基础框架
    Karaf 基于 osgi
    MEF(Managed Extensibility Framework) 微软平台插件化开发
    析构函数,构造函数
    C#和ASP.Net面试题目集锦
    论C# java的基本类型
    Boolean.parseBoolean("true") 和 Boolean.getBoolean("true");的区别及用法
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5020641.html
Copyright © 2020-2023  润新知