dfs序 + 树状数组
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序 + 树状数组的更多相关文章
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- 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 ...
- BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...
- 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组
题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
- [BZOJ1103][POI2007]大都市meg dfs序+树状数组
Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...
- 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)
传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- 【BZOJ】2819: Nim(树链剖分 / lca+dfs序+树状数组)
题目 传送门:QWQ 分析 先敲了个树链剖分,发现无法AC(其实是自己弱,懒得debug.手写栈) 然后去学了学正解 核心挺好理解的,$ query(a) $是$ a $到根的异或和. 答案就是$ l ...
- 【bzoj3653】谈笑风生 DFS序+树状数组
题目描述 给出一棵以1为根的有根树,q次询问,每次询问给出a和k,求点对 (b,c) 的数目,满足:a.b.c互不相同,b与a距离不超过k,且a和b都是c的祖先. 输入 输入文件的第一行含有两个正整数 ...
随机推荐
- Vue 语法的一些小问题
设置 sty行内样式 :style="{width:mapWidth,height:mapHeight}" This指向 axios 使用axios 的时候 ,在生命周期函数 ...
- 2019-11-6-Roslyn-how-to-use-WriteLinesToFile-to-write-the-semicolons-to-file
title author date CreateTime categories Roslyn how to use WriteLinesToFile to write the semicolons t ...
- 2018-4-29-C#-金额转中文大写
title author date CreateTime categories C# 金额转中文大写 lindexi 2018-04-29 09:50:38 +0800 2018-04-02 21:4 ...
- H3C 显示RIP当前运行状态及配置信息
- Spring Security 5.x兼容多种密码加密方式
1 spring security PasswordEncoder spring security 5不需要配置密码的加密方式,而是用户密码加前缀的方式表明加密方式,如: {MD5}88e2d8cd1 ...
- H3CSTP、RSTP的问题
- 【codeforces 749A】Bachgold Problem
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- H3C配置Header进入用户视图的提示信息--系统视图
incoming:登录终端用户界面时的提示信息. Header 3种类型 login:登录验证时的提示信息. Vty模式 ...
- CF140CNew Year Snowmen
CF140C 题目大意:堆雪人,需要三个大小不同的雪球,现有\(n\)个给定大小的雪球,问最多堆多少个雪人 一个很明显的思路是把每种雪球出现的个数记录下来,然后直接扔到大根堆里面,每次选择剩下出现次数 ...
- 使用Pandas加载数据
1.dataframe对象简述: dataframe为pandas中一种有行列索引的二维数据结构,可以看成在普通二维结构上加上行列id标记 示例为创建一个2X3的dataframe: import s ...