Problem1509--卫队

1509: 卫队

Time Limit: 2.000 Sec  Memory Limit: 128 MB
Submit: 496  Solved: 169
[Submit] [Status] [Web Board] [Creator:]

Description



【问题描述】
原始部落byteland中的居民们为了争夺有限的资源,经常发生冲突。几乎每个居民都有他的仇敌。部 落酋长为了组织一支保卫部落的队伍,希望从部落的居民中选出最多的居民入伍,并保证队伍中任何2 个 人都不是仇敌。 
【编程任务】 给定byteland部落中居民间的仇敌关系,编程计算组成部落卫队的最佳方案。 
【输入格式】
第一行2个正整数n和m,表示n个人,m个仇敌关系,接下来m行,每行2个正整数u和v,表示u和v是仇敌(人编号1..n)
【输出格式】
第一行是最多的人数,第二行是队伍的组成Xi,0表示第i个人不入伍,1表示入伍
【问题规模】 
1<=n<=100,   0<=m<=5000


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

HINT

#include<bits/stdc++.h>
using namespace std;
int n,ans[105],b[5005][5005],da[105],m,cow=-1234,sum,u,v;
void dfs(int x);
int main(){
    cin>>n>>m;
    for(int j=1;j<=m;j++){
        cin>>u>>v;
        b[u][v]=b[v][u]=1;  
    }
    dfs(1);
    cout<<cow<<endl;
    for(int i=1;i<=n;i++){
        cout<<da[i]<<" ";
    }
}
void dfs(int x){
    if(x>n){
        if(sum>cow){
            cow=sum;
            for(int i=1;i<=n;i++){
                da[i]=ans[i];
            }
        }
        return;
    }
    int bj=1;
    for(int i=1;i<=x;i++){
        if(b[x][i]==1&&ans[i]==1){
            bj=0;
            break;
        }
    }
    if(bj==1){
        ans[x]=1;
        sum++;
        dfs(x+1);
        sum--;
        ans[x]=0;
    }
    dfs(x+1);
}


Source/Category

 

[Submit] [Status]