hdu3887 Counting Offspring
Counting Offspring
问你对于每个节点,它的子树上标号比它小的点有多少个
/*
子树的问题,dfs序可以很轻松的解决,因为点在它的子树上,所以在线段树中,必定在它的两个时间戳的区间之间,所以我们只需要从小到大考虑,它的区间里有多少个点已经放了,然后再把它放进去。很容易的解决了
每行的最后一个数后面不要出输出多余的空格,否则会PE
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100010
using namespace std;
int n,p,num,head[maxn],xu[maxn],sz[maxn],opx,opl,opr;
struct node{int to,pre;}e[maxn*];
struct Node{int l,r,v;}tr[maxn<<];
void Insert(int from,int to){
e[++num].to=to;
e[num].pre=head[from];
head[from]=num;
}
void build(int l,int r,int k){
tr[k].l=l;tr[k].r=r;tr[k].v=;
if(l==r)return;
int mid=(l+r)>>;
build(l,mid,k<<);
build(mid+,r,k<<|);
}
int id;
void dfs(int now,int father){
xu[now]=++id;
sz[now]=;
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
if(to==father)continue;
dfs(to,now);
sz[now]+=sz[to];
}
}
int query(int l,int r,int k){
if(l>=opl&&r<=opr){return tr[k].v;}
int mid=(l+r)>>;
int res=;
if(opl<=mid)res+=query(l,mid,k<<);
if(opr>mid)res+=query(mid+,r,k<<|);
return res;
}
void change(int l,int r,int k){
if(l==r){tr[k].v++;return;}
int mid=(l+r)>>;
if(opx<=mid)change(l,mid,k<<);
else change(mid+,r,k<<|);
tr[k].v=tr[k<<].v+tr[k<<|].v;
}
int main(){
freopen("Cola.txt","r",stdin);
while(){
scanf("%d%d",&n,&p);
if(n==&&p==)return ;
memset(head,,sizeof(head));
memset(e,,sizeof(e));num=;
id=;build(,n,);
int x,y;
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
Insert(x,y);Insert(y,x);
}
dfs(p,);
for(int i=;i<=n;i++){
opl=xu[i],opr=xu[i]+sz[i]-;
if(i!=n)printf("%d ",query(,n,));
else printf("%d",query(,n,));
opx=xu[i];
change(,n,);
}
puts("");
}
}
hdu3887 Counting Offspring的更多相关文章
- HDU3887 Counting Offspring [2017年6月计划 树上问题03]
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Counting Offspring(hdu3887)
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 3887 Counting Offspring(DFS序+树状数组)
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 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 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- Hdu 3887 Counting Offspring \ Poj 3321 Apple Tree \BZOJ 1103 [POI2007]大都市meg
这几个题练习DFS序的一些应用. 问题引入: 给定一颗n(n <= 10^5)个节点的有根树,每个节点标有权值,现有如下两种操作: 1.C x y 以节点x的权值修改为y. 2.Q x ...
- 杭电 3887 Counting Offspring
根据上篇翻译的文章以及很多个帖子,都讲述了树状数组最基本的功能就是tree[i]保存的是位置i左边小于等于a[i]的数的个数. 这样也就可以解释代码中为什么有f[i]=getsum(sd[i-1])- ...
- HDU 3887 Counting Offspring (树状数组+人工模拟栈)
对这棵树DFS遍历一遍,同一节点入栈和出栈之间访问的节点就是这个节点的子树. 因此节点入栈时求一次 小于 i 的节点个数 和,出栈时求一次 小于 i 的节点个数 和,两次之差就是答案. PS.这题直接 ...
随机推荐
- POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误
------------------------------------------------------------ 以此题警告自己: 总结, 1.在数组的使用时,一定别忘了初始化 2.在两种情况 ...
- BEC listen and translation exercise 44
But over the past 70 years or so, there's been a massive increase in one type of crime which was wha ...
- python中的yield关键字
yield关键字一直困扰了我很久,一直也没有弄明白,现在将暂时理解的yield记录如下,供参考: 关键词:可迭代对象,生成器,迭代器 一.可迭代对象: 可迭代对象:可迭代对象是一个泛称,只要可以用fo ...
- Leetcode 1002. Find Common Characters
python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] ...
- Linux上用nginx搭建RTMP服务器
参考文章:https://obsproject.com/forum/resources/how-to-set-up-your-own-private-rtmp-server-using-nginx.5 ...
- BurpSuite工具应用及重放攻击实验
一.BurpSuite工具介绍 BurpSuite是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程.所有的工具都共享一个能处理并显示HT ...
- ACM学习历程—HDU4417 Super Mario(树状数组 && 离线)
Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...
- 浅谈Huffman树
所谓Huffman树,就是叶子结点带权的\(K\)叉树,假设每个叶子的权值为\(v\),到根的距离为\(dep\),那么最小化\(\sum v_i*dep_i\)就是\(Huffman\)树的拿手好戏 ...
- long long 与__int64使用总结
本文摘自网络.原文网址:http://blog.sina.com.cn/s/blog_6aa178410100vlwr.html 前言: 在16位环境下,int/unsigned int 占16位,l ...
- hdu 5616 Jam's balance 正反背包+转换
http://acm.hdu.edu.cn/showproblem.php?pid=5616 思路 题目中蕴含着两种需要计算的重量 1. 从所有的砝码中挑出任意种2.(转换的思想)在天平的两端都挑出这 ...