【BZOJ-3631】松鼠的新家 树形DP?+ 倍增LCA + 打标记
3631: [JLOI2014]松鼠的新家
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1231 Solved: 620
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 4 5 3 2
1 2
2 4
2 3
4 5
Sample Output
2
1
2
1
HINT
2<= n <=300000
Source
Solution
一眼树链剖分裸题,但是懒得打.....这里是一种巧妙的方法: LCA后打标记,树形DP对标记进行上传统计答案
方法就是对于从a[i]走到a[i+1]这个路径,对a[i]和a[i+1]打上start标记,对LCA(a[i],a[i+1])和LCA的父节点打上end标记
把所有的标记上传,更新答案;
自己的理解是因为在向上更新的时候,a[i]和a[i+1]的标记更新到LCA抵消后会多出一个额外的标记,所以需要两个end标记
至于最后答案减一?因为出始节点是不算入的,但上传标记需要给初始节点打标记,所以得减一(这个还是很好想的)
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define maxn 301000
int n,a[maxn];
struct EdgeNode{int next,to,from;}edge[maxn<<];
int head[maxn],cnt;
void add(int u,int v)
{
cnt++;edge[cnt].next=head[u];head[u]=cnt;edge[cnt].to=v;edge[cnt].from=u;
}
void insert(int u,int v) {add(u,v); add(v,u);}
int deep[maxn],father[maxn][],ans[maxn],start[maxn],end[maxn];
void DFS(int x)
{
for (int i=; i<=; i++)
if (deep[x]>=(<<i)) father[x][i]=father[father[x][i-]][i-];
else break;
for (int i=head[x]; i; i=edge[i].next)
if (edge[i].to!=father[x][])
{
deep[edge[i].to]=deep[x]+;
father[edge[i].to][]=x;
DFS(edge[i].to);
}
}
int LCA(int x,int y)
{
if (deep[x]<deep[y]) swap(x,y);
int dd=deep[x]-deep[y];
for (int i=; i<=; i++)
if (dd&(<<i)) x=father[x][i];
for (int i=; i>=; i--)
if (father[x][i]!=father[y][i])
x=father[x][i],y=father[y][i];
if (x==y) return x; else return father[x][];
}
void UpTag(int x)
{
ans[x]=start[x]-end[x];
for (int i=head[x]; i; i=edge[i].next)
if (edge[i].to!=father[x][])
UpTag(edge[i].to),ans[x]+=ans[edge[i].to];
}
int main()
{
n=read();
for (int i=; i<=n; i++) a[i]=read();
for (int u,v,i=; i<=n-; i++) u=read(),v=read(),insert(u,v);
DFS(a[]);
for (int lca,i=; i<=n-; i++)
start[a[i]]++,start[a[i+]]++,lca=LCA(a[i],a[i+]),end[lca]++,end[father[lca][]]++;
UpTag(a[]);
for (int i=; i<=n; i++) ans[a[i]]--;
for (int i=; i<=n; i++) printf("%d\n",ans[i]);
return ;
}
【BZOJ-3631】松鼠的新家 树形DP?+ 倍增LCA + 打标记的更多相关文章
- bzoj 3631 松鼠的新家 (树链剖分)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3631 思路: 直接用树链剖分求每一次运动,因为这道题只需要区间增添,单点求值,没必要用线段 ...
- BZOJ 3631 松鼠的新家
链剖. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- BZOJ - 3631 松鼠的新家 (树链剖分)
题目链接 树链剖分基础题,路径权值修改+差分 #include<bits/stdc++.h> using namespace std; typedef long long ll; ; in ...
- BZOJ 3631 松鼠的新家 树上差分
我猜会有智障说直接链剖+线段树…(希望没有) From RYC's 课件 然鹅我并不反对树剖...我是智障...QAQ 好吧还是树上差分:设 a[i]=u.a[i+1]=v ++w[u],++w[v] ...
- BZOJ 3631 松鼠的新家 - 树链剖分 / 树上差分
传送门 分析: 树链剖分:x->y,将x到y的路径加一,并将x端点的答案-1,最后统计答案. 树上差分:x->y,x+1,y+1,lca-1,fa[lca]-1,并将x打上标记,最后统计前 ...
- Codeforces 418d Big Problems for Organizers [树形dp][倍增lca]
题意: 给你一棵有n个节点的树,树的边权都是1. 有m次询问,每次询问输出树上所有节点离其较近结点距离的最大值. 思路: 1.首先是按照常规树形dp的思路维护一个子树节点中距离该点的最大值son_di ...
- hdu5449 Robot Dog (树形dp+倍增lca)
(警告:本篇博客包含大量人类本质内容) 先处理出来lca,然后就只需要知道从每个点到他的父亲.和从他的父亲到这个点的期望时间就可以了 我们设f[x]为x到他父亲的期望时间:g[x]为从x的父亲到x的期 ...
- 3631. [JLOI2014]松鼠的新家【树形DP】
Description 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在“树”上.松鼠想邀 ...
- BZOJ 3631: [JLOI2014]松鼠的新家( 树链剖分 )
裸树链剖分... ------------------------------------------------------------------- #include<bits/stdc++ ...
随机推荐
- linux numfmt 命令--转换数字
numfmt SYNOPSIS numfmt [OPTION]... [NUMBER]... DESCRIPTION Reformat NUMBER(s), or the numbers from s ...
- 利用ThinkPHP自带的七牛云驱动上传文件到七牛云以及删除七牛云文件方法
一.准备工作 1.注册七牛云账号 2.选择对象储存->创建空间->设置为公开 3.在config配置文件中添加以下代码 'UPLOAD_FILE_QINIU' => array ( ...
- VMware-Transport(VMDB) error -44:Message.The VMware Authorization Service is not running解决方案
出现的错误如下: 原因:本机中有一个VMware服务未开启导致的. 解决方案: 1.打开“运行”->输入services.msc !!!文章转自浩瀚先森博客,转载请注明,谢谢.http://ww ...
- 0 bug 读后感
本书全名是 <0 bug- C/C++商用工程之道>,这是一本有争议的书,豆瓣链接: http://book.douban.com/subject/4149139/ ,建议有一些商用的开发 ...
- ASP.NET Web API Help Pages using Swagger
Understanding the various methods of an API can be a challenge for a developer when building a consu ...
- 端口被占用的解决方案 sql server 10048 错误
一大早发现sql server服务无法启动,10048错误,一查是端口占用. 先找到哪个进程,结束即可. cmd命令, netstat /ano|findset "1433" 出现 ...
- 闲扯 『 document.write 』
初春的晚上,闲来无事,聊聊 document.write 方法. document.write 使用方式非常简单,把 "字符串化"(不好意思,这可能是我自己创造的名词)的 html ...
- 由一次程序崩溃引起的对new表达式的再次学习
1. 起因 某天,一个同事跟我反馈说在windows上调试公司产品的一个交易核心时出现了使用未初始化的指针导致后台服务崩溃的情况.示例代码如下所示: struct sample { ][]; //.. ...
- viso图插入Word中大片空白解决办法
按住CTRL-->将鼠标指向绘图页边缘-->指针变为移动符号(双向箭头)-->按下左键修改绘图页大小.
- Vware Workstation pro 12|虚拟机
Vmware是比较不错的PC虚拟化软件,vmware11+不在支持32的系统安装!体积比之前小了很多 VMware 12 官方中文页面 http://vmware.com/cn/products/wo ...