hdu 3887 Counting Offspring 树上求所有节点的子树上比当前节点小的个数 树状数组

本文介绍了一个使用树状数组解决HDU 3887问题的算法,该问题是求解树上每个节点的子树中比当前节点小的节点数量。通过初始化、添加边、计算节点贡献等步骤,实现了高效地更新和查询树中节点的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
  
  
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.
 

Input
  
  
Multiple 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.
 

Output
  
  
For 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

//

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=510000;
struct Node
{
    int t;
    int next;
};
int p[maxn];
Node G[maxn*2];
int l;
int ans[maxn];
int V,root;
void init()
{
    memset(p,-1,sizeof(p));
    memset(ans,0,sizeof(ans));
    l=0;
}
void addedge(int u,int t,int l)
{
    G[l].t=t;
    G[l].next=p[u];
    p[u]=l;
}
int c[maxn];
int n;
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int val)
{
    for(int i=x;i<=n;i+=lowbit(i))
    {
        c[i]+=val;
    }
}
int getsum(int x)
{
    if(x==0) return 0;
    int cnt=0;
    for(int i=x;i>=1;i-=lowbit(i))
    {
        cnt+=c[i];
    }
    return cnt;
}
int vis[maxn];
int stc[maxn];
int before[maxn];
int stp;
void calc()
{
    memset(vis,0,sizeof(vis));
    stp=0;
    stc[++stp]=root;
    while(stp>0)
    {
        int u=stc[stp];
        if(vis[u]==0)
        {
            vis[u]=1;
            before[u]=getsum(u-1);
            for(int i=p[u];i!=-1;i=G[i].next)
            {
                int t=G[i].t;
                if(vis[t]==0)
                {
                    stc[++stp]=t;
                }
            }
            continue;
        }
        int after=getsum(u-1);
        ans[u]=after-before[u];
        vis[u]=2;
        stp--;
        update(u,1);
    }
}
int main()
{
    while(scanf("%d%d",&V,&root)==2&&V)
    {
        init();n=V;
        for(int i=0;i<V-1;i++)
        {
            int u,t;scanf("%d%d",&u,&t);
            addedge(u,t,l++);
            addedge(t,u,l++);
        }
        calc();
        printf("%d",ans[1]);
        for(int i=2;i<=V;i++) printf(" %d",ans[i]);printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值