HDU 3887 Counting Offspring (DFS序构造+线段树)*

Counting Offspring

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3472    Accepted Submission(s): 1184


 

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

Author

bnugong

Source

2011 Multi-University Training Contest 5 - Host by BNU

Recommend

lcy   |   We have carefully selected several similar problems for you:  3450 1166 3030 1541 3743 

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define maxn 205
#define MAX 500005
#define ms memset
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") ///在c++中是防止暴栈用的
struct node
{
    int e,next;
    node (int x=0,int y=0)
    {
        e=x;
        next=y;
    }
}edge[MAX<<1];

int head[MAX];
int pl[MAX],pr[MAX];
int tot,ti;
int sum[MAX<<2];

void init()
{
    memset(head,-1,sizeof(head));
    tot=0;ti=0;
}

/*
题目大意:给定一棵数字组成的树,
统计每个节点的比该节点数字小的个数。

首先感受下什么是dfs序以及如何利用dfs序形成的区间
(时间戳)构成线段树,在pl和pr数组中分别存储
节点i的左右边界。
在update中按照dfs中产生的区间进行更新。
最后先查询后更新。

如何体现查询的答案是比当前节点值小的节点数量呢?
在最后的一维遍历中有答案,有点离线查询的意思在里面,
在查询的节点之前,已经更新过比该节点小的所有节点。
DFS序已经包含了子节点与父节点的关系所以才可以用线段树去破

*/

void add_edge(int a,int b)
{
    edge[tot]=node(b,head[a]);
    head[a]=tot++;
}

void dfs(int u,int fa)
{
    pl[u]=++ti;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].e;
        if(v==fa) continue;
        dfs(v,u);
    }
    pr[u]=ti;
    cout<<u<<" "<<pl[u]<<" "<<pr[u]<<endl;
}

void Build(int l,int r,int rt)
{
    sum[rt]=0;
    if(l==r) return ;
    int mid=(l+r)>>1;
    Build(l,mid,rt<<1);
    Build(mid+1,r,rt<<1|1);
}

void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void update(int l,int r,int rt,int pos,int d)
{
    if(l==r)
    {
        sum[rt]+=d;
        return ;
    }
    int mid=(l+r)>>1;
    if(pos<=mid) update(l,mid,rt<<1,pos,d);
    else  update(mid+1,r,rt<<1|1,pos,d);
    pushup(rt);
}

int query(int l,int r,int rt,int L,int R)
{
    if( L<=l && r<=R )  return sum[rt];
    int mid=(l+r)>>1 , ret = 0;
    if(mid>=L)   ret+= query(l,mid,rt<<1,L,R);
    if(mid<R)     ret+= query(mid+1,r,rt<<1|1,L,R);
    return ret;
}

int n,p,a,b;

int main()
{
    while( scanf("%d%d",&n,&p) &&(n||p))
    {
        init();
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d",&a,&b);
            add_edge(a,b);
            add_edge(b,a);
        }
        Build(1,n,1);
        dfs(p,-1);
        for(int i=1;i<=n;i++)
        {
            printf("%d",query(1,n,1,pl[i],pr[i]));
            if(i==n) puts("");
            else printf(" ");
            update(1,n,1,pl[i],1);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81110779