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

时间:2021-10-23 00:29:07

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 ;
}