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 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

题意:给出你一个无向图,然后对其中的点去上色, 只能上黑色和白色,要求是黑色点不能相邻(白色可以相邻),问最多能上多少黑色的顶点。

题解:就是求图的最大独立集,重点是输出路径,所以套模板,如果不用输出路径,那么可以套公式:最大独立集=顶点数-最大匹配数,匈牙利算法求最大匹配数即可。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<algorithm>
 6 using namespace std;
 7 int vis[110];
 8 int n,k,m;
 9 int ans,res[110];
10 vector<int>v[110];
11 int maxx;
12 void dfs(int pos,int cnt)
13 {
14     if(pos==n+1)
15     {
16         if(cnt>maxx)
17         {
18             int t=0;//一定要注意啊啊!!
19             for(int i=1;i<=n;i++)
20             {
21                 if(vis[i])
22                     res[t++]=i;
23             }
24             maxx=cnt;
25         }
26         return;
27     }
28     int flag=0;
29     for(int i=0;i<v[pos].size();i++)
30     {
31         if(vis[v[pos][i]])
32         {
33             flag=1;
34             break;
35         }
36     }
37     if(flag==0)
38     {
39         vis[pos]=1;
40         dfs(pos+1,cnt+1);
41         vis[pos]=0;
42     }
43     dfs(pos+1,cnt);
44     return;
45 }
46 void init()
47 {
48     for(int i=0;i<110;i++)
49         v[i].clear();
50     memset(res,0,sizeof(res));
51     memset(vis,0,sizeof(vis));
52 }
53 int main()
54 {
55     int x,y;
56     scanf("%d",&m);
57     while(m--)
58     {
59         init();
60         scanf("%d%d",&n,&k);
61         for(int i=0;i<k;i++)
62         {
63             scanf("%d%d",&x,&y);
64             v[x].push_back(y);
65             v[y].push_back(x);
66         }
67         maxx=-1;
68         dfs(1,0);
69         printf("%d\n",maxx);
70         for(int i=0;i<maxx-1;i++)
71         {
72             printf("%d ",res[i]);
73         }
74         printf("%d\n",res[maxx-1]);
75     }
76
77     return 0;
78 }

原文地址:https://www.cnblogs.com/1013star/p/9795322.html

时间: 2024-10-13 05:25:46

Graph Coloring(最大独立集模板题)的相关文章

poj1419 Graph Coloring 最大独立集(最大团)

最大独立集: 顶点集V中取 K个顶点,其两两间无连接. 最大团: 顶点集V中取 K个顶点,其两两间有边连接. 最大独立集=补图的最大团最大团=补图的最大独立集 #include<iostream> #include<cstring> #include<cstdio> using namespace std; int mp[110][110],mark1[505],mark2[505]; int n,m; int cnt,maxx; void dfs(int x) { i

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

【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_最大独立集

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

hdu 3549 Flow Problem(最大流模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input

[ACM] hdu 3549 Flow Problem (最大流模板题)

Flow Problem Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input contains an integer T, denoting the nu

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

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 maxim

hdu 2966 In case of failure kdtree模板题

问求每个点距离平方的最小的点 kd-tree模板题…… 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<(x)<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int>