(最大团) poj 1419

Graph Coloring

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4370   Accepted: 1980   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 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 1: An optimal graph with three black nodes

Input

The 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

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<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int n,mp[55][55],vis[55],cnt,bestn;
void dfs(int x)
{
    if(x>n)
    {
        if(cnt>bestn)
            bestn=cnt;
        return ;
    }
    bool ok=true;
    for(int i=1;i<x;i++)
    {
        if(vis[i]&&!mp[i][x])
        {
            ok=false;
            break;
        }
    }
    if(ok)
    {
        vis[x]=1;
        cnt++;
        dfs(x+1);
        cnt--;
    }
    if(cnt+n-x>bestn)
        vis[x]=0,dfs(x+1);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        cnt=bestn=0;
        memset(vis,0,sizeof(vis));
        memset(mp,0,sizeof(mp));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                scanf("%d",&mp[i][j]);
        }
        dfs(1);
        printf("%d\n",bestn);
    }
    return 0;
}

  

时间: 2024-08-07 22:00:28

(最大团) poj 1419的相关文章

poj 1419 Graph Coloring_最大独立集

题目链接 题意:给出你一个无向图,然后对其中的点去上色, 只能上黑色和白色,要求是黑色点不能相邻,问最多能上多少黑色的顶点. 思路:点独立集:设无向图G=<V,E>,顶点集合V'是V的子集,若V'中的任意两个顶点都不相邻,则称V'为G的点独立集 这题求的是最大独立集 还有一个定理是最大独立集=补图的最大团 最大团=补图的最大独立集 #include<stdio.h> #include<string.h> #define MAXN 100 int n,map[MAXN][

【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

poj 1419 (最大独立集)

基础知识: 最大独立集: 顶点集V中取 K个顶点,其两两间无连接. 最大团: 顶点集V中取 K个顶点,其两两间有边连接. 最大独立集 = 补图的最大团. (补图 = 完全图 - 原图) 挺详细的解释:http://www.cnblogs.com/yefeng1627/archive/2013/03/31/2991592.html 思路: 据说..求原图的最大独立集是NP问题,那么就可以通过求其补图中最大团中顶点数量,就可得出原图中最大独立集中顶点数量了. 附上优化后的模板: 1 #include

图论常用算法之一 POJ图论题集【转载】

POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:http://poj.org/ 1062* 昂贵的聘礼 枚举等级限制+dijkstra 1087* A Plug for UNIX 2分匹配 1094 Sorting It All Out floyd 或 拓扑 1112* Team Them Up! 2分图染色+DP 1125 Stockbroker

[转] 一些图论、网络流入门题总结、汇总

最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意:经典问题:K短路解法:dijkstra+A*(rec),方法很多相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础)http://ac

【转】一些图论、网络流入门题总结、汇总

最短路问题 此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等) http://acm.pku.edu.cn/JudgeOnline/problem?id=2449 题意:经典问题:K短路 解法:dijkstra+A*(rec),方法很多 相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144 该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础) ht

[转载] 一些图论、网络流入门题总结、汇总

转载 最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意:经典问题:K短路解法:dijkstra+A*(rec),方法很多相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础)http:/

图论题目总结(二)(提高版,转载)

如果,我已经做过的题(红色标记),基本都会在本博客写出解体报告.可以自行查找~ 最短路问题 此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等) http://acm.pku.edu.cn/JudgeOnline/problem?id=2449 题意:经典问题:K短路 解法:dijkstra+A*(rec),方法很多 相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144 该题亦放在搜索推