使用迭代加深搜索,对剪断顶点个数为深度进行搜索,比用位运算暴力枚举全部子集要稍微快一点。
注意题意的理解,每个顶点一条小圆环,打开小圆环时,其与其他所有边都断开了。
题目要求为断开某些顶点后,任一顶点度数至多为2,图上没有环,断掉顶点个数必须大于等于除去断掉的顶点的图上联通块个数减一(使用断掉的顶点将所有联通块连成一条链)。
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 20 + 5;
int G[maxn][maxn], vis[maxn], n, maxd, open[maxn], cnt = 0;
bool dfs2(int p, int r){
for (int i = 1; i <= n; i++){
if (i == p || i == r) continue;
if (G[p][i] && !open[i]){
if (!vis[i]){
vis[i] = 1;
if (dfs2(i, p)) return true;
}
else
return true;
}
}
return false;
}
bool check(){
for (int i &#