Hdu 3887树状数组+模拟栈
Counting Offspring
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1757 Accepted Submission(s): 582Problem DescriptionYou 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.InputMultiple 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.OutputFor each test case, output n integer in one line representing f(1), f(2) … f(n), separated by a space.Sample Input15 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 0Sample Output0 0 0 0 0 1 6 0 3 1 0 0 0 2 0
/*************************************************************************
> File Name: 3887.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月09日 星期六 14时11分33秒
> Propose:
************************************************************************/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ;
int n, p;
int ans[maxn], c[maxn], tmp[maxn];
bool vis[maxn];
vector<int> g[maxn]; int lowbit(int x) {
return x & -x;
} int sum(int x) {
int res = ;
while (x > ) {
res += c[x];
x -= lowbit(x);
}
return res;
} void add(int x, int v) {
while (x <= n) {
c[x] += v;
x += lowbit(x);
}
} void dfs(int u, int fa) {
tmp[u] = sum(u-);
for (int i = ; i < (int)g[u].size(); i++) {
int v = g[u][i];
if (v == fa) continue;
add(v, );
dfs(v, u);
}
ans[u] = sum(u-) - tmp[u];
} int main(void) {
while (~scanf("%d %d", &n, &p)) {
if (n == && p == ) return ;
for (int i = ; i <= n; i++) g[i].clear();
int x, y;
for (int i = ; i < n; i++) {
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
memset(c, , sizeof(c));
dfs(p, -);
for (int i = ; i <= n; i++)
printf("%d%c", ans[i], i == n ? '\n' : ' ');
}
return ;
}
附上模拟栈的AC代码:
/*************************************************************************
> File Name: 3887.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月09日 星期六 14时11分33秒
> Propose:
************************************************************************/
#include <stack>
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ;
int n, p;
int ans[maxn], c[maxn*], l[maxn], r[maxn], cur[maxn];
int len;
bool vis[maxn];
vector<int> g[maxn];
stack<int> S; int lowbit(int x) {
return x & -x;
} int sum(int x) {
int res = ;
while (x > ) {
res += c[x];
x -= lowbit(x);
}
return res;
} void add(int x, int v) {
while (x <= len) {
c[x] += v;
x += lowbit(x);
}
} void dfs(int u) {
memset(vis, false, sizeof(vis));
memset(cur, , sizeof(cur));
while (!S.empty()) S.pop();
S.push(u);
len = ;
while (!S.empty()) {
int now = S.top();
if (!vis[now]) {
vis[now] = true;
l[now] = ++len;
}
bool flag = false;
for (int& i = cur[now]; i < (int)g[now].size(); i++) {
int v = g[now][i];
if (!vis[v]) {
S.push(v);
flag = true;
break;
}
}
if (flag) continue;
if (vis[now]) {
r[now] = ++len;
S.pop();
}
}
} int main(void) {
while (~scanf("%d %d", &n, &p)) {
if (n == && p == ) return ;
for (int i = ; i <= n; i++) g[i].clear();
int x, y;
for (int i = ; i < n; i++) {
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
memset(c, , sizeof(c));
dfs(p);
for (int i = ; i <= n; i++) {
ans[i] = sum(r[i]-) - sum(l[i]);
add(l[i], );
}
for (int i = ; i <= n; i++)
printf("%d%c", ans[i], i == n ? '\n' : ' ');
}
return ;
}
Hdu 3887树状数组+模拟栈的更多相关文章
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 4777 树状数组+合数分解
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 3887 Counting Offspring (树状数组+人工模拟栈)
对这棵树DFS遍历一遍,同一节点入栈和出栈之间访问的节点就是这个节点的子树. 因此节点入栈时求一次 小于 i 的节点个数 和,出栈时求一次 小于 i 的节点个数 和,两次之差就是答案. PS.这题直接 ...
- HDU 2852 (树状数组+无序第K小)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...
- HDU 4911 (树状数组+逆序数)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...
- hdu 5792(树状数组,容斥) World is Exploding
hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...
- HDU 1934 树状数组 也可以用线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...
- 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...
- 【模板】HDU 1541 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...
随机推荐
- Delphi-DLL远程注入
1. 代码描述 枚举进程,然后向指定进程注入DLL 在被注入的进程窗口按下指定的键码值(#HOME),显示或者隐藏被注入的DLL窗口 未解决的问题: 卸载DLL DLL向exe发送消息 卸载键盘钩子 ...
- Python学习day24-面向对象的三大特征之继承
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Tomcat Add and Remove 项目时提示 Project facet Java version 1.8 is not supported 错误
原因:项目的jdk和tomcat的jdk版本不同 将eclipse-preference-server-runtime environments 点击你要用的tomcat 点击 edit-jre选择和 ...
- java开发系列-Http协议
概述 HTTP(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.这种协议用来规定通信数据的格式. HTTP请求 浏览器往服务器发送数据称之为请求.HTTP ...
- HBase 概念视图
- [转]浅谈C#中常见的委托
一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...
- Gym - 102021E
Gym - 102021Ehttps://vjudge.net/problem/2109787/origin主要是一个处理精度的技巧,避免精度误差可以加eps,然后乘1e(小数点之后的位数). #in ...
- odoo xml 时间搜索条件
今年 <filter string="This Year" name="year" domain="[('date','<=', time ...
- 如何使用Tunnel SDK上传/下载MaxCompute复杂类型数据
基于Tunnel SDK如何上传复杂类型数据到MaxCompute?首先介绍一下MaxCompute复杂数据类型: 复杂数据类型 MaxCompute采用基于ODPS2.0的SQL引擎,丰富了对复杂数 ...
- HZOI20190823 C magic
数论板子合集... 我们要求: $N^{\sum\limits_{i=1}^{N}[gcd(i,N)==1]C_{n}^{i}}mod p$ 其中p为54184622,是个合数 指数是组合数,不能用快 ...