Counting Offspring

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

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<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#define ll long long
#define eps 1e-10
#define LL unsigned long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=+;
const int mod=1e9+;
int head[N];
int tot,time;
int L[N],R[N];
struct node{
int to,next;
}edge[N<<];
int a[N*];
void init(){
memset(head,-,sizeof(head));
tot=;
time=;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void DFS(int x,int fa){
L[x]=++time;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==fa)continue;
DFS(v,x);
}
R[x]=time; }
int lowbit(int x){
return x&(-x);
}
void update(int x,int y){
while(x<=time){
a[x]=a[x]+y;
x=x+lowbit(x);
}
}
int getsum(int x){
int ans=;
while(x>){
//cout<<2<<endl;
ans=ans+a[x];
x=x-lowbit(x);
}
return ans;
}
int main(){
int n,p;
while(scanf("%d%d",&n,&p)!=EOF){
if(n==&&p==)break;
init();
memset(a,,sizeof(a));
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++){
int ans=getsum(R[i])-getsum(L[i]-);
if(i!=n)cout<<ans<<" ";
else{
cout<<ans<<endl;
}
update(L[i],);
}
}
}

HDU 3887 Counting Offspring(DFS序+树状数组)的更多相关文章

  1. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

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

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

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

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

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

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

  5. 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 ...

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

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

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

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

  8. 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 ...

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

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

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

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

随机推荐

  1. quartz 数据库表含义解释

    http://blog.csdn.net/tengdazhang770960436/article/details/51019291 一.表信息解析: 1.1.qrtz_blob_triggers : ...

  2. HDU_1848_博弈,sg函数

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  3. sysbench_memory

    对于内存而言,这里--memory-total-size=100G 就是 意味着 total number of events: 104857600 1. --memory-total-size=10 ...

  4. Xcode 插件因为UUID原因不能使用解决办法

    Xcode 经常因为一些原因不能使用,需要重新在  ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins目录下对每一个插件包下的p ...

  5. 洛谷——P3833 [SHOI2012]魔法树

    P3833 [SHOI2012]魔法树 题目背景 SHOI2012 D2T3 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的 ...

  6. chromeDriver下载地址

    http://chromedriver.storage.googleapis.com/index.html

  7. [luoguP1993] 小 K 的农场(差分约束 + spfa 判断负环)

    传送门 差分约束系统..找负环用spfa就行 ——代码 #include <cstdio> #include <cstring> #include <iostream&g ...

  8. noip模拟赛 c

    分析:一道比较难的爆搜题.首先要把9个块的信息存下来,记录每个块上下左右位置的颜色,然后记录每一排每一列能否操作,之后就是bfs了.在bfs的时候用一个数记录状态,第i位表示原来的第i个块现在在哪个位 ...

  9. js es6 Object.freeze

    将对象冻结,使用Object.freeze方法 const foo = Object.freeze({}); // 常规模式时,下面一行不起作用: // 严格模式时,该行会报错 foo.prop = ...

  10. nyoj_171_聪明的kk_201402281518

    聪明的kk时间限制:1000 ms  |  内存限制:65535 KB 难度:3描述 聪明的“KK”非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘,体现出本国不断变换和绚丽多彩的自然风光 ...