HDU3887 Counting Offspring [2017年6月计划 树上问题03]
Counting Offspring
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2809 Accepted Submission(s): 981
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.
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
#include <bits/stdc++.h> inline void read(int &x)
{
char ch = getchar();char c = ch;x = 0;
while(ch < '0' || ch > '9')c = ch, ch = getchar();
while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
if(c == '-')x = -x;
}
inline int lowbit(int &a){return a & (-a);}
const int MAXN = 500000 + 10; int n,root,tmp1,tmp2; struct Edge{int u,v,next;}edge[MAXN << 1];
int head[MAXN], cnt, l[MAXN << 1], r[MAXN << 1], bit[MAXN << 1];
inline void insert(int a, int b){edge[++cnt] = Edge{a,b,head[a]};head[a] = cnt;}
int b[MAXN], stack[MAXN], top, rank; void dfs(int root)
{
register int u,v,pos;
stack[++top] = root;
b[root] = 1;
while(top)
{
u = stack[top--];
if(l[u])
{
r[u] = ++rank;
continue;
}
stack[++top] = u;
l[u] = ++rank;
for(pos = head[u];pos;pos = edge[pos].next)
{
v = edge[pos].v;
if(b[v])continue;
b[v] = true;
stack[++top] = v;
}
}
} inline void modify(int p, int k)
{
register int tmp = n << 1;
for(;p <= tmp;p += lowbit(p))
bit[p] += k;
} inline int ask(int p)
{
register int ans = 0;
for(;p;p -= lowbit(p))
ans += bit[p];
return ans;
} bool ok;
int main()
{
while(true)
{
read(n);read(root);
if(!(n || root))break;
memset(edge, 0, sizeof(edge));
memset(head, 0, sizeof(head));
memset(l, 0, sizeof(l));
memset(r, 0, sizeof(r));
cnt = 0;
memset(bit, 0, sizeof(bit));
memset(b, 0, sizeof(b));
memset(stack, 0, sizeof(stack));
top = 0;
rank = 0;
register int i;
for(i = 1;i < n;++ i)
{
read(tmp1);read(tmp2);
insert(tmp1, tmp2);
insert(tmp2, tmp1);
}
dfs(root);
printf("%d", ask(r[1]) - ask(l[1] - 1));
modify(l[1], 1);
for(i = 2;i <= n;i ++)
{
printf(" %d", ask(r[i]) - ask(l[i] - 1));
modify(l[i], 1);
}
printf("\n");
}
return 0;
}
HDU3887 Counting Offspring [2017年6月计划 树上问题03]的更多相关文章
- 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]
P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...
- ZOJ3195 Design the city [2017年6月计划 树上问题04]
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]
[POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byte ...
- 洛谷P1774 最接近神的人_NOI导刊2010提高(02) [2017年6月计划 线段树03]
P1774 最接近神的人_NOI导刊2010提高(02) 题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门 ...
- 【CodeVS】2822 爱在心中 [2017年6月计划 强连通分量03]
2822 爱在心中 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够 ...
- hdu3887 Counting Offspring
Counting Offspring HDU - 3887 问你对于每个节点,它的子树上标号比它小的点有多少个 /* 子树的问题,dfs序可以很轻松的解决,因为点在它的子树上,所以在线段树中,必定在它 ...
- RQNOJ PID192 梦幻大PK [2017年6月计划 二分图02]
PID192 / 梦幻大PK ☆ 提交你的代码 查看讨论和题解 你还木有做过哦 我的状态 查看最后一次评测记录 质量 7 题目评价 质量 7 ★★★★★ ★★★★☆ ★★★☆☆ ★★☆ ...
- 洛谷P1368 均分纸牌(加强版) [2017年6月计划 数论14]
P1368 均分纸牌(加强版) 题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,纸牌总数必为 N 的倍数.可以在任一堆上取1张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取 ...
- 洛谷P1621 集合 [2017年6月计划 数论13]
P1621 集合 题目描述 现在给你一些连续的整数,它们是从A到B的整数.一开始每个整数都属于各自的集合,然后你需要进行一下的操作: 每次选择两个属于不同集合的整数,如果这两个整数拥有大于等于P的公共 ...
随机推荐
- VS2010-MFC(MFC应用程序框架分析)
转自:http://www.jizhuomi.com/software/145.html 一.SDK应用程序与MFC应用程序运行过程的对比 程序运行都要有入口函数,在之前的C++教程中都是main函数 ...
- Quartz:Quartz
ylbtech-Quartz:Quartz Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Qu ...
- <每日一题>题目15:mysql创建表及相关约束
题目: 解答: 第一个表创建: create table class( cid int not null auto_increment primary key, caption char(20) no ...
- Mac下使用Pecl安装PHP的Swoole扩展实践
一.背景 前段时间把Mac系统重装了,PHP的一些扩展都没了,昨天需要调试一个swoole开发的项目,发现命令行中的PHP是系统自带的,如果安装swoole扩展很不方便:需要自己手动去下载swoole ...
- jmeter参数化之用户参数
1. 用badboby进行录制,录制完成后保存,用JMeter格式进行保存,如:登陆.jmx 2. 在jmeter中打开保存的文件登陆.jmx. 3.在step1上右击-添加-前置处理 ...
- Spring Boot配置公共的线程池
内存资源很宝贵,线程池资源不宜过多的创建,同一个应用,尽量使用统一的线程池,并且相关参数需要设置适当,不造成资源的浪费,也不影响性能的提升. import java.util.concurrent.T ...
- 记录下sparkStream的做法(scala)
一直用storm做实时流的开发,之前系统学过spark但是一直没做个模版出来用,国庆节有时间准备做个sparkStream的模板用来防止以后公司要用.(功能模拟华为日常需求,db入库hadoop环境) ...
- 2019-10-31-C#-dotnet-获取整个局域网的-ip-地址
title author date CreateTime categories C# dotnet 获取整个局域网的 ip 地址 lindexi 2019-10-31 08:57:55 +0800 2 ...
- bzoj1003物流运输 最短路+DP
bzoj1003物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输 ...
- LUOGU NOIP 2018 模拟赛 DAY1
T1 传送门 解题思路 这似乎是小学数学知识???mod 9就相当于各位之和mod 9,打表求了个逆元,等差数列求和公式就行了. #include<iostream> #include&l ...