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的更多相关文章

  1. POJ 1419 Graph Coloring(最大独立集/补图的最大团)

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4893   Accepted: 2271   ...

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

                                                               Graph Coloring Time Limit: 1000MS   Memor ...

  3. UVA Graph Coloring

    主题如以下: Graph Coloring  You are to write a program that tries to find an optimal coloring for agiven ...

  4. Graph Coloring I(染色)

    Graph Coloring I https://www.nowcoder.com/acm/contest/203/J 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染 ...

  5. poj 1419 Graph Coloring

    http://poj.org/problem?id=1419 题意: 一张图黑白染色,相邻点不能都染黑色,最多能染几个黑色点 最大点独立集 但是图不能同构为二分图,不能用二分图匹配来做 那就爆搜吧 还 ...

  6. 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5775   Accepted: 2678   ...

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

        HamaWhite 原创,转载请注明出处!欢迎大家增加Giraph 技术交流群: 228591158     Graph coloring is the problem of assignin ...

  8. CF-1354 E. Graph Coloring(二分图,背包,背包方案输出)

    E. Graph Coloring 链接 n个点m条边的无向图,不保证联通,给每个点标号1,2,3.1号点个数n1,2号点个数n2,3号点个数n3.且每条边的两点,标号之差绝对值为1.如果有合法方案, ...

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

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

随机推荐

  1. OFBIZ文章

    0. OFBIZ Tutorials 1. A Tour of OFBiz 2. Entities and Services - Data Models and Business Logic in O ...

  2. 基于opencv的手写数字识别(MFC,HOG,SVM)

    参考了秋风细雨的文章:http://blog.csdn.net/candyforever/article/details/8564746 花了点时间编写出了程序,先看看效果吧. 识别效果大概都能正确. ...

  3. Windows下配置使用WinPcap

     0.前提 windows: win7 x64 WinPcap版本:4.1.3 WinPcap开发包:4.1.2 目标:在VS2010中配置使用winpcap 获取目标计算机中安装的网卡列表  1.下 ...

  4. CAT XQX --- 省市三级级联实现说明

    最终效果: 满足要求, 上代码 : 1.   需要调用这个控件 的地方:添加引用,因为里面写着逻辑呢..... <script type="text/javascript" ...

  5. css:outline

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  6. SandDock 应用示例

    直接上代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  7. 10个优质PSD文件资源下载

    很多设计需求并不一定要从头开始设计,你完全可以通过已有的灵感或素材开始设计任务.这个时候你可能需要一些PSD资源作为参考.今天我整理了一些常用的PSD资源供需要的朋友免费下载使用. Web & ...

  8. pthread_cond_wait 信号量丢失

    服务器在使用pthread_cond_wait的时候遇到一个问题.具体描述如下 一个主进程,给n个从线程发送计算请求,主进程会等待n个线程返回,在执行下一步 从线程计算完毕后,最后一个线程会通知主线程 ...

  9. SFTPTool 和 FTPTooL.java

    两个工具类依赖的jar包: FTPTool.java public static void main(String[] args) throws Exception{ FTPTooL ftpTool ...

  10. USB开发库文件分析

    stm32f10x_it.c: 该文件中包含 USB 中断服务程序,由于 USB 中断有很多情况,这里的中断服务程序只是调用 usb_Istr.c 文件中的 USB_Istr 函数,由 USB_Ist ...