pku 1419 Graph Coloring
地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=1419
题目大意:
就是给你n个点,m个边的无向图,要求把点进行涂黑,只不过涂黑时有一个要求,就是一个点如果被涂成黑色的话,与之相邻的点就只能是白色的,问在这种策略下,最多能把几个点涂黑。
具体分析:
坑待填。
AC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define N 128
int Map[N][N];
int vis[N],ans[N] , path[N];
int n ,m, maxx, cnt;
int check(int i)
{
for(int j = 1 ;j <= n ;j++)
if(j!=i&&Map[i][j]&&vis[j])
return 0;
return 1;
}
void DFS(int index)
{
if( index > n)
return ;
if(cnt > maxx)
{
maxx = cnt;
for(int j = 0 ; j < cnt; j++)
ans[j] = path[j];
}
for(int i = index; i <= n; i++)
{
if(!vis[i]&&check(i))
{
vis[i] =1;
path[cnt++]=i;
DFS(i+1);
cnt--;
vis[i] = 0;
}
}
}
int main()
{
//cout << N << endl;
int t, a, b;
while(cin >> t )
{
while(t--)
{
memset(Map,0,sizeof(Map));
memset(vis,0,sizeof(vis));
maxx = -1;
cnt = 0 ;
cin >> n >> m;
for(int i = 1 ; i <=m ;i++)
{
cin >> a >> b;
Map[a][b] = Map[b][a] = 1;
}
DFS(1);
cout << maxx << endl;
for(int i = 0 ;i < maxx ;i++)
cout << ans[i] << " ";
cout << endl << endl;
}
}
return 0;
}