AcWing 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;
}