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.
InputMultiple 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.OutputFor 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序,将其变成一维的数组的结构,在寻找的同时再添加一个时间戳,然后树状数组维护下标就可以,当然主席树也可以
代码示例 :
#define ll long long
const int maxn = 1e5+5;
const int mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f; int n, p;
vector<int>ve[maxn];
int s[maxn], e[maxn];
int cnt = 0; void dfs(int x, int fa){
s[x] = ++cnt; for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i];
if (to == fa) continue;
dfs(to, x);
}
e[x] = cnt;
}
int ans[maxn];
int c[maxn]; int lowbit(int x){return x&(-x);}
void add(int x){
int sum = 0;
for(int i = x; i <= n; i += lowbit(i)){
c[i]++;
}
} int query(int x){
int sum = 0; for(int i = x; i >= 1; i -= lowbit(i)){
sum += c[i];
}
return sum;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int a, b; while(scanf("%d%d", &n, &p) && n+p){
for(int i = 1; i <= n; i++) ve[i].clear();
cnt = 0;
for(int i = 1; i < n; i++){
scanf("%d%d", &a, &b);
ve[a].push_back(b);
ve[b].push_back(a);
}
dfs(p, 0);
memset(c, 0, sizeof(c));
for(int i = 1; i <= n; i++){
int st = s[i];
int et = e[i];
ans[i] = query(et)-query(st-1);
add(st);
}
for(int i = 1; i <= n; i++){
printf("%d%c", ans[i], i==n?'\n':' ');
}
}
return 0;
}

dfs序 + 树状数组的更多相关文章

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

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

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

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

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

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

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

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

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

  6. [BZOJ1103][POI2007]大都市meg dfs序+树状数组

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...

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

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

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

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

  9. 【BZOJ】2819: Nim(树链剖分 / lca+dfs序+树状数组)

    题目 传送门:QWQ 分析 先敲了个树链剖分,发现无法AC(其实是自己弱,懒得debug.手写栈) 然后去学了学正解 核心挺好理解的,$ query(a) $是$ a $到根的异或和. 答案就是$ l ...

  10. 【bzoj3653】谈笑风生 DFS序+树状数组

    题目描述 给出一棵以1为根的有根树,q次询问,每次询问给出a和k,求点对 (b,c) 的数目,满足:a.b.c互不相同,b与a距离不超过k,且a和b都是c的祖先. 输入 输入文件的第一行含有两个正整数 ...

随机推荐

  1. no_expand优化案例

    bond 来看一个烂语句: select a.*,b.dn from temp_allcrmuser a, phs_smc_user b  where a.USERNUMBER=b.dn  and ( ...

  2. Python--day42--mysql操作数据库及数据表和基本增删改查

    sql语法规则: 一.操作文件夹 1.创建数据库db2:create database db2; 2.创建数据库db2并标明数据库的编码格式为utf8:create database db2 defa ...

  3. java.util.NoSuchElementException: No value present

    错误: java.util.NoSuchElementException: No value present 原因: 经查询博客Java 8 Optional类深度解析发现,究其原因为: 在空的Opt ...

  4. 如何读取redis中的key值中的结果

    redis的值有5种类型,不同的类型有不同的命令来获取: 字符直接 get key 队列 左端弹出一个元素  LPOP key 哈希 HGET key field 集合 SMEMBERS key 返回 ...

  5. 前端小白-----ES6之字符串模板

    前言:只要坚持就会胜利--Coldfront-小白菜 既是总结也是一种分享 分享内容:ES6 字符串模板 案例1:var Musics=[{music:"六月的雨",singer: ...

  6. Hamcrest使用

    What is Hamcrest? 什么是Hamcrest?   Hamcrest is a library of matchers, which can be combined in to crea ...

  7. 几个关于2-sat的题

    几个关于2-sat的题 HDU3062 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3062 题意: 从2n个人去宴会,有 m条关系 i和j不能同时去 ...

  8. DP刷题记录

    目录 dp刷题记录 codeforces 706C codeforces 940E BZOJ3997 POJ2279 GYM102082B GYM102082D codeforces132C L3-0 ...

  9. 【Docker】初识与应用场景认知

    什么是Docker? Docker是一个容器化平台,它以容器的形式将您的应用程序及其所有依赖项打包在一起,以确保您的应用程序在任何环境中无缝运行. 什么是Docker容器? Docker容器包括应用程 ...

  10. 个人项目之数独的生成与数独残局求解——C语言实现

    点击获取项目文件 1.对项目的分析与初步计划: 起初拿到这个项目是非常懵逼的,因为涉及到很多个人的知识盲区,诸如:C语言文件的操作.命令行参数.Code Quality Analysis工具.性能分析 ...