BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆
BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆
Description
Sample Input
1 4
2 3
1 5
Sample Output
3
2
1
1
做法不唯一,这里用来练习可并堆。
先求出每个点$i$ 到根路径上的长度$dis[i]$ ,对每个点建一个可并堆(大根)。
然后从下往上合并,如果当前$dis[堆顶]-dis[x]>L$ 就弹出,记录每个节点最后剩下的点数即可。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 200050
typedef long long ll;
ll val[N<<1],L,v[N];
int head[N],to[N<<1],nxt[N<<1],cnt,n,root[N],ls[N],rs[N],dis[N],siz[N];
inline void add(int u,int v,ll w) {
to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; val[cnt]=w;
}
int merge(int x,int y) {
if(!x) return y;
if(!y) return x;
if(v[x]<v[y]) swap(x,y);
rs[x]=merge(rs[x],y);
if(dis[ls[x]]<dis[rs[x]]) swap(ls[x],rs[x]);
dis[x]=dis[rs[x]]+1;
return x;
}
void dfs(int x,int y) {
int i;
siz[x]=1; root[x]=x;
for(i=head[x];i;i=nxt[i]) {
if(to[i]!=y) {
v[to[i]]=v[x]+val[i];
dfs(to[i],x);
siz[x]+=siz[to[i]];
root[x]=merge(root[x],root[to[i]]);
}
}
while(v[root[x]]-v[x]>L) {
siz[x]--; root[x]=merge(ls[root[x]],rs[root[x]]);
}
}
int main() {
dis[0]=-1;
scanf("%d%lld",&n,&L);
int i,x;
ll y;
for(i=2;i<=n;i++) {
scanf("%d%lld",&x,&y);
add(i,x,y); add(x,i,y);
}
dfs(1,0);
for(i=1;i<=n;i++) {
printf("%d\n",siz[i]);
}
}
BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆的更多相关文章
- BZOJ 3011: [Usaco2012 Dec]Running Away From the Barn( dfs序 + 主席树 )
子树操作, dfs序即可.然后计算<=L就直接在可持久化线段树上查询 -------------------------------------------------------------- ...
- 【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn 可并堆
[BZOJ3011][Usaco2012 Dec]Running Away From the Barn Description It's milking time at Farmer John's f ...
- [BZOJ3011][Usaco2012 Dec]Running Away From the Barn
题意 给出一棵以1为根节点树,求每个节点的子树中到该节点距离<=l的节点的个数 题解 方法1:倍增+差分数组 首先可以很容易的转化问题,考虑每个节点对哪些节点有贡献 即每次对于一个节点,找到其第 ...
- [Usaco2012 Dec]Running Away From the Barn
题目描述 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 输入格式 Line 1: 2 integers, N and L (1 <= N <= 200,0 ...
- bzoj3011 [Usaco2012 Dec]Running Away From the Barn 左偏树
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3011 题解 复习一下左偏树板子. 看完题目就知道是左偏树了. 结果这个板子还调了好久. 大概已 ...
- BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序
BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序 题意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最 ...
- 【BZOJ3012】[Usaco2012 Dec]First! Trie树+拓补排序
[BZOJ3012][Usaco2012 Dec]First! Description Bessie has been playing with strings again. She found th ...
- [USACO 12DEC]Running Away From the Barn
Description It's milking time at Farmer John's farm, but the cows have all run away! Farmer John nee ...
- USACO Running Away From the Barn /// 可并堆 左偏树维护大顶堆
题目大意: 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于m的点有多少个 左偏树 https://blog.csdn.net/pengwill97/article/details/82 ...
随机推荐
- javascript—Mach的一些常用方法
1.Math.random():返回 0 ~ 1 之间的随机数. 2.Math.round():四舍五入取整. 3.Math.ceil():向上取整; 例如:a=1.2,b=5.8; ...
- Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)
根据中序和后续遍历构建二叉树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...
- java并发包小结(一)
java.util.concurrent 包含许多线程安全.高性能的并发构建块.换句话讲,创建 java.util.concurrent 的目的就是要实现 Collection 框架对数据结构所执行的 ...
- 基于libevent的tcp拆包分包库
TCP/IP协议虽然方便,但是由于是基于流的传输(UDP是基于数据报的传输),无论什么项目,总少不了解决拆包分包问题. 以前的项目总是每个程序员自己写一套拆包分包逻辑,实现的方法与稳定性都不太一致.终 ...
- hadoop配置文件详解系列(一)-core-site.xml篇
接上一个属性,这个属性就是设置阈值的. hadoop.security.groups.cache.secs 300 配置用户组映射缓存时间的,当过期时重新获取并缓存. hadoop.security. ...
- There is No Alternative~最小生成树变形
Description ICPC (Isles of Coral Park City) consist of several beautiful islands. The citizens reque ...
- java之Hibernate框架实现数据库操作
之前我们用一个java类连接MySQL数据库实现了数据库的增删改查操作---------MySQL篇: 但是数据库种类之多,除了MySQL,还有Access.Oracle.DB2等等,而且每种数据库语 ...
- longestCommonPrefix
Description: Write a function to find the longest common prefix string amongst an array of strings. ...
- 妙用this关键字
妙用this关键字 ## this关键字一般this关键字都是指的是对象的本身,在类的所有方法.构造器都可以拿到this引用,这是jvm"偷偷"帮我们传递进来的引用,指向调用方法对 ...
- 如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下)
前天给大家分享了如何利用Python网络爬虫爬取微信朋友圈数据的上篇(理论篇),今天给大家分享一下代码实现(实战篇),接着上篇往下继续深入. 一.代码实现 1.修改Scrapy项目中的items.py ...