poj1419 Graph Coloring,无向图,最大独立集

最大独立集 = 补图的最大团

最小顶点覆盖 + 最大独立集 = V

#include <stdio.h>
#include <string.h>

const int maxn =100 + 10;
int g[maxn][maxn], dp[maxn], n;
int x[maxn], ans[maxn], mx;
int dfs(int *adj, int ns, int dep) {
    int t[maxn];
    if(0 == ns) {
        if(dep > mx) {
            for(int i=0; i<dep; ++i) ans[i] = x[i]; //路径信息
            mx = dep;
            return 1;
        }
        return 0;
    }
    int i, j, cnt;
    for(i=0; i < ns; ++i) {
        int& k = adj[i];
        if(dep + n - k <= mx) return 0;
        if(dep + dp[k] <= mx) return 0;
        x[dep] = k; //路径信息
        for(cnt =0, j = i+1; j < ns; ++j) {
            int& p = adj[j];
            if(g[k][p]) t[cnt++]= p;
        }
        if(dfs(t, cnt, dep+1)) return 1;
    }
    return 0;
}

int max_clique(int n) {
    int i, j, ns;
    int adj[maxn];
    if(n<=0) return 0;
    mx = 0;
    for(i = n-1; i >= 0; --i) {
        x[0] = i; //路径信息
        for(ns = 0, j=i+1; j < n; ++j)
            if(g[i][j]) adj[ns++] = j;
        dfs(adj, ns, 1);
        dp[i] = mx;
    }
    return mx;
}

int main() {
    int t, m, i, j, v, u;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&n,&m);
        for(i=0; i<=n; ++i)
            for(j=0; j<=n; ++j)
                g[i][j] = 1;
        for(i=0; i<m; ++i) {
            scanf("%d%d",&v,&u);
            v--;u--;
            g[v][u] = g[u][v] = 0;
        }
        int mmax = max_clique(n);
        printf("%d\n",mmax);
        for(i=0; i<mmax; ++i) {
            printf("%d ",ans[i]+1);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-12-18 12:46:53

poj1419 Graph Coloring,无向图,最大独立集的相关文章

POJ1419 Graph Coloring(最大独立集)(最大团)

Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4926   Accepted: 2289   Special Judge 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 Coloring(最大独立集模板题)

Graph Coloring POJ - 1419 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

poj1419 Graph Coloring 最大独立集(最大团)

最大独立集: 顶点集V中取 K个顶点,其两两间无连接. 最大团: 顶点集V中取 K个顶点,其两两间有边连接. 最大独立集=补图的最大团最大团=补图的最大独立集 #include<iostream> #include<cstring> #include<cstdio> using namespace std; int mp[110][110],mark1[505],mark2[505]; int n,m; int cnt,maxx; void dfs(int x) { i

GPS-Graph Processing System Graph Coloring算法分析 (三)

Graph coloring is the problem of assigning a color to each vertex of an undirected graph such that no two adjacent vertices have the same color. We implement the greedy algorithm from Scalable parallel graph coloring algorithms. The algorithm iterati

uva193 - Graph Coloring

Graph Coloring 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 maxim

【POJ 1419】Graph Coloring

[POJ 1419]Graph Coloring 求图的最大独立集 最大独立集=补图最大团 很适合理解最大团/最大独立集的模板题 建立补图套模板既可 需要输出集合点 原本想用stack 但发现copy比较麻烦 vector用一个iterator指针 循环很便利 代码如下: #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <vecto

193 - Graph Coloring(DFS)

题目:193 - Graph Coloring 题目大意:给出一个图,图里面有点和边,要求相邻的点不可以都是黑色的,问怎样上色黑色的点最多的,给出字典序最大的那种组合情况. 解题思路:dfs每个点是黑是白,将黑的点保存记录下来,然后下次再试探某个点是黑点的时候,就看看这个点和之前的那些点有没有相邻,相邻就表示这个点不是黑点.每个试探的点只需要是这个点之后的点就可以了,因为前面的点已经试探过了. 代码: #include <stdio.h> #include <string.h> c

UVA Graph Coloring

主题如以下: Graph Coloring  You are to write a program that tries to find an optimal coloring for agiven graph. Colors are applied to the nodes of the graph and the only availablecolors are black and white. The coloring of the graph is called optimalif a

uva 193 Graph Coloring(回溯)

uva 193 Graph Coloring 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