hdu3887 Counting Offspring
来源:互联网 发布:淘宝立即购买按钮代码 编辑:程序博客网 时间:2024/05/28 08:29
Counting Offspring
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1148 Accepted Submission(s): 406
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.
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 77 107 17 97 37 410 1414 214 139 119 66 56 83 153 120 0
Sample Output
0 0 0 0 0 1 6 0 3 1 0 0 0 2 0
Author
bnugong
Source
2011 Multi-University Training Contest 5 - Host by BNU
Recommend
lcy
照样是带时间戳的树状数组,记得去年多校的时候宣姐死活YY不出来……orz,然后我提供了思路,但是不会写,今天终于把他写完了。
注意直接深搜会爆栈,于是乎学了下栈模拟递归的写法……orz。
代码
#include <stdio.h>#include <string.h>#include <stack>#include <vector>#include <algorithm>using namespace std;stack <int> s;int vis[100005];int in[100005];int ans[100005];int n;vector <int> v[100005];int lowbit(int t){ return t&(-t);}int Query(int t){ int sum=0; while(t>0) { sum+=in[t]; t-=lowbit(t); } return sum;}void Add(int t,int val){ while(t<=n) { in[t]+=val; t+=lowbit(t); }}void DFS(int t){ int tag; s.push(t); while(!s.empty()) { tag=s.top(); if (!vis[tag]) { vis[tag]=1; ans[tag]=Query(tag); } else if (v[tag].size()!=0) { if (vis[v[tag].back()]==0) s.push(v[tag].back()); v[tag].pop_back(); } else { ans[tag]=Query(tag)-ans[tag]; s.pop(); Add(tag,1); } }}int main(){ int m,i,j,T,x,y; while(1) { scanf("%d%d",&n,&m); if (n==0 && m==0) break; for (i=0;i<=n;i++) { v[i].clear(); } for (i=0;i<n-1;i++) { scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } memset(in,0,sizeof(in)); memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); DFS(m); for (i=1;i<n;i++) { printf("%d ",ans[i]); } printf("%d\n",ans[i]); } return 0;}
- hdu3887 Counting Offspring
- Counting Offspring(hdu3887)
- Counting Offspring(hdu3887)
- Counting Offspring(hdu3887)
- Counting Offspring(hdu3887)
- Counting Offspring(hdu3887)
- HDU3887 Counting Offspring
- 线段树 HDU3887 Counting Offspring
- HDU3887 Counting Offspring【dfs序】
- HDU3887 - Counting Offspring(树状数组+模拟栈)
- hdu3887 Counting Offspring(dfs序+树状数组)
- hdu3887-dfs序&树状数组-Counting Offspring
- hdu3887 Counting Offspring(dfs序+树状数组)
- hdu 3887 Counting Offspring
- HDU 3887 Counting Offspring
- hdu 3887 Counting Offspring
- 杭电 3887 Counting Offspring
- HDU 3887 Counting Offspring 树状数组
- 层跟随鼠标移动
- MyEclipse卡死的解决方案
- 关闭浏览器输入框自动补齐,兼容IE,FF,Chrome等
- MyEclipse快捷键、提示键、关键字颜色等配置
- jspsmart实现文件上传下载及jspSmartUpload.jar下载
- hdu3887 Counting Offspring
- Gridview过长字段截取显示
- ios(iphone)开发环境的配置
- GridView控件编辑、更新、删除示例(更新包含有DropDownList控件使用)(图)
- 基于JavaReport的Web报表开发实例
- 关于笔记本电脑开机输密码时关闭小键盘的方法 怎样关闭笔记本小键盘
- SQL语句查看SQL Server版本号
- GridView多行表头合并
- GridView导出Excel(解决Excel空白没有网格)