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的公共 ...
随机推荐
- Java-MyBatis-MyBatis3-XML映射文件:自动映射
ylbtech-Java-MyBatis-MyBatis3-XML映射文件:自动映射 1.返回顶部 1. 自动映射 正如你在前面一节看到的,在简单的场景下,MyBatis 可以为你自动映射查询结果.但 ...
- Lucene 的 Field 域和索引维护
一.Field 域 1.Field 属性 Field 是文档中的域,包括 Field 名和 Field 值两部分,一个文档可以包括多个 Field,Document 只是 Field 的一个承载体,F ...
- Docker在线文档收集
极客学院 kubernetes中文社区 易百教程
- Java(8)中List的遍历方式总结
本篇文章主要讲述了List这一集合类型在Java,包括Java8中的遍历方式,不包括其他的过滤,筛选等操作,这些操作将会在以后的文章中得到提现,由List可以类推到Set等类似集合的遍历方式. pub ...
- IDEA中log4j 无法输出到本地,properties配置无效问题。
log4j添加以后无法输出日志信息,经检查(按以下顺序): 1.jar包导入正常 2.log4j.properties配置文件正常 3.logger.info可以输出,但是properties文件无效 ...
- PKUOJ 区间内的真素数
http://bailian.openjudge.cn/tm2018/A/ #include <iostream> #include <math.h> #include < ...
- python使用suds来调用webservice
对于python仅作为客户端调用webservice的情况,推荐使用suds库来完成,比起zsi,soapy之类,它可以说是相当轻量级,使用非常方便. 安装suds建议使用easy_insall来做. ...
- vue.js_11_路由的2中参数传递和路由的嵌套
1.以?的形式传递参数 <router-link to="/login?id=10&name=zs">登录</router-link> 发送参数 ...
- vue.js_10_vue的路由
1.vue-router的基本使用 1>安装vue-rouder路由模块 <script src="js/vue-2.4.0.js"></script> ...
- React State&生命周期
State&生命周期 State&生命周期 到目前为止我们只学习了一种方法来更新UI. 我们调用ReactDOM.render()来改变输出: function tick(){ con ...