Cattle off-line programming _ trees

Title address
to a total of n m planting trees, constructed embodiment requires a minimum of two adjacent trees of different kinds lexicographically.

  • Search, and then if the number of occurrences of a certain tree, more than half the number of positions has not put on pruning.

code

#include <bits/stdc++.h>
using namespace std;
const int N=1e3+50;
int n,m,c[N];
bool flag;
vector<int> ans;
void dfs(int idx){
    if(idx==m){
        flag=true;
        return;
    }
    //剪枝
    for(int i=1;i<=n;i++){
        if(c[i]>(m-idx+1)/2){
            return;
        }
    }
    for(int i=1;i<=n;i++){
        if(c[i] && (idx==0 || ans[idx-1]!=i)){
            c[i]--;
            ans.push_back(i);
            dfs(idx+1);
            if(flag){
                return;
            }
            ans.pop_back();
            c[i]++;
        }
    }
}
int main(){
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    int mx=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&c[i]);
        m+=c[i];
        mx=max(mx,c[i]);
    }
    if((m%2 && mx>(m-mx+1)) ||(m%2==0 && mx>(m-mx))){
        printf("-\n");
        return 0;
    }
    //construct
    flag=false;
    dfs(0);
    if(flag){
        for(int i=0;i<m;i++){
            printf("%d%c",ans[i],i==m-1?'\n':' ');
        }
    }else{
        printf("-\n");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/zxcoder/p/12229864.html