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;
}
 

最新文章

  1. GCD的简单用法
  2. 技术之余。。。电吉他自弹 魂斗罗 solo
  3. nagios二次开发(一)---开发思想
  4. Windows Azure Cloud Service (43) 使用Azure In-Role Cache缓存(2)Dedicated Role
  5. 如何利用SmartGit将一个已经写好的项目push到github
  6. Leetcode 225 Implement Stack using Queues STL
  7. xcode命令行编译时:codesign命令,抛出“User interaction is not allowed.”异常 的处理
  8. 基于APK的Robotium登录人人网与发状态
  9. DEDE调用频道封面{dede:field:content/}内容方法
  10. Watch OS2.0开发概述
  11. Sum 类型题目总结
  12. ios ViewController present不同的方向
  13. HttpLuaModule——翻译(Nginx API for Lua) (转)
  14. 接水问题【NOIP2010普及组】优先队列
  15. Linux中的定时任务at、crontab
  16. MySQL命令,一篇文章替你全部搞定
  17. A1126. Eulerian Path
  18. Listener(1)—基础知识
  19. latex之行内公式与行间公式
  20. Docker虚拟化平台

热门文章

  1. Box &#39;laravel/homestead&#39; could not be found.
  2. Redis深度历险——核心原理与应用实践
  3. express 4 使用session和cookies
  4. 软件工程(C编码实践篇)学习总结
  5. L2-006 树的遍历 (层序遍历)
  6. Js中获取时间 new date()的用法
  7. Ubuntu18上安装Go和GoLand
  8. Cmp- Linux必学的60个命令
  9. DataSourceUtils(使用C3P0连接池的工具类)
  10. linux系统下重要的分区及其作用