[Luogu 3258] JLOI2014 松鼠的新家

<题目链接>


LCA + 树上差分。

我呢,因为是树剖求的 LCA,预处理了 DFN(DFS 序),于是简化成了序列差分。

qwq不讲了不讲了,贴代码。

#include <algorithm>
#include <cstdio>
#include <cstring> #define nullptr NULL const int MAXN = 300010; int n, a[MAXN]; namespace HLD
{
int qwq[MAXN];
class Graph
{
private:
bool vis[MAXN];
int num;
struct Node
{
int depth, size, father, son, top, DFN;
}s[MAXN];
struct Edge
{
int to;
Edge *next;
Edge(int to, Edge* next): to(to), next(next){}
~Edge(void)
{
if(next!=nullptr)
delete next;
}
}*head[MAXN];
void Modify(int l, int r, int k)
{
qwq[s[l].DFN] += k;
qwq[s[r].DFN + 1] -= k;
}
void Add(int x, int y)
{
int a, b;
while((a = s[x].top) ^ (b = s[y].top))
if(s[a].depth > s[b].depth)
{
Modify(a, x, 1);
x = s[a].father;
}
else
{
Modify(b, y, 1);
y = s[b].father;
}
if(s[x].depth < s[y].depth)
Modify(x, y, 1);
else
Modify(y, x, 1);
}
public:
Graph(int n): num(0)
{
memset(vis, 0, sizeof vis);
memset(s, 0, sizeof s);
std :: fill(head + 1, head + n + 1, (Edge*)nullptr);
}
~Graph(void)
{
for(int i = 1; i <= n; ++i)
delete head[i];
}
void AddEdges(int u, int v)
{
head[u] = new Edge(v, head[u]);
head[v] = new Edge(u, head[v]);
}
void DFS1(int u, int k)
{
s[u].depth = k;
s[u].size = 1;
int v;
for(Edge* i = head[u]; i != nullptr; i = i -> next)
if(!s[v = i -> to].depth)
{
DFS1(v, k + 1);
s[v].father = u;
s[u].size += s[v].size;
if(s[v].size > s[s[u].son].size)
s[u].son = v;
}
}
void DFS2(int u, int top)
{
s[u].top = top;
s[u].DFN = ++num;
vis[u] = true;
if(s[u].son)
DFS2(s[u].son, top);
int v;
for(Edge* i = head[u]; i != nullptr; i = i -> next)
if(!vis[v = i -> to])
DFS2(v, v);
}
void Walk(int x, int y)
{
Add(x, y);
Modify(y, y, -1);
}
void Find(void)
{
for(int i = 1; i < n; ++i)
qwq[i + 1] += qwq[i];
for(int i = 1; i <= n; ++i)
printf("%d\n", qwq[s[i].DFN]);
}
}*G;
void Init(void)
{
G = new Graph(n);
for(int i = 1, u, v; i < n; ++i)
{
scanf("%d %d", &u, &v);
G -> AddEdges(u, v);
}
G -> DFS1(1, 1);
G -> DFS2(1, 1);
}
} int main(void)
{
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
HLD :: Init();
for(int i = 1; i < n; ++i)
HLD :: G -> Walk(a[i], a[i + 1]);
HLD :: G -> Find();
return 0;
}

谢谢阅读。

[Luogu 3258] JLOI2014 松鼠的新家的更多相关文章

  1. [Luogu] P3258 [JLOI2014]松鼠的新家

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...

  2. 【luogu P3258 [JLOI2014]松鼠的新家】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3258 谁说树剖过不去会RE呢? 我今天就是要强行树剖了 树剖强艹 #include <cstdio&g ...

  3. [JLOI2014]松鼠的新家(树链剖分)

    [JLOI2014]松鼠的新家(luogu) Description 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间 ...

  4. BZOJ 3631: [JLOI2014]松鼠的新家( 树链剖分 )

    裸树链剖分... ------------------------------------------------------------------- #include<bits/stdc++ ...

  5. 3631: [JLOI2014]松鼠的新家

    3631: [JLOI2014]松鼠的新家 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 707  Solved: 342[Submit][Statu ...

  6. [填坑]树上差分 例题:[JLOI2014]松鼠的新家(LCA)

    今天算是把LCA这个坑填上了一点点,又复习(其实是预习)了一下树上差分.其实普通的差分我还是会的,树上的嘛,也是懂原理的就是没怎么打过. 我们先来把树上差分能做到的看一下: 1.找所有路径公共覆盖的边 ...

  7. P3258 [JLOI2014]松鼠的新家

    P3258 [JLOI2014]松鼠的新家倍增lca+树上差分,从叶子节点向根节点求前缀和,dfs求子树和即可,最后,把每次的起点和终点都. #include<iostream> #inc ...

  8. 洛谷 P3258 [JLOI2014]松鼠的新家 解题报告

    P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...

  9. 【洛谷】【lca+树上差分】P3258 [JLOI2014]松鼠的新家

    [题目描述:] 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n(2 ≤ n ≤ 300000)个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真 ...

随机推荐

  1. 四则运算截图and代码

    1.运行截图 2.代码 #include<stdio.h> #include<stdlib.h> int main() { int i=300; int a=0; while( ...

  2. jQ返回定位插件

    一.jQuery 提供开发者开发插件的几种模式 1.$.extend();     //这个方法是绑定在$上面的.可以通过$直接调用 2.$.fn.方法名     //这个方法是绑定在Dom对象上面的 ...

  3. pygame学习笔记(5)——精灵

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 据说在任天堂FC时代,精灵的作用相当巨大,可是那时候只知道怎么玩超级玛丽.魂斗罗,却对精灵一点也不知.pygame ...

  4. Java乐观锁、悲观锁

    乐观锁 乐观锁(Optimistic Lock), 顾名思义,就是很乐观,每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,可以使用版本号 ...

  5. 谁能告诉delphi7 的updatebatch使用属性说明?

    谁能告诉delphi7 的updatebatch使用属性说明? ADODataSet1.UpdateBatch(arAll); 就是提交你的数据集到数据库 arCurrentOnly the upda ...

  6. todomvc-app

    1.HTML <!doctype html> <html lang="en"> <head> <meta charset="ut ...

  7. Qt中 QString 转 char*

    Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...

  8. InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]

    在莫烦Python教程的“Dropout 解决 overfitting”一节中,出现错误如下: InvalidArgumentError: You must feed a value for plac ...

  9. [BZOJ1559]密码 AC自动机+状压

    问题 K: [JSOI2009]密码 时间限制: 1 Sec  内存限制: 64 MB 题目描述 众所周知,密码在信息领域起到了不可估量的作用.对于普通的登陆口令,唯一的破解 方法就是暴力破解一逐个尝 ...

  10. poj2761 feed the dog

    题目链接:http://poj.org/problem?id=2761 Description Wind loves pretty dogs very much, and she has n pet ...