Paint the Tree
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的更多相关文章
- E. Paint the Tree 树形dp
E. Paint the Tree 题目大意:给你一棵树,每一个点都可以染k种颜色,你拥有无数种颜色,每一种颜色最多使用2次,如果一条边的两个节点拥有同一种颜色,那么就说 这条边是饱和的,一个树的价值 ...
- Codeforces 1244D. Paint the Tree
传送门 首先如果某个点的度数大于 $2$ 那么显然无解 然后考虑点的度数小于等于 $2$ 的情况 发现其实是一条链 一旦确定了链开头的两个点,后面的点的颜色都可以通过之前的点推出 所以直接枚举即可 # ...
- Codeforces 1240C. Paint the Tree
传送门 首先每个点 $u$ 只能选择不超过 $k$ 个相连的边 并且设边为 $(u,v)$ ,那么此时 $v$ 也必须选择这条边 因为图是一颗树,显然考虑一下树形 $dp$ 设 $f[x][0/1]$ ...
- Codeforces1223E. Paint the Tree(树形dp)
题目链接:传送门 题目大意: 给出节点数为n的一棵带权树,和每个点的最大染色数k.一条边的权重w能产生价值w的条件是,这条边的两端的点至少有一个颜色相同.颜色种类数无限,但每种只能使用两次,问能产生的 ...
- 【CF1244D】Paint the Tree(树形DP,树)
题意: n<=1e5,1<=a[i][j]<=1e9 思路: 不是很懂INF为什么要开到1e15,我觉得只要1e14就好 #include<bits/stdc++.h> ...
- Codeforces Round #592 (Div. 2) D - Paint the Tree
题目链接:https://codeforces.com/contest/1244/problem/D 题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是 ...
- cf 1241 E. Paint the Tree(DP)
题意: 有一颗树,n个点,边有边权. 有无限多种颜色,每个点可以同时染上k种颜色,如果一条边的两个端点 拥有至少一种相同的颜色,那么说这条边是“饱和的”. 问:所有“饱和边”的权值和最大为多少,只需要 ...
- E. Paint the Tree(树形dp)
题:https://codeforces.com/contest/1241/problem/E 题目大意:给你一棵树,每一个点都可以染k种颜色,你拥有无数种颜色,每一种颜色最多使用2次,如果一条边的两 ...
- 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 ...
随机推荐
- FFmpeg的一般流程
FFMPeg一般流程: 1.av_register_all();//注册所有文件格式和编解码库 2.avformat_network_init();//打开网络视频流 3.av_open_input_ ...
- linux添加自定义命令
想添加一个命令, 比如我输入 cdms 按回车, 然后就执行了: cd /mnt/gopath/src/test/app/ 这条命令方法: vi /etc/bashrc 在文件末尾添加 alias c ...
- js 打开摄像头方法 (定制摄像头)
var video = document.getElementById("video");if (navigator.mediaDevices && navigat ...
- 20165206 2017-2018-2 《Java程序设计》第9周学习总结
20165206 2017-2018-2 <Java程序设计>第9周学习总结 教材学习内容总结 URL类:URL类是java.net包中的一个重要的类,使用URL创建对象的应用程序称为客户 ...
- SHELL打印两个日期之间的日期
SHELL打印两个日期之间的日期 [root@umout shell]# cat date_to_date.sh THIS_PATH=$(cd `dirname $0`;) cd $THIS_PATH ...
- windows下载安装MariaDB10.2.17 绿色版
1.下载 https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.2.17/winx64-packages/mariadb-10.2.17-w ...
- MySql中 delimiter 详解
转载于:http://blog.csdn.net/yuxin6866/article/details/52722913 其实就是告诉MySQL解释器,该段命令是否已经结束了,mysql是否可以执行了. ...
- Java基础知识➣环境搭建与类型整理(一)
概述 公司业务需要,产品既要有.NET又需要Java,没得选择,只能业余时间学习Java,整体觉得Java也.NET还是很相似的,只是语法有差别,差别也不是很大,这就将学习Java的基础知识整理下,以 ...
- python3改版后的特征
1.原始数据类型和运算符 # 整数 3 # => 3 # 算术没有什么出乎意料的 1 + 1 # => 2 8 - 1 # => 7 10 * 2 # => 20 # 但是除法 ...
- docker compose启动服务超时重启记录
一.停docker systemctl stop docker 然后ps -aux grep docker发现有些docker进程还是存在,此时强杀存在的docker进程:ps -aux|grep d ...