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. Joomla - T3模板(非常好用的4屏响应式模板)

    一.下载 T3 模板 下载地址(需要注册登录才能下载):https://www.joomlart.com/member/downloads/joomlart/t3-framework/t3-blank ...

  2. Git婴幼儿使用手册【十分钟让你帅气的使用命令行和团队工作】

    Git由来:...... Git使用的好处:...... 如何使用Git:(以上会显得我们以下的是很纯纯的干货) 代码库有两个部分: 本地代码库:远程代码库: 本地代码库使用方法: 一.先创建一个文件 ...

  3. iOS之UIButton的normal和selected状态切换

    1.问题? 改变播放器按钮状态,发现从selected切回到normal状态时候,中间会切换normal-selected-normal的图片? 2.原因…… normal-->selected ...

  4. 使用Eclipse的Working Set管理项目

    想必大家的Eclipse里也会有这么多得工程...... 每次工作使用到的项目肯定不会太多...... 每次从这么大数量的工程当中找到自己要使用的, 必须大规模的滚动滚动条......有点不和谐了. ...

  5. SpringIOC注入模块中xml文件导入其他xml文件配置

    如果我们在spring框架中配置了多个xml文件,我们可以在读取配置文件的时候把这些xml文件一下全都读取 也可以只读一个总的xml文件,在这个总的xml文件中把其他的xml全都都导入进来. 例如: ...

  6. 08-2-if的其他写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 设置listContrl中指定行的颜色

    在MFC中 自己通过手动拖放CListCtrl控件来制作自己的表格: 目的: 将指定item的行更该颜色: 步骤: 1,在窗口中拖放CListCtrl控件, 单击右键 创建控件对象: CListCtr ...

  8. Python中的sort()

    Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可 ...

  9. 基于宜搭的《T恤尺码收集》应用搭建

    简介: 在阿里,T恤是程序员必不可少的元素.每逢公司或者BU(部门)的重大节庆日,比如双11 .年会.新BU成立仪式.大型活动等,都会给员工定制发放统一的T恤或者POLO衫服装.而我们每次发放T恤之前 ...

  10. skyline(TG,arcgis server)BS系统部署

    skyline的BS系统部署,正常情况下应该是TG来统一管理,SFS对矢量数据服务进行管理.但我们一直是试用许可安装的TG,发现SFS要么安装不成功,要么就是不稳定.对于Fly工程可以通过Publis ...