码迷,mamicode.com
首页 > 编程语言 > 详细

HDU3887(树dfs序列+树状数组)

时间:2016-07-21 21:58:23      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

Counting Offspring

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


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

思路: 入栈前数目-入栈后数目.c++可调整栈的大小.#pragma comment(linker,"/STACK:100000000,100000000") 两个100000000均为字节数。

#pragma comment(linker,"/STACK:100000000,100000000")
#include <cstdio>
#include <string.h>
using namespace std;
const int MAXN=100005;
struct Edge{
    int to,net;
}es[MAXN+MAXN];
int head[MAXN],tot;
void addedge(int u,int v)
{
    es[tot].to=v;
    es[tot].net=head[u];
    head[u]=tot++;
    es[tot].to=u;
    es[tot].net=head[v];
    head[v]=tot++;
}
int bit[MAXN];
int n,root;
void add(int i,int x)
{
    while(i<MAXN)
    {
        bit[i]+=x;
        i+=(i&(-i));
    }
}
int sum(int i)
{
    int s=0;
    while(i>0)
    {
        s+=bit[i];
        i-=(i&(-i));
    }
    return s;
}
int res[MAXN],tmp[MAXN];
void dfs(int u,int fa)
{
    tmp[u]=sum(u-1);
    for(int i=head[u];i!=-1;i=es[i].net)
    {
        int v=es[i].to;
        if(v!=fa)
        {
            add(v,1);
            dfs(v,u);
        }
    }
    res[u]=sum(u-1)-tmp[u];
}
int main()
{
    while(scanf("%d%d",&n,&root)!=EOF&&(n||root))
    {
        memset(head,-1,sizeof(head));
        memset(bit,0,sizeof(bit));
        tot=0;
        for(int i=0;i<n-1;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        dfs(root,-1);
        for(int i=1;i<n;i++)
        {
            printf("%d ",res[i]);
        }
        printf("%d\n",res[n]);
    }
    return 0;
}

 

 

HDU3887(树dfs序列+树状数组)

标签:

原文地址:http://www.cnblogs.com/program-ccc/p/5693006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!