Solution -「NOI 2021」轻重边
Description
Link.
给出一棵树,初始边权为 \(0\),支持毛毛虫虫体赋 \(1\),虫足赋 \(0\),以及查询路径边权和操作,\(n,m\leqslant 10^5\)。
Solution
立马想到按时间染色,那么判定一条边 \((u,v)\) 为重边的充要条件即 \(col(u)=col(v)\)(初始每个节点的颜色为 \(1,\dots,n\))。
然后修改就转化为链覆盖,查询就成了查询区间相邻颜色相同二元组个数。HLD & 线段树可以解决。
说一下线段树的维护方法,维护区间左端点颜色、右端点颜色以及相邻颜色相同二元组个数(还有区间覆盖懒标)。向上 / 下更新均显。
因为整篇题解加起来还没有代码长,所以为避免头轻脚重折叠了
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++);
char buf[1<<21],*p1=buf,*p2=buf;
inline ll read() {
ll x=0,f=0; char ch=getchar();
while(ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+(ch&15),ch=getchar();
return f?-x:x;
}
const int N=100100;
vector<int> G[N];
int n,m,dep[N],son[N],top[N],fa[N],sz[N],dfn[N],sjc,ton[N],col[N],cnt;
struct node { int lc,rc,num,tag; } tr[N*4],emp;
void push_up(int now) {
tr[now].lc=tr[now<<1].lc;
tr[now].rc=tr[now<<1|1].rc;
tr[now].num=tr[now<<1].num+tr[now<<1|1].num+(tr[now<<1].rc==tr[now<<1|1].lc);
}
void push_down(int now,int l,int r) {
if(!tr[now].tag) return;
int mid=(l+r)>>1;
tr[now<<1].num=mid-l,tr[now<<1|1].num=r-mid-1;
tr[now<<1].lc=tr[now<<1|1].lc=tr[now].tag;
tr[now<<1].rc=tr[now<<1|1].rc=tr[now].tag;
tr[now<<1].tag=tr[now<<1|1].tag=tr[now].tag;
tr[now].tag=0;
}
void build(int l,int r,int now) {
tr[now]=emp;
if(l==r) return ++cnt,tr[now]=node{cnt,cnt,0,0},void();
int mid=(l+r)>>1;
build(l,mid,now<<1),build(mid+1,r,now<<1|1);
push_up(now);
}
void ins(int l,int r,int now,int x,int y,int v) {
if(l>y||r<x) return;
if(l>=x&&r<=y) return tr[now].tag=tr[now].lc=tr[now].rc=v,tr[now].num=r-l,void();
int mid=(l+r)>>1;
push_down(now,l,r);
ins(l,mid,now<<1,x,y,v),ins(mid+1,r,now<<1|1,x,y,v);
push_up(now);
}
int find0(int l,int r,int now,int x,int y) {
if(l>=x&&r<=y) return tr[now].num;
int mid=(l+r)>>1,res=0;
push_down(now,l,r);
if(mid>=x) res+=find0(l,mid,now<<1,x,y);
if(mid<y) res+=find0(mid+1,r,now<<1|1,x,y);
return res+(x<=mid&&y>mid&&tr[now<<1].rc==tr[now<<1|1].lc);
}
int find1(int l,int r,int now,int x) {
if(l==r) return tr[now].lc;
int mid=(l+r)>>1;
push_down(now,l,r);
return mid>=x?find1(l,mid,now<<1,x):find1(mid+1,r,now<<1|1,x);
}
void clear(int l,int r,int now) {
tr[now]=emp;
if(l==r) return;
int mid=(l+r)>>1;
clear(l,mid,now<<1),clear(mid+1,r,now<<1|1);
}
signed main() {
function<void(int,int)> dfs0=[&](int x,int las) {
fa[x]=las,dep[x]=dep[las]+1,sz[x]=1;
for(int y:G[x]) if(y!=las) dfs0(y,x),sz[x]+=sz[y],(sz[y]>sz[son[x]])&&(son[x]=y);
};
function<void(int,int,int)> dfs1=[&](int x,int las,int t) {
top[x]=t,ton[dfn[x]=++sjc]=x;
if(son[x]) dfs1(son[x],x,t);
for(int y:G[x]) if(y!=las&&y!=son[x]) dfs1(y,x,y);
};
for(int Case=read(); Case; --Case) {
n=read(),m=read();
for(int i=1,x,y; i<n; ++i) x=read(),y=read(),G[x].push_back(y),G[y].push_back(x);
dfs0(1,0),dfs1(1,0,1),build(1,n,1);
for(int t,x,y; m; --m) {
t=read(),x=read(),y=read();
if(t==1) {
++cnt;
while(top[x]!=top[y]) {
if(dep[top[x]]<dep[top[y]]) swap(x,y);
ins(1,n,1,dfn[top[x]],dfn[x],cnt);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
ins(1,n,1,dfn[x],dfn[y],cnt);
} else {
int res=0;
while(top[x]!=top[y]) {
if(dep[top[x]]<dep[top[y]]) swap(x,y);
res+=find0(1,n,1,dfn[top[x]],dfn[x])+(find1(1,n,1,dfn[top[x]])==find1(1,n,1,dfn[fa[top[x]]]));
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
res+=find0(1,n,1,dfn[x],dfn[y]);
printf("%d\n",res);
}
}
for(int i=1; i<=n; ++i) dep[i]=son[i]=top[i]=fa[i]=sz[i]=dfn[i]=ton[i]=0,G[i].clear();
sjc=cnt=0,clear(1,n,1);
}
return 0;
}
Solution -「NOI 2021」轻重边的更多相关文章
- Solution -「NOI 2021」「洛谷 P7740」机器人游戏
\(\mathcal{Description}\) Link. 自己去读题面叭~ \(\mathcal{Solution}\) 首先,参悟[样例解释 #2].一种暴力的思路即为钦定集合 \ ...
- Diary -「NOI 2021」酱油记
雨幕浓稠 远近一白 是水雾弥漫的天 还是泡沫撑起的海 雨真大呢. 前几天去 ZH 中学集训没啥好记的,就从会合日开始叭. [Day -1] 逃出 ZH,掉入梦麟.( 高中的同学们忘记带 ...
- Solution -「JOISC 2021」古老的机器
\(\mathcal{Description}\) Link. 这是一道通信题. 对于长度为一个 \(n\),仅包含字符 X, Y, Z 的字符串 \(s\),将其中 \(n\) 个字符按 ...
- Solution -「JOISC 2021」「LOJ #3489」饮食区
\(\mathcal{Description}\) Link. 呐--不想概括题意,自己去读叭~ \(\mathcal{Solution}\) 如果仅有 1. 3. 操作,能不能做? ...
- Solution -「JOISC 2021」「LOJ #3495」聚会 2
\(\mathcal{Description}\) Link. 给定一棵含 \(n\) 个结点的树.称点集 \(S\) 到结点 \(u\) 的会合距离为 \(\sum_{v\in S}\ope ...
- Solution -「JOISC 2021」「LOJ #3491」道路建设
\(\mathcal{Description}\) Link. 平面上有 \(n\) 个互不重合的点 \((x_{1..n},y_{1..n})\),求其两两曼哈顿距离的前 \(m\) 小值. ...
- Solution -「NOI 2020」「洛谷 P6776」超现实树
\(\mathcal{Description}\) Link. 对于非空二叉树 \(T\),定义 \(\operatorname{grow}(T)\) 为所有能通过若干次"替换 \( ...
- Solution -「NOI 2016」「洛谷 P1587」循环之美
\(\mathcal{Description}\) Link. 给定 \(n,m,k\),求 \(x\in [1,n]\cap\mathbb N,y\in [1,m]\cap \mathbb ...
- Solution -「NOIOL-S 2021」「洛谷 P7470」岛屿探险
\(\mathcal{Description}\) Link. 给定序列 \(\{(a,b)_n\}\),\(q\) 组形如 \((l,r,c,d)\) 的询问,求 \[\Big|\{i\in ...
- Solution -「NOI 2012」「洛谷 P2050」美食节
\(\mathcal{Description}\) Link. 美食节提供 \(n\) 种菜品,第 \(i\) 种的需求量是 \(p_i\),菜品由 \(m\) 个厨师负责制作,第 \(j\) ...
随机推荐
- CANoe_系统变量的创建过程
在Canoe中创建系统变量,可以用于定义和管理与CAN网络通信相关的参数和配置.遵循以下步骤: 1.打开Canoe 启动Canoe软件. 2.打开项目 在Canoe的菜单栏中,选择"File ...
- Pytorch-如何在模型中引入可学习参数
错误实例: def init(self): self.w1 = torch.nn.Parameter(torch.FloatTensor(1),requires_grad=True).cuda() s ...
- 第四章 IDEA的安装与使用
网上一大推的教程
- 采集douban
# -*- coding: utf-8 -*-"""Created on Thu Oct 31 16:14:02 2019 @author: DELL"&quo ...
- CentOS 8搭建Kubernetes-k8s集群-1.18.5
目录 环境配置 服务器信息 软件版本 环境正确性 端口正常开放 kube-master节点端口 kube-node节点端口 配置主机互信 配置hosts映射 配置ssh密钥 禁用swap 关闭SELi ...
- Code Generate 代码生成器 V1.0
Code Generate V1.0 代码生成器 根据配置的模板,根据建表语句,生成Code. 例如java代码.vue代码.jsp代码以及html代码等等,均可根据自己的代码写作习惯进行配置. 缺点 ...
- 最为常用的Laravel操作(3)-模板
Blade 模板引擎 模板继承 定义布局: <!-- 存放在 resources/views/layouts/app.blade.php --> <html> <head ...
- Linux 软件包:添加repo、升级内核、编译内核、交叉编译
添加 repo 增加 xxx.repo 文件 在/etc/yum.repos.d/目录下创建 add_openeuler_repo.repo 文件 [add_repo] name=add_repo b ...
- IRF技术介绍及配置介绍
IRF技术介绍及配置介绍 IRF(Intelligent Resilient Framework,智能弹性架构)是 H3C 自主研发的软件虚拟化技术. 它的核心思想是将多台设备通过 IRF 物理端口连 ...
- git关于分支的常用命令
上家公司实习,一个人干一个项目,没有用git管理代码,导致我以前学的命令都忘了 git checkout -b xxx 创建xxx分支 并切换到xxx分支 等价于 git branch xxx git ...