1. 程式人生 > >Graph Coloring(最大獨立集模板題)

Graph Coloring(最大獨立集模板題)

匈牙利 ins color mem ace rec ram 算法 rst

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 }



Graph Coloring(最大獨立集模板題)