[nowcoder3]补题向

J.Coloring Tree

题意

给一棵树,每个节点从$[1,k]$选择一种颜色染。定义树的颜色值为两个相同颜色节点之间的最小值。问如果一棵树的颜色值为$D$,那么它有多少种染色方式。

题解

Reference:https://www.nowcoder.com/discuss/88556?type=101&order=0&pos=1&page=1

本地能过就是能过

谁能知道我为啥超时10%有重赏

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;
//head
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;
}

真丶AC代码

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
#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;
//head
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],dis[maxn][maxn];
void add(int u,int v){
G[tot].to = v;
G[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u,int fa,int rt){
for(int i=head[u];~i;i=G[i].next){
int v = G[i].to;
if(v == fa) continue;
dis[v][rt] = dis[u][rt]+1;
dfs(v,u,rt);
}
}
ll solve(int d){
queue<int>q;
rep(i,1,n+1) cnt[i] = k;
ll ans = 1;
q.push(1);
memset(vis,0,sizeof(vis));vis[1] = 1;
while(!q.empty()){
int u = q.front();q.pop();
ll res = 0;
rep(v,1,n+1) if(v!=u) cnt[v] -= (dis[u][v]<d);
if(cnt[u] <= 0) return 0;
ans = ans*cnt[u]%mod;
for(int i=head[u];~i;i=G[i].next){
int v = G[i].to;
if(dis[1][u]+1 == dis[1][v]){
q.push(v);
}
}
}
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);
}
rep(i,1,n+1) dfs(i,-1,i);
printf("%lld\n",(solve(D)-solve(D+1)+mod)%mod);
return 0;
}