HDU 3887 Counting Offspring(DFS序+树状数组)
Counting Offspring
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3054 Accepted Submission(s): 1031
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.
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.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#define ll long long
#define eps 1e-10
#define LL unsigned long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=+;
const int mod=1e9+;
int head[N];
int tot,time;
int L[N],R[N];
struct node{
int to,next;
}edge[N<<];
int a[N*];
void init(){
memset(head,-,sizeof(head));
tot=;
time=;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void DFS(int x,int fa){
L[x]=++time;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==fa)continue;
DFS(v,x);
}
R[x]=time; }
int lowbit(int x){
return x&(-x);
}
void update(int x,int y){
while(x<=time){
a[x]=a[x]+y;
x=x+lowbit(x);
}
}
int getsum(int x){
int ans=;
while(x>){
//cout<<2<<endl;
ans=ans+a[x];
x=x-lowbit(x);
}
return ans;
}
int main(){
int n,p;
while(scanf("%d%d",&n,&p)!=EOF){
if(n==&&p==)break;
init();
memset(a,,sizeof(a));
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
DFS(p,-);
for(int i=;i<=n;i++){
int ans=getsum(R[i])-getsum(L[i]-);
if(i!=n)cout<<ans<<" ";
else{
cout<<ans<<endl;
}
update(L[i],);
}
}
}
HDU 3887 Counting Offspring(DFS序+树状数组)的更多相关文章
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 5877 Weak Pair dfs序+树状数组+离散化
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Prob ...
- 刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)
题目: Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There ar ...
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...
- 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组
题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
随机推荐
- Linux下清空文件的常用方法
1. 本人用的最多,感觉也是最方便的: > filename.log 2. : > filename.log 3. cat /dev/null > filename.log
- Cookie的实现
Cookie是web server下发给浏览器的任意的一段文本,在后续的http 请求中,浏览器会将cookie带回给Web Server.同时在浏览器允许脚本执行的情况下,Cookie是可以被Jav ...
- Redis 之仿微博demo
一.用户注册登录 include './header.php'; include './function.php'; $username = p('username'); $password = p( ...
- CAD动态绘制样条线(com接口)
主要用到函数说明: _DMxDrawX::SendStringToExecuteFun 把命令当着函数执行,可以传参数.详细说明如下: 参数 说明 IDispatch* pParam 命令参数,IMx ...
- vue中fetch请求
1. 请求方式:get 请求参数:menuName 返回的结果:data created(){ this._initPageData() }, methods:{ _initPageData(){ f ...
- 企业级mysql数据库完全备份、增量备份脚本
企业完全备份脚本 [root@client ~]# vim /opt/mysql_bak_wanbei.sh #!/bin/bash #MySQL数据库完全备份脚本 #设置登录变量 MY_USER=& ...
- pig常用命令
一.pig: pig提供了一个基于Hadoop的并行地执行数据流处理的引擎.它包含了一种脚本语言,称为Pig Latin.(类似SQL) 二.Pig Latin: 1.注释: 单行:-- 多行:/* ...
- https webservice通讯 参考网址 http://blog.csdn.net/small____fish/article/details/8214938
一.生成密钥库和证书可参考以下密钥生成脚本,根据实际情况做必要的修改,其中需要注意的是:服务端的密钥库参数“CN”必须与服务端的IP地址相同,否则会报错,客户端的任意. 1.生成服务器证书库keyto ...
- 洛谷——P1196 [NOI2002]银河英雄传说
P1196 [NOI2002]银河英雄传说 题目大意: 给你一个序列,支持两种操作: 合并指令为$M_{i,j}$j,含义为第i号战舰所在的整个战舰队列,作为一个整体(头在前尾在后)接至第j号战舰所 ...
- uva 133(The Dole Queue UVA - 133)
一道比较难想的模拟题,用了队列等东西,发现还是挺难做的,索性直接看了刘汝佳的代码,发现还是刘汝佳厉害! 代码本身难度并不是很大,主要还是p=(p+n+d-1)%n+1;这一句有些难度,实际上经过自己的 ...