题目链接

Counting Offspring

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1757    Accepted Submission(s): 582

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
可以直接dfs(需要手工加栈)
Accepted Code:
 /*************************************************************************
> 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树状数组+模拟栈的更多相关文章

  1. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. HDU 3887 Counting Offspring (树状数组+人工模拟栈)

    对这棵树DFS遍历一遍,同一节点入栈和出栈之间访问的节点就是这个节点的子树. 因此节点入栈时求一次 小于 i 的节点个数 和,出栈时求一次 小于 i 的节点个数 和,两次之差就是答案. PS.这题直接 ...

  4. HDU 2852 (树状数组+无序第K小)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...

  5. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

  6. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  7. HDU 1934 树状数组 也可以用线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...

  8. 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 ...

  9. 【模板】HDU 1541 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...

随机推荐

  1. day 42 01--CSS的引入方式及CSS选择器

    01--CSS的引入方式及CSS选择器   本节目录 一 CSS介绍 二 行内样式 三 内接样式 四 外接样式 五 CSS的选择器 六 CSS的高级选择器 七 CSS的属性选择器 八 CSS的伪类选择 ...

  2. js时间操作getTime(),ios移动端真机上返回显示NAN

    ios移动端,js时间操作getTime(),getFullYear()等返回显示NaN的解决办法及原因 在做移动端时间转化为时间戳时,遇到了一个问题,安卓手机上访问时,能拿到时间戳,从而正确转换时间 ...

  3. C#去掉字符串两端空格以及去掉字符串中多余空格保留一个空格

    string str = " asdf asd saddf sdfwrqeqw a asdf "; string[] strs = str.Trim().Split(new cha ...

  4. vue 关闭微信浏览器(返回路由为undefined时)

    参考:https://blog.csdn.net/KingJin_CSDN_/article/details/77050569 main.js: import router from './route ...

  5. 小程序 设置tabBar选中颜色和图标

    "tabBar": { "selectedColor": "#4da9ff", //颜色 "list": [{ &quo ...

  6. 2018-8-10-WPF-鼠标移动到列表上-显示列表图标

    title author date CreateTime categories WPF 鼠标移动到列表上 显示列表图标 lindexi 2018-08-10 19:16:51 +0800 2018-2 ...

  7. centos Python2.6 升级到2.7

    需求: centos 6.x 系统默认Python 版本都是2.6,实际生产环境中需要用到Python 2.7.x Python 2.7 下载地址 [root@ansible package]# wg ...

  8. 深入浅出 Java Concurrency (18): 并发容器 part 3 ConcurrentMap (3)[转]

    在上一篇中介绍了HashMap的原理,这一节是ConcurrentMap的最后一节,所以会完整的介绍ConcurrentHashMap的实现. ConcurrentHashMap原理 在读写锁章节部分 ...

  9. 利用TensorFlow识别手写的数字---基于Softmax回归

    1 MNIST数据集 MNIST数据集主要由一些手写数字的图片和相应的标签组成,图片一共有10类,分别对应从0-9,共10个阿拉伯数字.原始的MNIST数据库一共包含下面4个文件,见下表. 训练图像一 ...

  10. Xcode10 代码块(Code Snippet)添加和删除

    https://blog.csdn.net/lg767201403/article/details/82761448 Code Snippets 使用 https://blog.csdn.net/lv ...