题目链接

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. PAT甲级——A1085 Perfect Sequence

    Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...

  2. eval(str)函数

    转载:地址于http://blog.csdn.net/SeeTheWorld518/article/details/47983511 eval(str)函数很强大,官方解释为:将字符串str当成有效的 ...

  3. Django 连接MySQL的驱动设置

    对于在Django 中连接MySQL 的驱动,有以下三种: 1) mysqlclient 2) mysql-connector-python 3) pymysql (建议:这个包已经有一年未升级了,本 ...

  4. Netty TCP粘包/拆包问题《一》

    1.使用LineBasedFrameDecoder,StringDecoder解析器进行解决TCP粘包/拆包问题 2.代码搞起: TimeClient:客户端 /* * Copyright 2013- ...

  5. Java基础知识(面试问题1)

    1.什么是 GC?为什么要有 GC?GC(Garbage Collection)是垃圾收集的意思,负责清除对象并释放内存.Java 提供的 GC 功能可以自动检测对象是否超过作用域从而达到自动回收内存 ...

  6. 利用animate.css和es6制作文字向上滚动的效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel= ...

  7. 廖雪峰Java11多线程编程-1线程的概念-2创建新线程

    Java语言内置多线程支持: 一个Java程序实际上是一个JVM进程 JVM用一个主线程来执行main()方法 在main()方法中又可以启动多个线程 1.创建新线程 1.1 方法一:使用Thread ...

  8. MyBatis-Spring(四)--MapperFactoryBean实现增删改查

    上一篇文章中提到,使用SqlSessionTemplat时需要输入一长串字符串来获取mapper,这种方式IDE不会检查程序的准确性并且很容易出错,所以这篇文章介绍另一种可以避免这种问题,并且也可以使 ...

  9. PAT甲级——A1062 Talent and Virtue

    About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...

  10. OCP/OCA Oracle 学习001

    select * from TEST3 t select object_type, count(object_type) from user_objects group by object_type ...