显示代码纯文本
#include <cstdio>
using namespace std;
int n,way[50001]={0},map[50001][51]={{0}},f[50001][2]={{0}};
bool una[50001][51]={{false}};
int findmax(int a,int b)
{
if (a>b)
return(a);
else
return(b);
}
void work(int pos)
{
int i,j,temp=0,temp2=0;
for (i=0;i<way[pos];i++)
if (!una[pos][i])
{
for (j=0;j<way[map[pos][i]];j++)
if (map[map[pos][i]][j]==pos)
{
una[map[pos][i]][j]=true;
break;
}
work(map[pos][i]);
temp+=findmax(f[map[pos][i]][1],f[map[pos][i]][0]);
temp2+=f[map[pos][i]][0];
}
f[pos][0]=temp;
f[pos][1]=temp2+1;
}
int main(void)
{
freopen("vacation.in","r",stdin);
freopen("vacation.out","w",stdout);
int i,temp,temp2;
scanf("%d\n",&n);
for (i=1;i<n;i++)
{
scanf("%d %d\n",&temp,&temp2);
map[temp][way[temp]++]=temp2;
map[temp2][way[temp2]++]=temp;
}
work(1);
if (f[1][0]>f[1][1])
printf("%d\n",f[1][0]);
else
printf("%d\n",f[1][1]);
fclose(stdin);
fclose(stdout);
return(0);
}