Paint the Tree

题目来源:

Moscow Pre-Finals Workshop 2018 Day 5 C

题目大意:

一棵\(n(n\le2000)\)个点的树,有\(m(2<m<n)\)种颜料,为每个结点涂色,希望让最近的同色点对距离最远。问最远距离是多少,此时有多少种涂色方案。

思路:

二分答案\(k\),用BFS序枚举点对,若距离\(<k\)则说明这两个点一定是不同颜色。

时间复杂度\(\mathcal O(n^2\log n)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=2001,mod=1e9+7;
bool vis[N];
int n,m,dis[N][N],q[N];
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
void dfs(const int &x,const int &par,int dis[]) {
for(auto &y:e[x]) {
if(y==par) continue;
dis[y]=dis[x]+1;
dfs(y,x,dis);
}
}
inline void bfs() {
q[++q[0]]=1;
for(register int i=1;i<=n;i++) {
const int &x=q[i];
vis[x]=true;
for(auto &y:e[x]) {
if(vis[y]) continue;
q[++q[0]]=y;
}
}
}
inline int check(const int &k) {
int ans=1;
for(register int i=1;i<=n;i++) {
int cnt=0;
for(register int j=1;j<i;j++) {
cnt+=dis[q[i]][q[j]]<k;
}
if(cnt>m) return 0;
ans=1ll*ans*(m-cnt)%mod;
}
return ans;
}
int main() {
n=getint(),m=getint();
for(register int i=1;i<n;i++) {
add_edge(getint(),getint());
}
for(register int i=1;i<=n;i++) {
dfs(i,0,dis[i]);
}
bfs();
int l=1,r=n-1,ans;
while(l<=r) {
const int mid=(l+r)>>1;
int tmp;
if(tmp=check(mid)) {
l=mid+1;
ans=tmp;
} else {
r=mid-1;
}
}
printf("%d %d\n",l-1,ans);
return 0;
}

Paint the Tree的更多相关文章

  1. E. Paint the Tree 树形dp

    E. Paint the Tree 题目大意:给你一棵树,每一个点都可以染k种颜色,你拥有无数种颜色,每一种颜色最多使用2次,如果一条边的两个节点拥有同一种颜色,那么就说 这条边是饱和的,一个树的价值 ...

  2. Codeforces 1244D. Paint the Tree

    传送门 首先如果某个点的度数大于 $2$ 那么显然无解 然后考虑点的度数小于等于 $2$ 的情况 发现其实是一条链 一旦确定了链开头的两个点,后面的点的颜色都可以通过之前的点推出 所以直接枚举即可 # ...

  3. Codeforces 1240C. Paint the Tree

    传送门 首先每个点 $u$ 只能选择不超过 $k$ 个相连的边 并且设边为 $(u,v)$ ,那么此时 $v$ 也必须选择这条边 因为图是一颗树,显然考虑一下树形 $dp$ 设 $f[x][0/1]$ ...

  4. Codeforces1223E. Paint the Tree(树形dp)

    题目链接:传送门 题目大意: 给出节点数为n的一棵带权树,和每个点的最大染色数k.一条边的权重w能产生价值w的条件是,这条边的两端的点至少有一个颜色相同.颜色种类数无限,但每种只能使用两次,问能产生的 ...

  5. 【CF1244D】Paint the Tree(树形DP,树)

    题意: n<=1e5,1<=a[i][j]<=1e9 思路: 不是很懂INF为什么要开到1e15,我觉得只要1e14就好 #include<bits/stdc++.h> ...

  6. Codeforces Round #592 (Div. 2) D - Paint the Tree

    题目链接:https://codeforces.com/contest/1244/problem/D 题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是 ...

  7. cf 1241 E. Paint the Tree(DP)

    题意: 有一颗树,n个点,边有边权. 有无限多种颜色,每个点可以同时染上k种颜色,如果一条边的两个端点 拥有至少一种相同的颜色,那么说这条边是“饱和的”. 问:所有“饱和边”的权值和最大为多少,只需要 ...

  8. E. Paint the Tree(树形dp)

    题:https://codeforces.com/contest/1241/problem/E 题目大意:给你一棵树,每一个点都可以染k种颜色,你拥有无数种颜色,每一种颜色最多使用2次,如果一条边的两 ...

  9. Codeforces Round #382 (Div. 2)E. Ostap and Tree

    E. Ostap and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. Ajax爬虫必用到的字典转换器

    1.使用情景 在我们Ajax爬虫时需要用到以下这样的数据的时候我们会一个一个地复制粘贴,这样会很麻烦 def dictionary_converter(key_value): '''主要用于爬虫时复制 ...

  2. ubuntu下使用matplotlib绘图无法显示中文label

    原因是字体导致的.大家的做法基本都是搞一个windows上的字体文件(simhei.ttf, 点我fq下载)然后刷新一下缓存文件. 只不过百度搜到第一篇CSDN的博客,写的很不靠谱(不是所有的CSDN ...

  3. autofac 创建实例方法总结

    1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component so tha ...

  4. QQ登录用到的URL

    //QQ 登陆页面的URL,client_id就是APP ID,会返回一个codehttps://graph.qq.com/oauth2.0/authorize?response_type=code& ...

  5. Elasticsearch索引模板和别名

    创建模板(模板名和索引名一样都不能有大写) PUT http://222.108.x.x:9200/_template/templateds { "template": " ...

  6. Vs2015 本地git获取的代码目录文件修改后,启动提示error:Unable to start program “C:\Program Files\dotnet\dotnet.exe” 已解决.

    http://stackoverflow.com/questions/39938453/unable-to-start-program-c-program-files-dotnet-dotnet-ex ...

  7. mysql的下载与安装

    官网 下载 https://www.mysql.com/ 依次点击:downloads>community>mysql community server,如图所示   image.png ...

  8. springboot中使用拦截器、监听器、过滤器

     拦截器.过滤器.监听器在web项目中很常见,这里对springboot中怎么去使用做一个总结. 1. 拦截器(Interceptor)   我们需要对一个类实现HandlerInterceptor接 ...

  9. jQuery选择器总结 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法

    新年第一编文章 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法   $("#myELement")    选择id值等于myElement的元素,id值 ...

  10. 【Android】Android模拟器快速root

    启动Android模拟器,开始-运行-输入cmd,运行命令行 adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system ...