牛客网暑期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 ...
随机推荐
- 37:密码截取(回文串manacher算法)
题目描述:Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解.比如 ...
- ssh port forwarding
SSH端口转发,总是忘记,今天记录下.端口转发有两种,一个是local一个是remote(可能还有一种dynamic,还没有研究) 贴个链接 https://www.ssh.com/ssh/tunne ...
- 给js对象赋值,赋值key
var pastResult = []; pastResult.push(feature.attributes.F_iID); pastResult.push(feature.attributes.F ...
- 【Python基础】之异常
一.常见异常 try: open('abc.txt','r') except FileNotFoundError: print('异常啦!') 输出结果: ======= 异常啦! 我们通过 open ...
- Cesium--气泡弹窗
参考资料 首先感谢以下博主们的帮助,本人刚接触Cesium不久,无奈只能拾人牙慧了. 由于cesium没有自带的点击弹出气泡的功能,所以需要自己去开发一个这样的功能,网络上资源很多,看到基本思路都一致 ...
- 关于msbuild 编译.net 4.5新语法错误的解决方法
.net4.5以前msbuild 是在%windir%/Microsoft.NET/FrameworkXX/vXX目录下,如:C:\Windows\Microsoft.NET\Framework64\ ...
- Javaweb之 servlet 开发具体解释1
1.1 Tip:Servlet简单介绍 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发 ...
- python 基础 2.8 python练习题
python 练习题: #/usr/bin/python #coding=utf-8 #@Time :2017/10/26 9:38 #@Auther :liuzhenchuan #@File ...
- python 基础 1.5 python数据类型(四)--字典
一.python 数据类型--字典 1.用字符串存储信息,如:存储“姓名,身高,性别”: In [1]: info='Tom 170 M' //字符串存储信息 In [3]: info[0:3] // ...
- Linux Tomcat的安装
inux版本:CentOS 6.2 iso文件下载地址:http://mirrors.163.com/centos/6.2/isos/i386/CentOS-6.2-i386-bin-DVD1.iso ...