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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for(int i=(a);i<(n);i++) #define per(i,a,n) for(int i=(n-1);i>=(a);i--) #define fi first #define se second typedef pair <int,int> pII; typedef long long ll; const int INF = 0x3f3f3f3f;
const int maxn = 5e3 + 10; int n,k,D,tot; const ll mod = 1e9 + 7; struct node{ int next,to; }G[maxn<<1]; int head[maxn],vis[maxn],cnt[maxn]; void add(int u,int v){ G[tot].to = v; G[tot].next = head[u]; head[u] = tot++; } ll ans = 1; ll pow3(ll a,ll b){ ll res = 1; while(b){ if(b&1) res = res*a%mod; a = a*a%mod; b>>=1; } return res; } ll bfs(int rt,int ds){ queue<pII> q; ll ans = 0; q.push(make_pair(rt,0));vis[rt] = rt; ans = cnt[rt]%mod; while(!q.empty()){ int u = q.front().fi; int d = q.front().se; q.pop(); if(d >= ds) break; cnt[u]--; for(int i = head[u];~i;i=G[i].next){ int v = G[i].to; if(vis[v]==rt) continue; vis[v] = rt; q.push(make_pair(v,d+1)); } } return ans; } inline int read() { int x=0; char c=getchar(); bool flag=0; while(c<'0'||c>'9'){if(c=='-')flag=1; c=getchar();} while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();} return flag?-x:x; } int main(){ #ifdef LOCAL freopen("j.in","r",stdin); #endif memset(head,-1,sizeof(head)); scanf("%d%d%d",&n,&k,&D); rep(i,0,n-1){ int u,v; u =read();v= read(); add(u,v); add(v,u); } ll ans1 = 1,ans2 = 1; rep(i,1,n+1) cnt[i] = k; queue<int>q; memset(vis,0,sizeof(vis)); q.push(1);int sign[maxn]={0};sign[1] = 1; while(!q.empty()){ int u = q.front();q.pop(); ans1 = ans1*bfs(u,D)%mod; for(int i= head[u];~i;i=G[i].next){ int v = G[i].to; if(sign[v]) continue; sign[v] = 1; q.push(v); } } rep(i,1,n+1) cnt[i] = k; memset(sign,0,sizeof(sign)); q.push(1); sign[1] =1; while(!q.empty()){ int u = q.front();q.pop(); ans2 = ans2*bfs(u,D+1)%mod; for(int i= head[u];~i;i=G[i].next){ int v = G[i].to; if(sign[v]) continue; sign[v] = 1; q.push(v); } } printf("%lld\n",(ans1-ans2+mod)%mod); return 0; }
|