标签:
传说中,有这样一个部落.......
原始部落byteland中的居民们为了争夺有限的资源,经常发生冲突,几乎每个居民都有仇敌。村落酋长为了组织一支保卫部落的队伍,希望从部落的居民中选出至多的居民入伍,并保证队伍中的任何2个人都不是仇敌。
给定byteland部落中居民之间的仇敌关系,编程计算组成部落卫队的最佳方案。
第一行有2个正整数n和m,表示byteland部落中有n个居民,居民间有m个仇敌关系。接下来的m行中,每行有2个正整数u和v,表示居民u与居民v是仇敌。
(居民编号按输入顺序为1,2...n)
第一行是部落卫队的最多人数。第二行是卫队组成xi,1<=i<=n,xi=0表示居民i不在卫队中,xi=1表示居民i在卫队中。
(有多组解时先选编号小的入伍)
只有1s
40%的n满足n<=100,100%的n满足n<=120
100%的m满足m<=5000
简单的搜索,多剪剪枝,期待大牛们的秒杀~
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
using namespace std;
#define INF 1<<30
int n,m,a[110][110],temp[110],ans[110],step,best;
void dfs(int x)
{
if(x==n)
{
if(step>best)
{
best=step;
memcpy(ans,temp,sizeof(int)*n);
}
return ;
}
bool flag=true;
for(int i=0;i<x;i++)
{
if(temp[i]==1&&a[i][x]==0)
{
flag=false;
break;
}
}
if(flag)
{
step++;
temp[x]=1;
dfs(x+1);
step--;
temp[x]=0;
}
if(step+n-x-1>best)
{
temp[x]=0;
dfs(x+1);
}
}
int main()
{
int x,y;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]=1;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
a[x-1][y-1]=0;
a[y-1][x-1]=0;
}
dfs(0);
printf("%d\n",best);
for(int i=0;i<n-1;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n-1]);
return 0;
}
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4185949.html