[luoguP2325] [SCOI2005]王室联邦(树分块乱搞)
2024-10-20 11:41:06
想了半小时,没什么思路。。
看了题解,是个叫做树分块的奇奇怪怪的操作。。
#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;
}
最新文章
- 【USACO 2.3】The Longest Prefix
- jstl 小总结 以及 jstl fn
- 14.Xcode8imageview图片圆角不显示的bug
- Yii CModel中rules验证 获取错误信息
- c语言.大数的输出
- js调用java代码返回解决方案
- eclipse从数据库逆向生成Hibernate实体类
- 第二章 Mysql 数据类型简介--(整数类型、浮点数类型和定点数类型,日期与时间类型,字符串类型,二进制类型)
- windows phone listbox虚拟化(下)
- What floating point types are available in .NET?
- select绑定json数组对象 asp.net
- sort,uniq命令
- 使用最新的log4cplus(1.1.1)隔离不同的 log 文件输出
- 《java入门第一季》之面向对象面试题(this和super的区别)
- FLV 封装格式解析
- typeof() 和 GetType()区是什么
- 使用Visual Studio Team Services进行压力和性能测试(二)——压力测试执行
- Spark:java api读取hdfs目录下多个文件
- pdo连接数据
- pycharm 配置服务器,脚本,测试文件