显示代码纯文本
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int n,a[3001],a1,a2,su1,su2,dep[3001],ans;
vector<int>b[3001];
void Work(int x,int fa,int cnt)
{
dep[x]=cnt;
for(int i=0;i<b[x].size();i++)
{
int to=b[x][i];
if(to==fa)
continue;
Work(to,x,cnt+1);
}
return;
}
int main()
{
freopen("usaco_Feb_clocktree.in","r",stdin);
freopen("usaco_Feb_clocktree.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n-1;i++)
{
scanf("%d%d",&a1,&a2);
b[a1].push_back(a2);
b[a2].push_back(a1);
}
Work(1,0,0);
int jl1=0;
int jl2=0;
for(int i=1;i<=n;i++)
{
if(dep[i]%2)
{
su1+=a[i];
jl1++;
}
else
{
su2+=a[i];
jl2++;
}
}
if(su1%12==su2%12)
ans=n;
if((su1+1)%12==su2%12)
ans=jl2;
if((su1+11)%12==su2%12)
ans=jl1;
printf("%d\n",ans);
return 0;
}