http://acm.hdu.edu.cn/showproblem.php?pid=3887

题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的。

思路:这题和那道苹果树是一样的,DFS序+树状数组,一开始没想到,用了DFS序+排序,结果超时了。在in和out之间的时间戳是该节点子树的范围,从后往前扫,再删掉大的,这样可以满足值小于该节点的条件。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define N 100010
#define INF 0x3f3f3f3f
struct node
{
int v, nxt;
}edge[N*];
int bit[N*], n, tot, head[N], tim, in[N], out[N], ans[N]; void add(int u, int v)
{
edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++;
} void dfs(int u, int fa)
{
in[u] = ++tim;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(v == fa) continue;
dfs(v, u);
}
out[u] = tim;
} int lowbit(int x)
{
return x & (-x);
} void update(int x, int val)
{
while(x <= tim) {
bit[x] += val;
x += lowbit(x);
}
} int query(int x)
{
int ans = ;
while(x) {
ans += bit[x];
x -= lowbit(x);
}
return ans;
} int main()
{
int rt;
while(scanf("%d%d", &n, &rt), n + rt) {
memset(head, -, sizeof(head));
memset(bit, , sizeof(bit));
tot = tim = ;
for(int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v); add(v, u);
}
dfs(rt, -);
for(int i = ; i <= n; i++) update(i, );
for(int i = n; i >= ; i--) {
ans[i] = query(out[i]) - query(in[i]);
update(in[i], -);
}
for(int i = ; i <= n; i++) {
if(i == n) printf("%d\n", ans[i]);
else printf("%d ", ans[i]);
}
}
return ;
}

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. 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. git merge

    1. git 解决冲突 ***** <<<<<<< HEAD *** *** ======= **** **** ** >>>>> ...

  2. shapely and geos break在distance方法

    问题 from shapely.geometry import Point print Point(0,0).distance(Point(1,1)) 报错 python: GeometryCompo ...

  3. 如何修改DBSNMP和SYSMAN用户的密码

    SYSMAN和DBSNMP用户密码过期后OEM无法使用,并报以下错误: SYSMAN用户的密码被加密后存放在不同的地方,这样database control(OMS和agent)可以不用每次访问数据库 ...

  4. mongodb概念

    一.mongodb与关系型数据库的一些概念上的改变 sql术语 mongodb术语 说明 database database 数据库 table collection 表/集合 row documen ...

  5. nyist 597 完数?

    http://acm.nyist.net/JudgeOnline/problem.php?pid=597 完数? 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 一个 ...

  6. nyist28大数阶乘

    http://acm.nyist.net/JudgeOnline/problem.php?pid=28 大数阶乘 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 我们 ...

  7. java项目中可能会使用到的jar包解释

    一.Struts2 用的版本是struts2.3.1.1 一个简单的Struts项目所需的jar包有如下8个 1. struts2-core-2.3.1.1.jar: Struts2的核心类库. 2. ...

  8. Windows 服务入门指南

    有很多时候,我们需要创建Windows Service. 这篇文章可以算是一个入门指南吧,希望对初学者有帮助. 要创建Windows Service, 首先选择Windows服务项目,如下图: 这里我 ...

  9. linux-批量重命名脚本

    #!/bin/bash # rename jpg and png count= for img in *.jpg *.png do new=image-$count.${img#*.} mv > ...

  10. 有趣的linux命令

    安装工具 debian => apt-get (In Debian like OS) red hat=> yum -y (In Red Hat like OS) mac => bre ...