牛客网暑期ACM多校训练营(第三场)G:Coloring Tree(函数的思想)
之前两次遇到过函数的思想的题,所以这次很敏感就看出来了。可以参考之前的题:
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(函数的思想)的更多相关文章
- 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?
牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...
- 牛客网 暑期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 ,问你 ...
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...
- 牛客网暑期ACM多校训练营(第九场) A题 FWT
链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...
- 牛客网暑期ACM多校训练营(第九场)D
链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...
- 牛客网暑期ACM多校训练营(第二场)B discount
链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
- 牛客网暑期ACM多校训练营(第二场) I Car 思维
链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...
- 牛客网暑期ACM多校训练营(第二场) D money 思维
链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...
随机推荐
- 【转载】IIS与asp.net管道
阅读目录 asp.net是什么 HTTP协议 IIS与asp.net asp.net管道 参考资料 我们在基于asp.net开发web程序,基本上都是发布部署到安装了IIS的windows服务器上,然 ...
- C 语言学习 3
[程序3] 题目:一个整数,它加上100后是一个全然平方数.再加上168又是一个全然平方数.请问该数是多少? 1.程序分析:在10万以内推断.先将该数加上100后再开方,再将该数加上268后再开方,假 ...
- Android推断是否有sd卡
推断手机上是否有SD卡存在.作为经常用法,写到工具类里,用时直接调用.代码例如以下: public static boolean hasSdcard(){ String state = Environ ...
- 在IDEA建立Maven的多模块Web项目
由于要搭建的是Maven项目,考虑到后面可能会有扩展,因此项目搭建的分模块的. 下面一步一步的来搭建这个项目 打开IDEA集成开发环境,点击File ---> New ---> Proje ...
- 【转】Android7.0版本以上的手机Eclipse无法打出LogCat
本来想用Eclipse连下手机看下log的,结果LogCat没打出来任何信息,起初怀疑是我的DDMS有问题,结果连了下我老大的手机,完美打出log,看了下Android系统,老大的是6.0的,我的7. ...
- WPF之DataGrid篇:DataGridComboBoxColumn
准备数据源 1 准备数据源.基类为Student,数据对象为Student3,数据集为StuList3. END 编辑DataGrid显示列 1 若要填充下拉列表,请首先使用下列选项之一设置 ...
- java ResultSet获得总行数
在Java中,获得ResultSet的总行数的方法有以下几种. 第一种:利用ResultSet的getRow方法来获得ResultSet的总行数 Statement stmt = con.create ...
- 用户对变量或寄存器进行位操作 、“|=”和“&=~”操作
给定一个整型变量a,写两段代码,第一个设置a的bit 3,第二个清除a的bit 3.在以上两个操作中,要保持其他位不变. 答案: ----------------------------------- ...
- vue-cli3.0升级失败,vue-cli卸载不掉,vue-cli升级不了3.0
https://juejin.im/post/5bf7d67c51882518805acb1a vue-cli3.0 使用图形化界面创建和管理项目
- 使用了Tomcat JDBC连接池不能重连的问题
在项目中用到了tomcat 的jdbc连接池,发现一个问题是,当数据库重启时,服务没有重新的去连接数据库,需要将部署的项目重新启动才能连接到数据库.经过测试对配置做一下修改: 在配置dataSourc ...