Counting Offspring

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2424 Accepted Submission(s): 838

Problem Description
You are given a tree, it’s root is p, and the node is numbered from 1 to n. Now define f(i) as the number of nodes whose number is less than i in all the succeeding nodes of node i. Now we need to calculate f(i) for any possible i.

Input
Multiple cases (no more than 10), for each case:
The first line contains two integers n (0<n<=10^5) and p, representing this tree has n nodes, its root is p.
Following n-1 lines, each line has two integers, representing an edge in this tree.
The input terminates with two zeros.

Output
For each test case, output n integer in one line representing f(1), f(2) … f(n), separated by a space.

Sample Input
15 7
7 10
7 1
7 9
7 3
7 4
10 14
14 2
14 13
9 11
9 6
6 5
6 8
3 15
3 12
0 0

Sample Output
0 0 0 0 0 1 6 0 3 1 0 0 0 2 0

思路: dfs序+树状数组。c++可调整栈的大小.#pragma comment(linker,"/STACK:1024000000,1024000000") 两个1024000000均为字节数。

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
struct Edge{
int to,net;
}es[MAXN+MAXN];
int n,root;
int head[MAXN],tot;
int lch[MAXN],rch[MAXN],key;
int bit[MAXN];
int res[MAXN];
void addedge(int u,int v)
{
es[tot].to=v;
es[tot].net=head[u];
head[u]=tot++;
}
void dfs(int u,int fa)
{
lch[u]=++key;
for(int i=head[u];i!=-;i=es[i].net)
{
int v=es[i].to;
if(v!=fa)
{
dfs(v,u);
}
}
rch[u]=key;
}
void add(int i,int x)
{
while(i<MAXN)
{
bit[i]+=x;
i+=(i&-i);
}
}
int sum(int i)
{
int s=;
while(i>)
{
s+=bit[i];
i-=(i&-i);
}
return s;
}
int main()
{
while(scanf("%d%d",&n,&root)!=EOF&&(n+root)!=)
{
memset(bit,,sizeof(bit));
memset(head,-,sizeof(head));
memset(res,,sizeof(res));
tot=;
key=;
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs(root,-);
for(int i=;i<=n;i++)
{
res[i]=sum(rch[i])-sum(lch[i]-);
add(lch[i],);
}
for(int i=;i<n;i++)
{
printf("%d ",res[i]);
}
printf("%d\n",res[n]);
}
return ;
}

第二版:

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int MAXN=;
vector<int> arc[MAXN];
int n,root;
int bit[MAXN];
void add(int i,int x)
{
while(i<MAXN)
{
bit[i]+=x;
i+=(i&(-i));
}
}
int sum(int i)
{
int s=;
while(i>)
{
s+=bit[i];
i-=(i&(-i));
}
return s;
}
int res[MAXN],vis[MAXN];
void dfs(int u)
{
vis[u]=;
res[u]=sum(u);
add(u,);
for(int i=;i<arc[u].size();i++)
{
int to=arc[u][i];
if(!vis[to])
{
dfs(to);
}
}
res[u]=sum(u)-res[u]-;
}
int main()
{
while(scanf("%d%d",&n,&root)!=EOF&&(n||root))
{
memset(bit,,sizeof(bit));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) arc[i].clear();
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
arc[u].push_back(v);
arc[v].push_back(u);
}
dfs(root);
for(int i=;i<n;i++)
{
printf("%d ",res[i]);
}
printf("%d\n",res[n]);
}
return ;
}
 

HDU3887(树dfs序列+树状数组)的更多相关文章

  1. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  2. 【BZOJ-2434】阿狸的打字机 AC自动机 + Fail树 + DFS序 + 树状数组

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2022  Solved: 1158[Submit][Sta ...

  3. bzoj2434 fail树 + dfs序 + 树状数组

    https://www.lydsy.com/JudgeOnline/problem.php?id=2434 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现, ...

  4. 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)

    传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...

  5. dfs序 线段树 dfs序列 主席树

    并查集 #include<stdio.h> ]; void sset(int x) { ;i<=x;i++) stt[i]=i; } int ffind(int x) { if(x= ...

  6. POJ3321(dfs序列+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25711   Accepted: 7624 Descr ...

  7. 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) F dfs序+树状数组

    Performance ReviewEmployee performance reviews are a necessary evil in any company. In a performance ...

  8. POJ 2763 Housewife Wind(DFS序+LCA+树状数组)

    Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 11419   Accepted: 3140 D ...

  9. BZOJ 4765 普通计算姬 dfs序+分块+树状数组+好题!!!

    真是道好题...感到灵魂的升华... 按dfs序建树状数组,拿前缀和去求解散块: 按点的标号分块,分成一个个区间,记录区间子树和 的 总和... 具体地,需要记录每个点u修改后,对每一个块i的贡献,记 ...

随机推荐

  1. Python的装饰器实例用法小结

    这篇文章主要介绍了Python装饰器用法,结合实例形式总结分析了Python常用装饰器的概念.功能.使用方法及相关注意事项 一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让 ...

  2. 使用eclipse搭建第一个python+Django的web开发实例

    python+Django的web开发实例   一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的 ...

  3. 项目打包部署到tomcat操作步骤

    对于项目部署到tomcat中,需进行一下步骤: 1.对于项目打war包,方式有以下几种:install一下   找到war包的路径即可 另外:在eclipse中,选中项目 1.1 选中Export 1 ...

  4. 玲珑杯 第4次 String cut(暴力字符串)

    题意:给你长度为n(<=100000)的字符串,问你任意删除一个字符后得到循环节最多的数量是多少 题解:最简单的想法就是枚举删除的字符,再kmp求循环节,但是时间复杂度为O(n*n)会超时 因此 ...

  5. php中浮点型变量

    $d = (0.1+0.7)*10;//$d为8 //floor函数返回不大于value的最接近的整数,舍去小数部分取整 $e = floor($d);//$e为7 为什么$e为7,而不是8 因为$d ...

  6. 2017-02-20 可编辑div中如何在光标位置添加内容

    之前做了一个可编辑div需要在里面插入内容,搜了好多代码,就这个能实现我的功能,记录一下,以备以后用 <!DOCTYPE HTML> <html> <head> & ...

  7. 《Advanced Bash-scripting Guide》学习(七):描述、列表和确定是否可以安装一个rpm包

    本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 Example 3-2.将一个代码块的结果保存到文件 #!/bin/bash # ...

  8. hdu3605最大流

    题意:给n个人m个星球,每个人适不适合去某个星球,每个星球的最大容量,看能不能满足所有人能去一个星球 题解:可以看出如果直接建边,边的个数是1e5左右的,这样时间肯定会爆掉,那么我们考虑用二进制状态压 ...

  9. HTTP通道

    通过httptunnel技术进行入侵示例 httptunnel是通过HTTP通道来传输其他协议数据的工具软件,下载地址为:www.http-tunnel. com,目前最新版本3.0.5 工具/原料 ...

  10. 本地安装phpcms步骤

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012145816/article/details/72183032 http://download ...