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 maximum of nodes is black. The coloring is restricted by the rule that no two connected nodes may be black.


Figure: An optimal graph with three black nodes

Input and Output

The graph is given as a set of nodes denoted by numbers , , and a set of undirected edges denoted by pairs of node numbers , . 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
 
求图的最大独立集,即把尽量多的结点图黑使得任意两个黑点不相邻
 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=100+5;
int m,n,k;
vector<int> G[maxn];
int node[maxn], black_node[maxn];
int ans;
enum {white, black};

void dfs(int d, int cnt) {
        if(d==n) {
                if(cnt>ans) {
                        int j=0;
                        for(int i=0;i<n;i++) if(node[i]) black_node[j++]=i+1;
                        ans=cnt;
                }
                return;
        }

        if(n-d+cnt<=ans)
                return;
        //尝试黑的
        int ok=true;
        node[d]=black;
        for(int i=0;i<G[d].size();i++) {
                int v=G[d][i];
                //两个相邻黑点
                if(node[v]==black) {
                        ok=false;
                        break;
                }
        }
        if(ok) dfs(d+1, cnt+1);

        //尝试白的
        node[d]=white;
        dfs(d+1, cnt);
}

int main()
{
#ifndef ONLINE_JUDGE
        freopen("./uva193.in", "r", stdin);
#endif
        int x,y;
        cin>>m;
        while(m--) {
                cin>>n>>k;
                memset(G, 0, sizeof(G));
                memset(node, 0, sizeof(node));
                ans=0;
                for(int i=0;i<k;i++) {
                        cin>>x>>y;
                        x--;y--;
                        G[x].push_back(y);
                        G[y].push_back(x);
                }
                dfs(0, 0);
                printf("%d\n", ans);
                for(int i=0;i<ans-1;i++)
                        printf("%d ", black_node[i]);
                printf("%d\n", black_node[ans-1]);

        }

    return 0;
}

uva193 - Graph Coloring

时间: 2024-10-14 21:55:53

uva193 - Graph Coloring的相关文章

193 - Graph Coloring(DFS)

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

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

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

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

【POJ 1419】Graph Coloring

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

poj 1419 Graph Coloring

http://poj.org/problem?id=1419 题意: 一张图黑白染色,相邻点不能都染黑色,最多能染几个黑色点 最大点独立集 但是图不能同构为二分图,不能用二分图匹配来做 那就爆搜吧 还可以转化为补图的最大团问题 #include<cstdio> #include<cstring> #include<iostream> using namespace std; bool map[101][101]; int color[101],res[101]; int

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

图着色算法详解(Graph Coloring)

图着色算法描述: https://www.jianshu.com/p/6a52b390f5fa 给定无向连通图和m种不同的颜色.用这些颜色为图G的各顶点着色,每个顶点着一种颜色.是否有一种着色法使G中每条边的两个顶点有不同的颜色. 这个问题是图的m可着色判定问题.若一个图最少需要m种颜色才能使图中每条边相连接的两个顶点着不同颜色,称这个数m为这个图的色数. 求一个图的色数m称为图的m可着色优化问题. 给定一个图以及m种颜色,请计算出涂色方案数. 分析: 细致分析后,t代表顶点还是能分析出来的.