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 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染 ...
随机推荐
- PowerShell工具脚本---按行数切割大文本文件
我编写的PowerShell工具脚本,[按行数切割大(文本)文件],生成n个小文件. 主要目的是为了能够让excel快速处理.或用脚本并发处理文本. 注意: 1 如果有必要,你可以先用其他工具,把大文 ...
- powershell玩转litedb数据库
powershell可以玩nosql数据库吗?答案是肯定的.只要这个数据库兼容.net,就可以很容易地被powershell使用. 发文初衷:世界上几乎没有讲powershell调用nosql的帖子, ...
- JAVA小记
关于重写equals()方法和重写toString()方法,一般来说,Objects的默认子类都重写了这两个方法,直接利用就行了: 对于用户自定义的类,如果要用到这两方法,就必须在程序中重写.
- 用腻了bootstrap的可以试试semantic-ui
semancti-ui介绍 semantic-ui是html/css框架的新贵,是继bootstrap和foundation之后的又一css神器.semantic-ui一出现在github上就受到火热 ...
- matlab图像剪裁命令imcrop()
调用格式: I2=imcrop(I,RECT): X2=imcrop(X,MAP,RECT): RGB2=imcrop(RGB,RECT): 其中,I.X.RGB分别对应灰度图像.索引图像.RGB图像 ...
- servlet filter可以用注解
现在好像可以在新建一个servlet.filter等的的时候在选项中设置urlmapping,通过注解的方式来监控action,以及设置初始参数initparameter.
- config、make、make install
.config/ .configure (查看该目录下是否有这个文件,如果有makefile,可直接make) 配置 config是一个shell脚本,根据平台的特性生成Makefile文件 ...
- jQuery 常用动画
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- sqlserver定时备份
前言:给客户部署好系统以后,如果不加一个定时备份,总感觉心里不放心,所以一定要做定时备份,并且定时备份是很简单的 新建作业--基本信息 新建步骤 ) ),) print @filename , NOF ...
- BZOJ 1093 最大半连通子图
缩点求最长链. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...