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 }



posted @ 2018-10-15 23:01  *starry*  阅读(503)  评论(0编辑  收藏  举报