【Codevs 4672】辛苦的老园丁

http://codevs.cn/problem/4672/

那个一看这不是(最大独立集)的最大权值和,类似

反图→ 最大团  NP问题 搜索解决

改一下模板即可

参考最大独立集  Maximum Clique最大团问题

// <4672.cpp> - Sun Oct  9 12:58:23 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
#define IN inline
#define RG register
using namespace std;
typedef long long LL;
typedef long double LB;
const int MAXN=100010;
const int MAXM=100010;
inline int max(int &x,int &y) {return x>y?x:y;}
inline int min(int &x,int &y) {return x<y?x:y;}
inline LL gi() {
	register LL w=0,q=0;register char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')q=1,ch=getchar();
	while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
	return q?-w:w;
}
struct MAX_CLIQUE{
    static const int N=26;
    bool G[N][N];
    int n,Max[N],Alt[N][N],ans,a[N];
    void DFS(int cur,int x,int tot){
        if(!cur){
            if(tot>ans)ans=tot;
            return;
        }
        for(int i=1;i<=cur;i++){
            int u=Alt[x][i],nxt=0;
            if(Max[u]+tot<=ans)return;
            for(int j=i+1;j<=cur;j++)
                if(G[u][Alt[x][j]])Alt[x+1][++nxt]=Alt[x][j];
            DFS(nxt,x+1,tot+a[u]);
        }
    }
    int MaxClique(){
        ans=0,memset(Max,0,sizeof(Max));
        for(int i=n;i;i--){
            int cur=0;
            for(int j=i+1;j<=n;j++)
                if(G[i][j])Alt[1][++cur]=j;
            DFS(cur,1,a[i]);
            Max[i]=ans;
        }
        return ans;
    }
    void Work(){
        n=gi();int m=gi();
        for(int i=1;i<=n;i++)a[i]=gi();
        for(int i=1;i<=m;i++){
            int u=gi(),v=gi();
            G[u][v]=G[v][u]=1;
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                G[i][j]^=1;
        printf("%d",MaxClique());
    }
}Group;
int main()
{
	freopen("4672.in","r",stdin);
	freopen("4672.out","w",stdout);
	Group.Work();
	return 0;
}

  

posted @ 2016-10-18 16:39  _Mashiro  阅读(259)  评论(0编辑  收藏  举报