「luogu3258」[JLOI2014] 松鼠的新家
https://www.luogu.org/problemnew/show/P3258
(树剖裸题
树上差分 = = 差分 + lca
1. 树上差分基本思想:和差分一样,用前缀和的思想来处理解(操作后的树上,任意节点的糖果数 是通过所有与其相连的子节点的和 以及该节点在差分数组里的值 得到的(dfs);
2. 因为这道题并不需要除了lca以外的信息,故tarjan
对于一组修改u,v :只需修改差分数组 ----- diff[u]++, diff[v], diff[lca(u,v)]--, diff[fa[lca(u,v)]]--
至于为什么,看1;
剩下自己想;;
// 15owzLy1
//luogu3258.cpp
//2018 09 25 18:45:43
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
typedef double db;
using namespace std; const int N = ;
struct node {
int next, to;
}edge[N<<];
int set_[N], candy[N], diff[N], n, head[N], head_q[N], fa[N];
int query[N][], a, b, c, cnt;
bool vis[N]; template<typename T>inline void read(T &x_) {
x_=;bool f_=;char c_=getchar();
while(c_<''||c_>''){f_|=(c_=='-');c_=getchar();}
while(c_>=''&&c_<=''){x_=(x_<<)+(x_<<)+(c_^);c_=getchar();}
x_=f_?-x_:x_;
} inline void jb(int u, int v) {
edge[++cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt;
} int get_set_(int x) {
return (set_[x]==x)?x:set_[x]=get_set_(set_[x]);
} inline void Merge(int x, int y) {
set_[get_set_(y)]=get_set_(x);
} void dfs(int u) {
vis[u]=true;
for(int i=head[u];i;i=edge[i].next) {
int v=edge[i].to;
if(v==fa[u]) continue;
fa[v]=u;
dfs(v);
Merge(u, v);
} if(vis[query[u][]]) {
a=get_set_(query[u][]);
if(a!=get_set_(u)) {
diff[a]--, diff[fa[a]]--;
diff[u]++, diff[query[u][]]++;
}
}
if(vis[query[u][]]) {
a=get_set_(query[u][]);
if(a!=get_set_(u)) {
diff[a]--, diff[fa[a]]--;
diff[query[u][]]++, diff[u]++;
}
}
} void get_ans(int u) {
candy[u]=diff[u];
for(int i=head[u];i;i=edge[i].next) {
int v=edge[i].to;
if(v==fa[u]) continue;
get_ans(v);
candy[u]+=candy[v];
}
} int main() {
#ifndef ONLINE_JUDGE
freopen("luogu3258.in","r",stdin);
freopen("luogu3258.out","w",stdout);
#endif
int x, y, a1;
read(n); read(x); a1=x;
for(int i=;i<n;i++) {
read(y);
query[y][]=x, query[x][]=y;
x=y;
} for(int i=;i<n;i++) read(x), read(y), jb(x, y), jb(y, x);
for(int i=;i<=n;i++) set_[i]=i;
dfs(); get_ans(); candy[a1]++;
for(int i=;i<=n;i++)
printf("%d\n", candy[i]-);
return ;
}
「luogu3258」[JLOI2014] 松鼠的新家的更多相关文章
- BZOJ 3631: [JLOI2014]松鼠的新家( 树链剖分 )
裸树链剖分... ------------------------------------------------------------------- #include<bits/stdc++ ...
- 3631: [JLOI2014]松鼠的新家
3631: [JLOI2014]松鼠的新家 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 707 Solved: 342[Submit][Statu ...
- [填坑]树上差分 例题:[JLOI2014]松鼠的新家(LCA)
今天算是把LCA这个坑填上了一点点,又复习(其实是预习)了一下树上差分.其实普通的差分我还是会的,树上的嘛,也是懂原理的就是没怎么打过. 我们先来把树上差分能做到的看一下: 1.找所有路径公共覆盖的边 ...
- P3258 [JLOI2014]松鼠的新家
P3258 [JLOI2014]松鼠的新家倍增lca+树上差分,从叶子节点向根节点求前缀和,dfs求子树和即可,最后,把每次的起点和终点都. #include<iostream> #inc ...
- 洛谷 P3258 [JLOI2014]松鼠的新家 解题报告
P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...
- 【洛谷】【lca+树上差分】P3258 [JLOI2014]松鼠的新家
[题目描述:] 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n(2 ≤ n ≤ 300000)个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真 ...
- [Luogu 3258] JLOI2014 松鼠的新家
[Luogu 3258] JLOI2014 松鼠的新家 LCA + 树上差分. 我呢,因为是树剖求的 LCA,预处理了 DFN(DFS 序),于是简化成了序列差分. qwq不讲了不讲了,贴代码. #i ...
- [JLOI2014] 松鼠的新家 (lca/树上差分)
[JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在 ...
- 洛谷P3258 [JLOI2014]松鼠的新家
P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...
随机推荐
- How to move lobsegment and lobindex to a different Tablespace
Hi, Assuming that I have table "TEST" in USERS TableSpace CREATE TABLE TEST ( TEST_ID NUMB ...
- go笔记-限速器(limiter)
参考: https://blog.csdn.net/wdy_yx/article/details/73849713https://www.jianshu.com/p/1ecb513f7632 http ...
- JavaWeb工程中web.xml基本配置(转载学习)
一.理论准备 先说下我记得xml规则,必须有且只有一个根节点,大小写敏感,标签不嵌套,必须配对. web.xml是不是必须的呢?不是的,只要你不用到里面的配置信息就好了,不过在大型web工程下使用该文 ...
- 全文搜索引擎 ElasticSearch 还是 Solr?
最近项目组安排了一个任务,项目中用到了全文搜索,基于全文搜索 Solr,但是该 Solr 搜索云项目不稳定,经常查询不出来数据,需要手动全量同步,而且是其他团队在维护,依赖性太强,导致 Solr 服务 ...
- IPv6绝不仅仅是对IPv4地址长度的增加
众所周知,IPv6 IP地址长度是IPv4 IP地址长度的四倍,是解决IPv4公共网址资源枯竭的最佳技术.的确,IETF在制定IPv6标准时也是基于这一因素考虑的.当时正是90年代初,Web开始出现, ...
- (hdu) 4857 逃生 (拓扑排序+优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄 ...
- PyQt5基础应用一
一.PyQt5基础 1.1 创建窗口 import sys from PyQt5.QtWidgets import QApplication, QWidget if __name__ == '__ ...
- CodeForces Round #545 Div.2
A. Sushi for Two 代码: #include <bits/stdc++.h> using namespace std; ; ; int a[maxn], vis[maxn]; ...
- Airflow Python工作流引擎的重要概念介绍
Airflow Python工作流引擎的重要概念介绍 - watermelonbig的专栏 - CSDN博客https://blog.csdn.net/watermelonbig/article/de ...
- oracle:TNS:监听程序无法分发客户机连接
挂上vpn的时候,PL/SQL连接到oracle的时候,显示ORA-12518:监听程序无法分发客户机连接.如下图: 一.[问题描述] 最近,在系统高峰期的时候,会提示如上的错误,致使无法连接到服务器 ...