1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include<bits/stdc++.h> #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,l,r) for(int i=l;i>=r;i--) #define link(x) for(edge*j=h[x];j;j=j->next) #define ll long long #define sqr(x) ((x)*(x)) #define succ(x) (1<<x) #define lowbit(x) (x&(-x)) #define mem(a) memset(a,0,sizeof(a)) #define NM 10005 #define nm 505 using namespace std; const int inf=1e6; ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return x*f; }
struct edge{int t;edge*next;}e[2*NM],*h[NM],*o=e; void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;} int n,c,_x,_y,dep[NM],ca; ll d[NM][nm],ans,g[NM][nm];
void dfs(int x,int f){ dep[x]=0; inc(i,0,c-1)d[x][i]=0; d[x][c]=1; link(x)if(j->t!=f){ dfs(j->t,x); inc(k,1,min(c-1,dep[x])){ inc(v,max(0,c-1-k),min(c-1,dep[j->t])) (g[x][min(k,v+1)]+=d[x][k]*d[j->t][v]%inf)%=inf; (g[x][min(k,c)]+=d[x][k]*d[j->t][c]%inf)%=inf; } inc(v,0,min(c-1,dep[j->t]))(g[x][min(c,v+1)]+=d[x][c]*d[j->t][v]%inf)%=inf; (g[x][c]+=d[x][c]*d[j->t][c]%inf)%=inf;
inc(k,0,c)d[x][k]=g[x][k],g[x][k]=0; dep[x]=max(dep[x],dep[j->t]+1); } d[x][0]=1; link(x)if(j->t!=f){ d[x][0]*=(d[j->t][c]+d[j->t][c-1]);d[x][0]%=inf; } }
int main(){ while(~scanf("%d%d",&n,&c)&&n&&c){ o=e;ans=0; inc(i,1,n)h[i]=0; inc(i,1,n-1){_x=read();_y=read();add(_x,_y);add(_y,_x);} dfs(1,0); inc(i,0,c)ans+=d[1][i],ans%=inf; printf("Case #%d: %lld\n",++ca,ans); } return 0; }
|