POJ1419 Graph Coloring(最大独立集)(最大团)
| Time Limit: 1000MS | Memory Limit: 10000K | |||
| Total Submissions: 4926 | Accepted: 2289 | Special Judge | ||
Description

Figure 1: An optimal graph with three black nodes
Input
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
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 <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#define inf 0x7fffffff
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
bool w[N][N];
bool use[N]; //进入团的标号
bool bestx[N];
int cn,bestn,p,e; void dfs(int x) {
bool flag;
if(x>p) {
bestn=cn; //cn的值是递增的
for( int i=;i<=p; i++) //赋值给另外一个数组,
bestx[i]=use[i];
return ;
}
flag=true;
for( int i=; i<x; i++)
if(use[i]&&!w[i][x]) {
flag=false;
break;
}
if(flag) {
cn++;
use[x]=true;
dfs(x+);
cn--;
use[x]=false;//回溯
}
if(cn+p-x>bestn) { //剪枝
dfs(x+);
}
} int main() {
int num,u,v;
scanf("%d",&num);
while(num--) {
memset(w,true,sizeof(w));
memset(use,false,sizeof(use));
memset(bestx,false,sizeof(bestx));
scanf("%d%d",&p,&e);
for(int i=; i<e; i++) {
scanf("%d%d",&u,&v);
w[u][v]=false;
w[v][u]=false;
}
cn=bestn=;
dfs();
printf("%d\n",bestn);
for (int i=; i<=p; i++)if(bestx[i])printf("%d ",i);printf("\n");
}
return ;
}
POJ1419 Graph Coloring(最大独立集)(最大团)的更多相关文章
- poj1419 Graph Coloring 最大独立集(最大团)
最大独立集: 顶点集V中取 K个顶点,其两两间无连接. 最大团: 顶点集V中取 K个顶点,其两两间有边连接. 最大独立集=补图的最大团最大团=补图的最大独立集 #include<iostream ...
- POJ1419 Graph Coloring
嘟嘟嘟 求无向图的最大独立集. 有这么一回事:最大独立集=补图的最大团. 所谓的最大团,就是一个子图,满足图中任意两点都有边. 然后ssy巨佬告诉了我一个很没有道理强的做法:随机. 每一次random ...
- POJ 1419 Graph Coloring(最大独立集/补图的最大团)
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4893 Accepted: 2271 ...
- 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5775 Accepted: 2678 ...
- uva193 - Graph Coloring
Graph Coloring You are to write a program that tries to find an optimal coloring for a given graph. ...
- poj 1419 Graph Coloring
http://poj.org/problem?id=1419 题意: 一张图黑白染色,相邻点不能都染黑色,最多能染几个黑色点 最大点独立集 但是图不能同构为二分图,不能用二分图匹配来做 那就爆搜吧 还 ...
- GPS-Graph Processing System Graph Coloring算法分析 (三)
HamaWhite 原创,转载请注明出处!欢迎大家增加Giraph 技术交流群: 228591158 Graph coloring is the problem of assignin ...
- UVA Graph Coloring
主题如以下: Graph Coloring You are to write a program that tries to find an optimal coloring for agiven ...
- Graph Coloring I(染色)
Graph Coloring I https://www.nowcoder.com/acm/contest/203/J 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染 ...
随机推荐
- Program L 暴力求解
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- ScrollView中嵌入ListView,GridView冲突的解决(让ListView全显示出来)
ScrollView中嵌入原生ListView或GridView,会出现ListView,GridView显示不全的问题. 解决方法:重新构造一个ListView或GridView,重写OnMeasu ...
- ubuntu 添加源
edit file : /etc/apt/sources.list add: deb http://mirrors.163.com/ubuntu/ intrepid main restricted ...
- Android 导入jar包 so模块--导入放置的目录
Android视图下 app目录下的jniLibs 对应Project 视图app下的libs目录 把jar包或者带so文件的文件夹(一般以处理器型号命名如:arbeabi arm64-v8a)复制 ...
- linux安装时出现your cpu does not support long mode的解决方法
如果你确定你的电脑支持64bit且是64bit的宿主系统,则需要修改BIOS中的Inter Virtualization Technology为enabled.
- RelativeLayout相对布局中拖放控件的办法
相对布局中拖了一个控件以后,要拖放第二个空间,死活拖不进去.仔细查看了鼠标的状况,发现要把第二个控件拖到第一个控件的周围,才能成功.果然是相对布局.
- 2014年3月份第1周51Aspx源码发布详情
Graphics创建饼图示例源码 2014-3-7 [VS2010]源码描述:这个程序是一个在c#中使用图形类用来创建饼图,此程序是用Graphics 类的DrawPie() 和 FillPie() ...
- Android布局— — —帧布局
帧布局,开发中很少使用,最简单的布局 添加多个控件,这些控件会按顺序在屏幕左上角重叠显示,且会透明显示之前控件的文本语法格式:<?xml version="1.0" enco ...
- Ubuntu 14.10 下网络流量实时监控ifstat iftop命令详解
ifstat 介绍 ifstat工具是个网络接口监测工具,比较简单看网络流量 实例 默认使用 #ifstat eth0 eth1 KB/s in KB/s out KB/s in KB/s out 0 ...
- log4j之NDC、MDC
NDC 介绍 NDC(Nested Diagnostic Context)是 Neil Harrison 在名为<Patterns for Logging Diagnostic Message ...