zoukankan      html  css  js  c++  java
  • POJ Graph Coloring (暴力染色or最大独立集)

    题目

    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.

    Figure 1: An optimal graph with three black nodes

    Input
    The graph is given as a set of nodes denoted by numbers 1...n, n <= 100, and a set of undirected edges denoted by pairs of node numbers (n1, n2), n1 != n2. 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.

    Output
    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

    思路

    这个题目其实可以暴力染色,就是说,我们按顺序丢n个点进去判断,如果check(相邻点没有黑色颜色)成功,我们就记录这个点,然后dfs就可以了。那么另外的一种解法就是求补图的最大团,补图的最大团就是原图的最大独立点集,就是两两不相邻的点个数,这个点集就是可以被最大染成黑色。

    代码实现

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
    #define per(i,n,a) for (int i=n;i>=a;i--)
    #define MT(x,i) memset(x,i,sizeof(x) )
    #define rev(i,start,end) for (int i=0;i<end;i++)
    #define inf 0x3f3f3f3f
    #define mp(x,y) make_pair(x,y)
    #define lowbit(x) (x&-x)
    #define MOD 1000000007
    #define exp 1e-8
    #define N 1000005 
    #define fi first 
    #define se second
    #define pb push_back
    typedef long long ll;
    typedef pair<int ,int> PII;
    ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
    inline int read() {
        char ch=getchar(); int x=0, f=1;
        while(ch<'0'||ch>'9') {
            if(ch=='-') f = -1;
            ch=getchar();
        } 
        while('0'<=ch&&ch<='9') {
            x=x*10+ch-'0';
            ch=getchar();
        }   return x*f;
    }
    const int maxn=101;
    int n,m;
    int ans,cnt;
    int g[maxn][maxn];
    int color[maxn];
    int res[maxn];
    
    bool check (int x) {
       rep (i,1,n) {
           if (g[x][i]&&color[i]==1) return false;
       }
       return true;
    }
    
    void dfs  (int u) {
       if (u>n) {
           ans=cnt;
           int k=0;
           rep (i,1,n) {
               if (color[i]==1) res[++k]=i;
           }
          return;
       }
       if (cnt+n-u+1<=ans) return ;
       if (check (u)) {
          cnt++;
          color[u]=1;
          dfs (u+1);
          cnt--;
       } 
       color[u]=2;
       dfs (u+1);
    }
    
    
    int main () {
       int t;
       cin>>t;
       while (t--) {
          cin>>n>>m;
          MT (g,0);
          MT (color,0);
          rep (i,1,m) {
              int x,y;
              x=read (); y=read ();
              g[x][y]=g[y][x]=1;      
          }
          ans=cnt=0;
          dfs (1);
          cout<<ans<<endl;
          rep (i,1,ans) cout<<res[i]<<" ";
          cout<<endl;
       }
    
    
        return 0;
    }
    
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
    #define per(i,n,a) for (int i=n;i>=a;i--)
    #define MT(x,i) memset(x,i,sizeof(x) )
    #define rev(i,start,end) for (int i=0;i<end;i++)
    #define inf 0x3f3f3f3f
    #define mp(x,y) make_pair(x,y)
    #define lowbit(x) (x&-x)
    #define MOD 1000000007
    #define exp 1e-8
    #define N 1000005 
    #define fi first 
    #define se second
    #define pb push_back
    typedef long long ll;
    typedef pair<int ,int> PII;
    ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
    inline int read() {
        char ch=getchar(); int x=0, f=1;
        while(ch<'0'||ch>'9') {
            if(ch=='-') f = -1;
            ch=getchar();
        } 
        while('0'<=ch&&ch<='9') {
            x=x*10+ch-'0';
            ch=getchar();
        }   return x*f;
    }
    const int maxn=101;
    int n,m;
    int t,ans,num;
    int group [N];
    int g[maxn][maxn];
    int now[N];
    
    bool dfs (int u) {
        if (u>n) {
            ans=num;
            rep (i,1,n) {
                group[i]=now[i];
            }
            return ;
        }
        int flag=0;
        rev (i,1,u) {
           if (now[i]&&!g[i][u]) {
               flag=1;
               break;
           }
        }
        if (!flag)  {
            num++;
            now[u]=1;
            dfs (u+1);
            num--;
            now[u]=0;
        }
        if (num+n-u>ans) dfs (u+1);
    }
    
    int main () {
        cin>>t;
        while (t--) {
            cin>>n>>m;
            MT (g,1);
            MT (now,0);
            MT (group,0);
            rep (i,1,n) {
                int x,y;
                cin>>x>>y;
                g[x][y]=g[y][x]=0;
            }
            dfs (1);
            cout<<ans<<endl;
            rep (i,1,n) {
                if (group[i]) cout<<i<<" ";
            }
            cout<<endl;
        }
    
    
        return 0;
    }
    
  • 相关阅读:
    来自平时工作中的javascript知识的积累---持续补充中
    javascript function
    CSS3 3D变换
    HTTP1.1缓存策略
    jQuery插件开发
    mac下好用的工具收录(慢慢完善)
    mac 彻底卸载vscode
    Git冲突:commit your changes or stash them before you can merge. 解决办法(转载)
    关于vscode使用的一些设置
    (linux服务器)apache开启gzip的配置以及效果对比
  • 原文地址:https://www.cnblogs.com/hhlya/p/13416743.html
Copyright © 2011-2022 走看看