Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 6036 | Accepted: 2781 | 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
Source
Southwestern European Regional Contest 1995
题目大意:给你N个点K条边,问你类似于题干上的图那样岔开染色的结果中,我们现在要求出那个黑色节点最多的那个情况,并且要输出那个节点是黑色的
思路:其实这个题目的话,就有点暴力了,因为就100吧,可能胆子大了许多。。。
就是拿出一个点作为起始节点,然后通过给这个点染色,我们就可以得到整个图的颜色,其中也包括我们需要的黑色节点的个数,我们作比较,然后输出就可以了,
具体的看一下代码吧,
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#define black 0
#define white 1
using namespace std;
const int maxn=100+10;
int G[maxn][maxn];
int color[maxn];
int a[maxn];
int n,m;
void init(int n)
{
memset(color,black,sizeof(color));
memset(a,0,sizeof(a));
memset(G,0,sizeof(G));
}
int write(int u)
{
int count=0;
for(int i=u; i <= n+u-1; i++)//不要每次都在1节点开始
{
int pos = i%n;
if(!pos) pos=n;
if(color[pos]==black)
{
count++;
for(int v=1; v <= n; v++ )//染色并计数过程
if(G[pos][v])
color[v]=white;
}
}
return count;
}
void solve()
{
int ans=0;
for(int i=1;i<=n;i++)
{
memset(color,black,sizeof(color)); //假设他一开始都是黑色的
int k=write(i);
if(k>ans)
{
ans=k;
int cnt=0;
for(int j=1;j<=n;j++) //保存可行结果
if(color[j]==black)
a[cnt++]=j;
}
}
printf("%d\n",ans);
for(int i=0;i<ans;i++)
cout<<a[i]<<" ";
cout<<endl;
}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init(n);
int u,v;
while(m--)
{
scanf("%d%d",&u,&v);
G[u][v]=G[v][u]=1;
}
solve();
}
}