代码如下
#include<bits/stdc++.h>
using namespace std;
int n,m,a[301][301],b[301][301],x,y,fa[301],s[301],md,ch[301],en;
bool use[301];
bool pro(int x){
if (x==1) return 0;
if (use[x]) return 1;
return pro(fa[x]);
}
void bb(int i,int d){
s[d]++;
md=max(md,d);
b[d][s[d]]=i;
ch[i]=1;
for (int t=1;t<=a[i][0];t++){
if (fa[i]!=a[i][t]) {
fa[a[i][t]]=i;
bb(a[i][t],d+1);
ch[i]+=ch[a[i][t]];
}
}
}
void dfs(int dep,int res){
if (dep>md) return;
for (int i=1;i<=s[dep];i++){
if (pro(b[dep][i])) continue;
use[b[dep][i]]=1;
en=max(en,res+ch[b[dep][i]]);
dfs(dep+1,res+ch[b[dep][i]]);
use[b[dep][i]]=0;
}
}
int main(){
cin>>n>>m;
for (int i=1;i<=m;i++){
cin>>x>>y;
a[x][0]++;
a[x][a[x][0]]=y;
a[y][0]++;
a[y][a[y][0]]=x;
}
bb(1,1);
dfs(1,0);
cout<<2*n-en<<endl;
return 0;
}
有没有大佬解释一下这怎么AC的(/ω\)