传送门

想了半小时,没什么思路。。

看了题解,是个叫做树分块的奇奇怪怪的操作。。

题解

树分块的研究

#include <cstdio>
#include <cstring>
#define N 2001 int n, b, size, cnt, tot;
int head[N], to[N], nex[N], belong[N], s[N], root[N];
bool vis[N]; inline void add(int x, int y)
{
to[cnt] = y;
nex[cnt] = head[x];
head[x] = cnt++;
} inline void dfs(int u)
{
int i, v, bottom = size;
vis[u] = 1;
for(i = head[u]; ~i; i = nex[i])
{
v = to[i];
if(!vis[v])
{
dfs(v);
if(size - bottom >= b)
{
root[++tot] = u;
while(size > bottom)
belong[s[size--]] = tot;
}
}
}
s[++size] = u;
} int main()
{
int i, x, y;
scanf("%d %d", &n, &b);
memset(head, -1, sizeof(head));
for(i = 1; i < n; i++)
{
scanf("%d %d", &x, &y);
add(x, y);
add(y ,x);
}
dfs(1);
while(size) belong[s[size--]] = tot;
printf("%d\n", tot);
for(i = 1; i <= n; i++) printf("%d ", belong[i]);
puts("");
for(i = 1; i <= tot; i++) printf("%d ", root[i]);
return 0;
}

  

最新文章

  1. 【USACO 2.3】The Longest Prefix
  2. jstl 小总结 以及 jstl fn
  3. 14.Xcode8imageview图片圆角不显示的bug
  4. Yii CModel中rules验证 获取错误信息
  5. c语言.大数的输出
  6. js调用java代码返回解决方案
  7. eclipse从数据库逆向生成Hibernate实体类
  8. 第二章 Mysql 数据类型简介--(整数类型、浮点数类型和定点数类型,日期与时间类型,字符串类型,二进制类型)
  9. windows phone listbox虚拟化(下)
  10. What floating point types are available in .NET?
  11. select绑定json数组对象 asp.net
  12. sort,uniq命令
  13. 使用最新的log4cplus(1.1.1)隔离不同的 log 文件输出
  14. 《java入门第一季》之面向对象面试题(this和super的区别)
  15. FLV 封装格式解析
  16. typeof() 和 GetType()区是什么
  17. 使用Visual Studio Team Services进行压力和性能测试(二)——压力测试执行
  18. Spark:java api读取hdfs目录下多个文件
  19. pdo连接数据
  20. pycharm 配置服务器,脚本,测试文件

热门文章

  1. Jenkins默认工作空间及更改默认工作空间
  2. dp 20190618
  3. python之路——递归函数
  4. 【转】Spring, MyBatis 多数据源的配置和管理
  5. 【转】Java重构-策略模式、状态模式、卫语句
  6. 使用xcode workspace 多个project协同工作
  7. 【Java_基础】空串、空格串、null的区别
  8. Git学习——提交BUG
  9. Ubuntu中安装配置 JDK与apache
  10. 【Linux】VirtualBox虚拟网络配置