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. UEditor黑白名单配置

    在网上找了很多,都不对.自己尝试,代码如下: 在new UEditor之前加上: Object.assign(window.UEDITOR_CONFIG.whitList, { filling: [' ...

  2. WebService服务介绍与调用

    一: WebService简介 WebService是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言的下的一个子集)标准来描述.发布.发现.协调 ...

  3. Vuex详解笔记2

    关于 state 每个vuex 应用只有一个 store 实例,所以使用起来不会太复杂,对于定位错误状态和操作会很方便. 简单用法:在vuex 的计算属性中返回vuex 的状态 最基本的使用方式,通过 ...

  4. C#学习-析构函数

    析构函数用于在类销毁之前释放类实例所使用的托管和非托管资源. 对于C#应用程序所创建的大多数对象,可以依靠.NET Framework的垃圾回收器(GC)来隐式地执行内存管理任务. 但是,若创建封装了 ...

  5. golang 中操作nsq队列数据库

    首先先在本地将服务跑起来,我用的是docker-compose ,一句话6666 先新建一个docker-compose.yml version: '2' services: nsqlookupd: ...

  6. Elasticsearch snapshot 备份的使用方法 【备忘】

    常见的数据库都会提供备份的机制,以解决在数据库无法使用的情况下,可以开启新的实例,然后通过备份来恢复数据减少损失.虽然 Elasticsearch 有良好的容灾性,但由于以下原因,其依然需要备份机制. ...

  7. 将Elasticsearch的快照备份到HDFS

    1.安装Elasticsearch插件repository-hdfs 下载地址:https://artifacts.elastic.co/downloads/elasticsearch-plugins ...

  8. nodejs 2017

    1.  nodejs函数 path()  nodejs全局变量 __dirname a.js // 运行 node a.js var path = require('path'); console.l ...

  9. 使用impala对kudu进行DML操作

    将数据插入 Kudu 表 impala 允许使用标准 SQL 语句将数据插入 Kudu 插入单个值 创建表: CREATE TABLE my_first_table ( id BIGINT, name ...

  10. vsftp为不同用户设置不同的ftp的根目录

    需求 要求ftp登录后的根目录是/var/test/,但是又不能影响其他用户的登录路径,因为有些程序是直接在根目录进行操作的,而没有目录切换的过程.操作过程新建用户 useradd test1user ...