Counting Offspring

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3450 1166 3030 1541 3743 
 
//====================================
被格式错误卡了半个小时
开始多了空格,去了空格,不对
后来发现多组数据要加空行,然后在数据之间加了空行,不对
看了看题解才发现末尾要多一个空行
 
竟无语凝噎
//====================================
 
跟树状数组求逆序对的思想类似,大家可以去看那一道题的思路
 
#include <bits/stdc++.h>

inline void read(int &x)
{
char ch = getchar();char c = ch;x = 0;
while(ch < '0' || ch > '9')c = ch, ch = getchar();
while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
if(c == '-')x = -x;
}
inline int lowbit(int &a){return a & (-a);}
const int MAXN = 500000 + 10; int n,root,tmp1,tmp2; struct Edge{int u,v,next;}edge[MAXN << 1];
int head[MAXN], cnt, l[MAXN << 1], r[MAXN << 1], bit[MAXN << 1];
inline void insert(int a, int b){edge[++cnt] = Edge{a,b,head[a]};head[a] = cnt;}
int b[MAXN], stack[MAXN], top, rank; void dfs(int root)
{
register int u,v,pos;
stack[++top] = root;
b[root] = 1;
while(top)
{
u = stack[top--];
if(l[u])
{
r[u] = ++rank;
continue;
}
stack[++top] = u;
l[u] = ++rank;
for(pos = head[u];pos;pos = edge[pos].next)
{
v = edge[pos].v;
if(b[v])continue;
b[v] = true;
stack[++top] = v;
}
}
} inline void modify(int p, int k)
{
register int tmp = n << 1;
for(;p <= tmp;p += lowbit(p))
bit[p] += k;
} inline int ask(int p)
{
register int ans = 0;
for(;p;p -= lowbit(p))
ans += bit[p];
return ans;
} bool ok;
int main()
{
while(true)
{
read(n);read(root);
if(!(n || root))break;
memset(edge, 0, sizeof(edge));
memset(head, 0, sizeof(head));
memset(l, 0, sizeof(l));
memset(r, 0, sizeof(r));
cnt = 0;
memset(bit, 0, sizeof(bit));
memset(b, 0, sizeof(b));
memset(stack, 0, sizeof(stack));
top = 0;
rank = 0;
register int i;
for(i = 1;i < n;++ i)
{
read(tmp1);read(tmp2);
insert(tmp1, tmp2);
insert(tmp2, tmp1);
}
dfs(root);
printf("%d", ask(r[1]) - ask(l[1] - 1));
modify(l[1], 1);
for(i = 2;i <= n;i ++)
{
printf(" %d", ask(r[i]) - ask(l[i] - 1));
modify(l[i], 1);
}
printf("\n");
}
return 0;
}
 

HDU3887 Counting Offspring [2017年6月计划 树上问题03]的更多相关文章

  1. 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]

    P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...

  2. ZOJ3195 Design the city [2017年6月计划 树上问题04]

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  3. 洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]

    [POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byte ...

  4. 洛谷P1774 最接近神的人_NOI导刊2010提高(02) [2017年6月计划 线段树03]

    P1774 最接近神的人_NOI导刊2010提高(02) 题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门 ...

  5. 【CodeVS】2822 爱在心中 [2017年6月计划 强连通分量03]

    2822 爱在心中 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond         题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够 ...

  6. hdu3887 Counting Offspring

    Counting Offspring HDU - 3887 问你对于每个节点,它的子树上标号比它小的点有多少个 /* 子树的问题,dfs序可以很轻松的解决,因为点在它的子树上,所以在线段树中,必定在它 ...

  7. RQNOJ PID192 梦幻大PK [2017年6月计划 二分图02]

    PID192 / 梦幻大PK ☆ 提交你的代码 查看讨论和题解 你还木有做过哦 我的状态         查看最后一次评测记录 质量 7 题目评价 质量 7 ★★★★★ ★★★★☆ ★★★☆☆ ★★☆ ...

  8. 洛谷P1368 均分纸牌(加强版) [2017年6月计划 数论14]

    P1368 均分纸牌(加强版) 题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,纸牌总数必为 N 的倍数.可以在任一堆上取1张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取 ...

  9. 洛谷P1621 集合 [2017年6月计划 数论13]

    P1621 集合 题目描述 现在给你一些连续的整数,它们是从A到B的整数.一开始每个整数都属于各自的集合,然后你需要进行一下的操作: 每次选择两个属于不同集合的整数,如果这两个整数拥有大于等于P的公共 ...

随机推荐

  1. L2-006 树的遍历 (层序遍历)

    根据访问根节点与左右子树的先后顺序,二叉树一般有三种遍历方式:先序遍历.中序遍历和后序遍历. 只要给定中序遍历序列与先序或后序中的一种,可以还原二叉树结构.学习数据结构课程时,一直都只会手动构建还原二 ...

  2. Python学习day18-常用模块之NumPy

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  3. python学习笔记3.1_数据读取常用函数参数

    一.read_table/read_csv常用函数参数 1.path:表明文件系统位置的字符串.url或文件型对象 2.sep或delimiter:用于分隔每行字段的字符序列或正则表达式 3.head ...

  4. docker中使用源码方式搭建SRS流媒体服务

    一.背景 搭建流媒体服务的方式一般会采用nginx+rtmp和srs服务两种,前者是nginx加上插件所用,而后者是专门为了为了流媒体而生,在这一节中我们将从头搭建srs流媒体服务 二. 运行环境 为 ...

  5. matlab-选择-循环-函数

    1 选择 3 循环 break 3 函数

  6. Python学习之--python概要

    1 Python的优点 Python语言类库齐全,语法简洁,而且在linux上自带安装,在处理大数据以及自动化方面有其独有的特点.2 Python的解释器 Python解释器用来解释python代码, ...

  7. Ubuntu 卸载nvidia驱动

    1.切换为集成显卡 如果没有,那么先切换到字符界面 2.卸载驱动 sudo apt-get --purge remove nvidia* sudo apt autoremove To remove C ...

  8. 2019-8-31-dotnet-启动-JIT-多核心编译提升启动性能

    title author date CreateTime categories dotnet 启动 JIT 多核心编译提升启动性能 lindexi 2019-08-31 16:55:58 +0800 ...

  9. 如何在终端编译C++代码

    C++语言从编写-->执行整个过程.一般来讲,开发一个C++程序需要经过以下几步1. 编写代码,2. 编译器进行编译,compile    生成.o的可执行的二进制目标文件3. 连接器进行连接. ...

  10. [CQOI2009]叶子的染色【性质+树形Dp】

    Online Judge:Bzoj1304,Luogu P3155 Label:无根树,树形Dp 题目描述 给定一棵\(N\)个节点的无根树,它一共有\(K\)个叶子节点.你可以选择一个度数大于1的节 ...