树形DP
首先一个点要么按一次按钮要么不按
f[i][x][y]表示i的子树内除了i全部点亮,i节点是否亮,是否按过按钮,最少按的次数
code:
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1e8
using namespace std;
inline int read()
{
char c; int x;
while(!((c=getchar())>='0'&&c<='9'));
x=c-'0';
while((c=getchar())>='0'&&c<='9') (x*=10)+=c-'0';
return x;
}
inline void down(int &x,const int &y){if(x>y)x=y;}
const int maxn = 310;
const int maxm = 2100;
struct edge
{
int y,nex;
edge(){}
edge(const int &_y,const int &_nex){y=_y;nex=_nex;}
}a[maxn]; int len,fir[maxn];
inline void ins(const int &x,const int &y){a[++len]=edge(y,fir[x]);fir[x]=len;}
int n;
int f[maxn][2][2];
void dfs(const int x,const int fa)
{
f[x][0][0]=0; f[x][1][1]=1;
for(int k=fir[x];k;k=a[k].nex)
{
const int &y=a[k].y;
if(y!=fa)
{
dfs(y,x);
int t00=min(f[x][0][0]+f[y][1][0],f[x][1][0]+f[y][1][1]);
int t01=inf;
int t10=min(f[x][0][0]+f[y][1][1],f[x][1][0]+f[y][1][0]);
int t11=f[x][1][1]+f[y][0][0];
down(t00,inf); down(t10,inf); down(t11,inf);
f[x][0][0]=t00; f[x][0][1]=t01; f[x][1][0]=t10; f[x][1][1]=t11;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF&&n)
{
memset(fir,0,sizeof fir); len=0;
for(int i=1;i<n;i++)
{
int x=read(),y=read();
ins(x,y); ins(y,x);
}
memset(f,63,sizeof f);
dfs(1,0);
printf("%d\n",min(f[1][1][0],f[1][1][1]));
}
return 0;
}