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 ...
随机推荐
- 检查URL的可用性脚本
#!/bin/bash check_url() { HTTP_CODE=$(curl -o /dev/ -s -) ];then echo "Warning: $1 Access failu ...
- iptables-snat-dnat-设置
nat internet iptables -t nat -A POSTROUTING -s 192.168.0.0/255.255.255.0 -o eth1 -j SNAT --to-source ...
- 混合编译.c/.cpp与.cu文件
混合编译.c/.cpp与.cu文件 项目中用到cuda编程,写了kernel函数,需要nvcc编译器来编译..c/.cpp的文件,假定用gcc编译. 如何混合编译它们,整体思路是:.cu文件编译出的东 ...
- java解析html的table
import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org. ...
- react native初始化项目
打开命令行窗口,进入我们想要创建项目的父目录,输入命令: npm install -g yarn react-native-cli react-native init 项目名 进入新建的项目目录,执行 ...
- [转]pyCharm最新2018激活码
https://blog.csdn.net/u014044812/article/details/78727496 因公司的需求,需要做一个爬取最近上映的电影.列车号.航班号.机场.车站等信息,所以需 ...
- 利用Jmeter做接口测试的时候,如何提取头部的JSESSIONID然后传递到下一个请求,继续完成当前用户的请求。
其实,关于这个问题有三种种解决方法: 1)从响应数据里面提取JSESSIONID,点击链接可以查看https://www.cnblogs.com/liulinghua90/p/5320290.html ...
- Python交互图表可视化Bokeh:2. 辅助参数
图表辅助参数设置 辅助标注.注释.矢量箭头 参考官方文档:https://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html#col ...
- Shell工具| 流程控制
1. 流程控制 if 判断 ()[ 条件判断式 ],中括号和条件判断式之间必须有空格 ()if后要有空格 [kris@hadoop datas]$ vim if.sh #!/bin/bash -eq ...
- 061 SparkStream数据接收原理
1.宏观 2.看英文解释过程 ------------------------------------------------------------------------------------- ...