之前两次遇到过函数的思想的题,所以这次很敏感就看出来了。可以参考之前的题:

https://www.cnblogs.com/hua-dong/p/9291507.html

Christmas is coming! Eddy has received a Christmas tree as gift. Not surprisingly, the tree consists of N vertices and N-1 edges and magically remains connected. Currently, all the vertex of the tree is uncolored. Eddy wants to color each vertex into one of K colors. However, there are too many way to color the tree(i.e. KN ways). Eddy doesn't want the result of coloring being too boring. Thus, he defines the colorness of a tree as follow:

The colorness of a tree is the minimum distance between two vertex colored in the same color.

Now, Eddy is wondering how many way to color the tree such that the colorness of the tree will be D.

The first line of input contains three space-separated integer N, K, D indicating the number of vertices, number of colors, and the required colorness.
For each following N-1 lines, each contains two space-separated positive integer ui, vi indicating that there's an edge between ui and vi.

1 ≤ K < N ≤ 5000
1 ≤ D ≤ N
1 ≤ ui < vi ≤ N
It's guaranteed that the given input is a tree.

题意:用K种颜色给树染色,问有多少种染色方案,使得min{同色间距离}=D。

思路:一眼题,但是当时没时间了,4点就出门了。晚上一会就补了,emmm。

还是函数的思想,令F(X)表示距离在X内的点都不同色。那么只要在搜索的同时(BFS或者DFS都可以)染色即可。

如果对S点染色,在X范围内的有Y个点染色了,那么S点的染色种类数为K-Y,回去搜索一下已经染过色的有多少就行了,复杂度为O(N^2)。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int Mod=1e9+,maxn=;
int Next[maxn],Laxt[maxn],To[maxn],cnt;
int K,q[maxn],col[maxn],head,tail,ans;
void add(int u,int v){ Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; }
int dfs(int u,int f,int dis){
int res=;
for(int i=Laxt[u];i;i=Next[i])
if(To[i]!=f&&col[To[i]]&&dis>) res+=dfs(To[i],u,dis-);
return res;
}
int cal(int D)
{
int res=; memset(col,,sizeof(col)); head=tail=;
q[++head]=;
while(tail<head){
int u=q[++tail]; col[u]=;
int num=dfs(u,,D);
res=(ll)res*(K+-num)%Mod;
for(int i=Laxt[u];i;i=Next[i]) if(!col[To[i]]) q[++head]=To[i];
}
return res;
}
int main()
{
int N,D,u,v,i;
while(~scanf("%d%d%d",&N,&K,&D)){
cnt=; ans=; head=; tail=;
memset(Laxt,,sizeof(Laxt));
for(i=;i<N;i++) scanf("%d%d",&u,&v),add(u,v),add(v,u);
printf("%d\n",(cal(D-)-cal(D)+Mod)%Mod);
}
return ;
}

牛客网暑期ACM多校训练营(第三场)G:Coloring Tree(函数的思想)的更多相关文章

  1. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  2. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  3. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  4. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  5. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

  6. 牛客网暑期ACM多校训练营(第九场)D

    链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...

  7. 牛客网暑期ACM多校训练营(第二场)B discount

    链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...

  8. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  9. 牛客网暑期ACM多校训练营(第二场) I Car 思维

    链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...

  10. 牛客网暑期ACM多校训练营(第二场) D money 思维

    链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...

随机推荐

  1. 应对ie双外边距,不使用hack

    1.在浮动元素内层加一层div 2.使用不浮动的内层外边距来定义距离 ie在浮动时,并且使用外边距,会产生双倍外边距.

  2. scrapy之Logging使用

    #coding:utf-8 __author__ = 'similarface' ###################### ##Logging的使用 ###################### ...

  3. Delphi下如何使程序在Win7/Vista上用管理员权限运行(转)

    Delphi程序必须在资源里面嵌入MANIFEST信息 一 首先编辑一个文件,内容如下: <?xml version="1.0" encoding="UTF-8&q ...

  4. Gunicorn、Supervisor

    简介 Gunicorn来源于Ruby的unicorn项目,是一个Python WSGI HTTP Server,通过pre-fork worker模型来管理和维护worker. 简而言之就是通过多进程 ...

  5. linux 经常使用命令

    帮助信息 ./configure -help|grep mysql 安装php ./configure --prefix=/usr/local/fastphp --with-mysql=mysqlnd ...

  6. 【WPF】ComboBox:根据绑定选取、设置固定集合中的值

    问题场景 我有一个对象,里面有一个属性叫Limit,int类型.虽然int可取的范围很大,我想要在用户界面上限制Limit可取的值,暂且限制为5.10.15.20. 所以ComboBox绑定不是绑定常 ...

  7. React常用方法手记

    1.Reactjs 如何获取子组件的key值?请问antd中table自定义列render方法怎么获取当前第几列? https://segmentfault.com/q/101000000453235 ...

  8. Memcache安装与使用

    一.资源下载 安装memcached 之前必需要先安装 libevent 分别在libevent和memcached的官网下载安装包libevent-1.4.14b-stable.tar.gz和mem ...

  9. vsftp时间差8个小时的解决方法

    $ vi /etc/vsftpd/vsftpd.conf use_localtime=YES ;

  10. CentOS、乌班图设置固定静态IP

    CentOS.乌班图设置固定静态IP 一.centOS 1.编辑 ifcfg-eth0 文件 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2,在文件 ...