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. webpack学习(二)初识打包配置

    前言:webpack打包工具让整个项目的不同文件夹相互关联,遵循我们想要的规则.想 .vue文件, .scss文件浏览器并不认识,因此webpage暗中做了很多转译,编译等工作. 事实上,如果我们在没 ...

  2. C++的价值

    In May 2010, the GCC steering committee decided to allow use of a C++ compiler to compile GCC. The c ...

  3. jmeter登录配置

    前言: jmeter, Apache下的测试工具, 常用来进行压测, 项目中, 接口通常都需要进行登录才能被调用, 直接调用将提示"登录失效", 下面介绍如何在jmeter中配置参 ...

  4. P1013 高精度加法

    题目描述 给你两个很大的正整数A和B,你需要计算他们的和. 输入格式 输入一行包含两个正整数A和B,以一个空格分隔(A和B的位数都不超过 \(10^5\)) 输出格式 输出一行包含一个整数,表示A+B ...

  5. C# 性能分析 反射 VS 配置文件 VS 预编译

    本文分析在 C# 中使用反射和配置文件和预编译做注入的性能,本文的数据是为预编译框架,开发高性能应用 - 课程 - 微软技术暨生态大会 2018 - walterlv提供 本文通过代码生成工具,使用C ...

  6. 【23.91%】【hdu 4694】Important Sisters("支NMLGB配树"后记)(支配树代码详解)

    Time Limit: 7000/7000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission( ...

  7. CodeForces 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  8. Android利用Fiddler进行网络数据抓包,手机抓包工具汇总,使用mono运行filddler

    Fiddler抓包工具 Fiddler抓包工具很好用的,它可以干嘛用呢,举个简单例子,当你浏览网页时,网页中有段视频非常好,但网站又不提供下载,用迅雷下载你又找不到下载地址,这个时候,Fiddler抓 ...

  9. apache WEB服务器安装(包括虚拟主机)

    一.apache下载编译安装 yum install apr apr-devel apr-util apr-util-devel gcc-c++ wget tar -y cd /usr/src wge ...

  10. Android APP前后台状态切换

    getActivity().getApplication().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleC ...