AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

AcWing 1. 最大团分支限界法求解(选与不选思想)    原题链接    简单

作者: 作者的头像   acwing_81750 ,  2025-06-06 14:42:39 · 山东 ,  所有人可见 ,  阅读 3


0


1
//最大团问题(分支限界法)
//与01背包问题不同
//它成图,每次迭代会有多种可能而非一种,所以要使用for循环考虑所有的可能性
#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

const int N=20;
int n,m;
int edge[N][N];
vector<int>bestpath;

struct Node{
    vector<int>sele;
    int lastindex;
    int bound;
    bool operator<(const Node &W)const{
        return bound<W.bound;
    }
};
priority_queue<Node>q;

int canadd(int x,vector<int>t){
    for(auto u:t){
        if(!edge[x][u])return 0;
    }
    return 1;
}
void bfs(){
    q.push({{},0,n});
    while(q.size()){
        auto t=q.top();
        q.pop();
        if(t.bound<=bestpath.size())continue;
        if(t.sele.size()>bestpath.size()){
            bestpath=t.sele;
        }
//        for(int v=t.lastindex+1;v<=n;v++){
//            if(!canadd(v,t.sele))continue;
//            Node newnode;
//            newnode.sele=t.sele;
//            newnode.sele.push_back(v);
//            newnode.lastindex=v;
//            newnode.bound=newnode.sele.size()+n-v;
//            if(newnode.bound>bestpath.size()){
//            q.push(newnode);
//        }
//        }

        for(int v=t.lastindex+1;v<=n;v++){
            //xuan
        if(canadd(v,t.sele)){
            Node left=t;
            left.sele.push_back(v);
            left.lastindex=v;
            left.bound=left.sele.size()+n-v;
            if(left.bound>bestpath.size()){
                q.push(left);
            }
        }

        //buxuan
        Node right=t;
        right.lastindex=v;
        right.bound=t.sele.size()+n-v;
        if(right.bound>bestpath.size()){
            q.push(right);
        }

        }
    }
}
signed main() {
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        edge[x][y]=edge[y][x]=1;
    }
    bfs();
    cout<<bestpath.size()<<endl;
    // for(auto t:bestpath){
    //     cout<<t<<" ";

    // }
    // cout<<endl;
    return 0;
}

0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息