HDU3887 Counting Offspring

本文介绍了一种利用树状数组解决特定问题的方法,即对于一棵给定的树,计算每个节点的子树中节点序列号小于该节点的所有节点数量。通过深度优先搜索建立节点访问序列,并运用树状数组高效地完成求和与更新操作。

摘要生成于 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

http://www.cnblogs.com/kane0526/archive/2013/01/10/2855125.html

题目大意: 给你一颗n个节点的数,对于每个节点i,问你每个节点的子树中有
多少个节点序列数小于i,求f[i]。

这里写图片描述

解题思路:

用样例来说明,我们可以先深搜一遍,那么dfs序就是:7, 10, 14, 2 ,2 ,13, 13, 14, 10, 1, 1 … …

对这个序列进行分析可以发现,举例:14, 2 ,2 ,13, 13, 14 , 节点14中间的数就是它的子树序列。

dfs打好序列后就对序列进行遍历一遍,开两个st[], sd[] 数组, 存每个节点开始和结束位置。接下来就是树状数组求和,注意这里要序列从大到小求和(也就是n–>1),求完后删掉对应位置的数,树状数组进行相应的更新操作。

//从大到小机智,从15开始,其他数都比它小,因此用每个数都是1,就是求和。接着将15划掉,那么左14到右14中间的数都比它小,因此求和。

for(int i=n; i>=1; i--)
        {
            f[i]=(getsum(sd[i]-1)-getsum(st[i]))/2;
            cal(st[i],-1);
            cal(sd[i],-1);
        }
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;

const int maxn=100005;
vector<int>vt[maxn];
int bit[2*maxn];
int que[2*maxn];//<2倍 
int st[maxn];
int sd[maxn];
int f[maxn];
int n, rt, num;

void dfs(int u, int fa)
{
    que[++num]=u;
    for(int i=0; i<vt[u].size(); i++)//
    {
        int v=vt[u][i];
        if(v==fa) continue;
        dfs(v,u);
    }                                 //
    que[++num]=u;
}

int lowbit(int x)
{
    return x&(-x);
}

void cal(int x, int val)
{
    while(x<=num)
    {
        bit[x]+=val;
        x+=lowbit(x);
    }
}

int getsum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=bit[x];
        x-=lowbit(x);
    }
    return ans;
}

int main()
{
    while(~scanf("%d%d",&n,&rt),n+rt)
    {
        for(int i=0; i<=n; i++)
            vt[i].clear();
        for(int i=1; i<n; i++)
        {
            int x, y;
            scanf("%d%d",&x,&y);
            vt[x].push_back(y);
            vt[y].push_back(x);
        }
        fill(st+1,st+1+n,0);
        num=0;
        dfs(rt,-1);

        for(int i=1; i<=num; i++)//.... 
        {
            if(!st[que[i]]) st[que[i]]=i;
            else sd[que[i]]=i;
        }
        memset(bit,0,sizeof(bit));
        for(int i=1; i<=num; i++)
            cal(i,1);//每个数都是1

        for(int i=n; i>=1; i--)
        {
            f[i]=(getsum(sd[i]-1)-getsum(st[i]))/2;
            cal(st[i],-1);
            cal(sd[i],-1);
        }
        printf("%d",f[1]);
        for(int i=2; i<=n; i++)
            printf(" %d",f[i]);
        puts("");
    }
    return 0;
}
资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化和响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”和“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)主要负责样式设计。它首先清除了一些默认的边距和填充,对 html 和 body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值