0
已解决
1052 部落卫队
题目描述 Description
原始部落byteland中的居民们为了争夺有限的资源,经常发生冲突。几乎每个居民都有他的仇敌。部落酋长为了组织一支保卫部落的队伍,希望从部落的居民中选出最多的居民入伍,并保证队伍中任何2 个人都不是仇敌。
给定byteland部落中居民间的仇敌关系,编程计算组成部落卫队的最佳方案。
输入描述 Input Description
第1行有2个正整数n和m,表示byteland部落中有n个居民,居民间有m个仇敌关系。居民编号为1,2,…,n。接下来的m行中,每行有2个正整数u和v,表示居民u与居民v是仇敌。
输出描述 Output Description
第1行是部落卫队的人数;文件的第2行是卫队组成xi,1≤i≤n,xi =0 表示居民i不在卫队中,xi=1表示居民i在卫队中。输出字典序最大的解。
样例输入 Sample Input
7 10 1 2 1 4 2 4 2 3 2 5 2 6 3 5 3 6 4 5 5 6
样例输出 Sample Output
3 1 0 1 0 0 0 1
数据范围及提示 Data Size & Hint
n <= 100
#include <iostream>
using namespace std;
int n,m,chou[105][105],out[105],put[105],maxn,last,cnt[105];
int x1,x2;
void output(){
cout<<last<<endl;
for(int i=1;i<=n;i++)
cout<<put[i]<<" ";
}
void dfs(int k){
for(int i=k+1;i<=n;i++){//尝试
bool can=true;
for(int j=1;j<=k;j++){//检验i是否可以加入
bool can1=true;
for(int k=1;k<=cnt[i];k++){
if(chou[i][k]==j){
can=false; can1=false;
break;
}
}
if(!can1) break;
}
if(!can) continue;
out[i]=1;
maxn++;
dfs(i);//深搜
maxn--;
out[i]=0;//回溯
}
if(maxn>last){
last=maxn;
for(int i=1;i<=n;i++)
put[i]=out[i];
return;
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>x1>>x2;
chou[x1][++cnt[x1]]=x2;
chou[x2][++cnt[x2]]=x1;
}
dfs(0);
output();
return 0;
}
75枚酷町豆
周琪岳在2020-12-22 19:59:47追加了内容
顶
0
已采纳
搜索部分参考一下
void seach(int x,int tot){
if(x>n){
if(tot>send){
send=tot;
memcpy(end,use,n+1);
}
return ;
}
if(!can[x] && !use[x]){
use[x]=true;
bool now[105];
memcpy(now,can,n+1);
for(int j=0;j<vec[x].s;j++){
can[vec[x].other[j]]=true;
}
seach(x+1,tot+1);
use[x]=false;
memcpy(can,now,n+1);
}
seach(x+1,tot);
}
定义
struct vellager{
int other[105],s;
}vec[105];
int m,n,send;
bool can[105],use[105],end[105];
主函数中
seach(1,0);
cout<<send;
0
0
