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. mysql 5.7 ERROR 1820 (HY000):

    在首次登录Mysql 5.7 后,mysql数据库做出了很多的调整.执行大部分操作会提示这个错误 : ERROR 1820 (HY000): You must reset your password ...

  2. Android相关 博客收藏

    #1 Android 网络编程 参考博客 :http://blog.csdn.net/kieven2008/article/details/8210737 #2 Could not find com. ...

  3. tomcat和server安装和操作

    servers的创建 最后一步:ctrl+s

  4. Fisher–Yates shuffle 算法

    费希尔 - 耶茨洗牌 维基百科,自由的百科全书     所述费-耶茨洗牌是一种算法,用于产生随机排列的有限的序列 -in平原而言,算法打乱的序列.该算法有效地将所有元素放在帽子里; 它通过随机从帽子中 ...

  5. 插件使用一表单验证一validation

    jquery-validation是一款前端经验js插件,可以验证必填字段.邮件.URL.数字范围等,在表单中应用非常广泛. 官方网站 https://jqueryvalidation.org/ 源码 ...

  6. robocopy的用法,数据库局域网备份

    robocopy,我主要是用来进行局域网数据库备份使用,不得不说这个小工具速度还是蛮快的,同时属于系统内置功能,用着还算方便. 这项功能就是RoboCopy,它是一个命令行的目录复制命令,自从Wind ...

  7. nginx配置http和https共存

    server { listen ; listen ssl; server_name localhost; #ssl on; ssl_certificate /usr/local/Tengine/ssl ...

  8. Java集合源码学习(四)HashMap

    一.数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据的存储,这两者有不同的应用场景,数组的特点是:寻址容易,插入和删除困难:链表的特点是:寻址困难,插入和删除容易:哈希表的实现结合了这两点, ...

  9. Centos7编译安装GCC7.2

    通常编译的时候可能需要新版本的gcc,本文就说明下基于低版本的gcc升级为gcc7.2 wget 'http://mirrors-usa.go-parts.com/gcc/releases/gcc-7 ...

  10. 【Android】Android WebView 网页输入框获取焦点

    webView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEv ...