HDU 3887 Counting Offspring
2016-08-29 13:19
501 查看
Counting Offspring
[align=left]Problem Description[/align]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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, output n integer in one line representing f(1), f(2) … f(n), separated by a space.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
0 0 0 0 0 1 6 0 3 1 0 0 0 2 0
#include<cstdio> #include<vector> #include<cstring> #include<algorithm> #define pb push_back using namespace std; const int N=1e5+5; int n,rt,dfs_clock,L ,R ,sum ,ans ; vector<int>G ; inline void init() { memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++)G[i].clear(); dfs_clock=0; } void dfs(int u,int fa) { L[u]=++dfs_clock; for(int len=G[u].size(),i=0;i<len;i++) { int v=G[u][i]; if(v==fa)continue; dfs(v,u); } R[u]=dfs_clock; } inline void add(int pos) { for(int i=pos;i<=n;i+=i&(-i)) sum[i]+=1; } inline int query(int pos) { int res=0; for(int i=pos;i>0;i-=i&(-i)) res+=sum[i]; return res; } int main() { while(scanf("%d%d",&n,&rt),n|rt) { init(); for(int _=0;_<n-1;_++) { int u,v; scanf("%d%d",&u,&v); G[u].pb(v),G[v].pb(u); } dfs(rt,-1); for(int i=1;i<=n;i++) { ans[i]=query(R[i])-query(L[i]-1); add(L[i]); } for(int i=1;i<=n;i++) printf(i==n?"%d\n":"%d ",ans[i]); } return 0; }
相关文章推荐
- 如何在spring security避免登录页面素材文件被禁止加载
- JAVA基础(001_Collection与Collections的区别)
- eclipse优化
- CXF 和Spring 整合,客户端代码如何调用举例说明
- java 比较两对象的方法
- java MD5校验
- java身份证合法性校验并获取并根据身份证号提取身份证相关信息
- java设计模式之建造者模式(生成器模式)最好的例子。
- Java工具类实现校验公民身份证的有效性
- spring4+mybatis3+velocity+mysql+gradle
- 搭建web项目结合spring+cxf的webservice服务
- Java回调函数
- java spring框架学习总结
- CXF之用spring配置服务端和客户端实例(转)
- Spring-Security (学习记录一)--登录
- Java如何查看死锁?
- Myeclipse 2014破解教程
- spring mvc中jsp不能访问的原因
- struts不可直接方式jsp的web.xml配置
- java的位运算符(>>,<<,>>>,&,|)