Counting Offspring

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
 
Author
bnugong
 
Source
题意:给你一棵树,你需要求每个子树中序号比其小的个数;
思路:dfs序,处理出每个子树的序列,因为比其小才有贡献,所以从小到大处理即可,用树状数组维护一下;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
int tree[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int c)
{
while(x<N)
{
tree[x]+=c;
x+=lowbit(x);
}
}
int getnum(int x)
{
int sum=;
while(x)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int query(int L,int R)
{
return getnum(R)-getnum(L-);
}
struct is
{
int v,nex;
}edge[N<<];
int head[N<<],edg;
int in[N],out[N],tot;
int n,p;
void init()
{
memset(tree,,sizeof(tree));
memset(head,-,sizeof(head));
edg=;
tot=;
}
void add(int u,int v)
{
edg++;
edge[edg].v=v;
edge[edg].nex=head[u];
head[u]=edg;
}
void dfs(int u,int fa)
{
in[u]=++tot;
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
if(v==fa)continue;
dfs(v,u);
}
out[u]=tot;
}
int main()
{
while(~scanf("%d%d",&n,&p))
{
init();
if(n==&&p==)break;
for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(p,-);
for(int i=;i<=n;i++)
{
printf("%d%c",query(in[i],out[i]),((i==n)?'\n':' '));
update(in[i],);
}
}
return ;
}

hdu 3887 Counting Offspring dfs序+树状数组的更多相关文章

  1. hdu 5877 Weak Pair dfs序+树状数组+离散化

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Prob ...

  2. 刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)

    题目: Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There ar ...

  3. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  4. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  5. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  6. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  7. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  8. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

  9. 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组

    题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...

随机推荐

  1. Suffix array

    A suffix array is a sorted array of all suffixes of a given string. The definition is similar to Suf ...

  2. NEC学习 ---- 布局 -三列,右侧自适应

    效果如图 html代码: <div class="g-bd3 f-cb"> <div class="g-sd31"> <p> ...

  3. 【翻译】How To Tango With Django 1.5.4 第四章

    4.模板和静态媒体 这章讲解模板引擎 4.1使用模板 前面我们讲解了view和url 映射,创建出了django 的web页面,现在就要将模板混合进去 好的网站在布局上总是有许多重复的.django提 ...

  4. 安装LNMP之后出现 Access denied.解决方法

    权限问题, 执行 sudo chown -R www:www /home/wwwroot

  5. centos升级支持到C++11, gcc4.8.2

    升级到4.8[这个应该是目前最新的啦,不过网上查的话已经到5.2啦,感觉落后一点比较稳,当然还有就是这个版本是新的里面使用最多的] wget http://people.centos.org/tru/ ...

  6. linux环境下配置java WEB项目运行环境,jdk8+tomcat8+mysql5.7.11 新手向

    一:安装jdk 1.下载jdk  在oracle下载东西的时候因为oracle的一些验证机制,所以需要在链接前面添加一些参数 wget --no-check-certificate --no-cook ...

  7. 感冒了~ vs中py和vb实现一个小算法

    1+1*2+1*2*3+--+1*2*3*n 下面是窗体,就一个按钮和编辑框. 中途还遇到了编码问题,但是感冒太难受,加上明天还要上课.就睡了~ 晚安世界.

  8. jquery通过name,id名称获取当前value值

    name是input标签的属性值,jQuery提供了attr() 方法用于设置/改变属性值 $("input:text").attr("name");$(&qu ...

  9. servletFileUpload

    引用:http://bbs.csdn.net/topics/390290685?page=1 Java code? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  10. cpp异常详解

    1. 异常介绍 在函数在执行过程中如果碰到对错误的处理可以有两种方式, 1. 返回错误,2. 使用异常. 如果作为函数的调用者想要知道具体的错误信息, 就需要维护一套错误列表, 或者用string类型 ...