跳到正文

个人记录

实链剖分

T7_DanieI约 33 分钟
原文

这是 2026年5月12日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年5月12日
更新
2026年5月12日
篇幅
约 33 分钟

版本

  1. v2当前
  2. v1

引入

如果你需要在一棵树上实现单点修改、路径上信息询问,你可以实现重链剖分实现这个问题。

但是,如果这里不是一棵树呢?如果是多棵树,伴随着边的连接、断开,我们该怎么办呢?

【模板】动态树(LCT),比如这个题。我们发现,在这个时候,如果只用原本预处理链的思路是无法实现的。

我们需要想一个办法动态地处理连边和断边的信息。

连接,还需要保证时间复杂度?那也就是说,我们需要在这上面进行一些操作稳定复杂度?

这很难不让人想起 Splay 大人。

算法思想

我们把每一段实链用 Splay 存起来。

在实现上,我们会往右方向去连接,也就是说,一个 1−>2−>3−>41 -> 2 -> 3 -> 4,建出来是这样的:

那么这个实链的中序遍历顺序是 4,3,2,14,3,2,1。

我们引入一个操作 access(x),用于打通一个点到其根节点的路径,并将 xx 换到中序遍历的最后一个。

比如我们在这里 access(3),然后这棵树就会变成:

我们再引入一个“虚链”的概念,这个就是不是实链的连接。

比如这个:

假如实链是 6−>56 -> 5,3−>43 -> 4 和 2−>12 -> 1。

那么虚链就是 1−>41 -> 4 和 4−>54 -> 5,这两条边只标记了 fa 的信息。

那么这个时候,我们将 6−>56 -> 5 断开,然后将 44 接到右儿子上,那么就变成了这样:

这个时候,6−>56 -> 5 和 1−>41 -> 4 是虚链。

而 33 变成了 3−>4−>53 -> 4 -> 5 这条实链中序遍历最右侧的点。

但是,如果我们对 33 连边,显然不能对叶子节点操作,所以我们还需要用一次 splay(3) 将 33 转到顶上。

然后这条实链就变成了:

但是,这个是向左的一条链,有问题,我们需要将左右儿子交换,变成这样。

随后我们才可以进行操作。刚才这个操作叫做 makeroot(x),将 xx 设置为根节点。

其分为三步:access(x),splay(x),reverse(x)。

makeroot 函数
C++
inline void makeroot(int x){
  access(x);splay(x);reverse(x);
}

那么,access 怎么写呢?

首先,我们从 xx 开始,一直向上走,直到根节点。

对于每一个点,我们都进行 splay(x),找到 xx 原本的右儿子,将其断开,连接上。

此处是借助 fa 来跳的,因为我们的虚链上都只有 fa 信息,而没有对应的儿子信息,最后,更改了别忘记 pushup。

具体实现大概长这样。

access 函数
C++
inline void access(int x){
	for(int son=0;x;son=x,x=tr[x].fa){
		splay(x);tr[x].ch[1]=son;pushup(x);
	}
}

至于 reverse,我们延续数据结构的懒标记思想,打一个标记 tag 去实现。

具体实现大概长这样。

reverse 函数
C++
inline void reverse(int x){//翻转左右子树 
	if(!x) return;//注意空白节点不能有信息
	swap(tr[x].ch[0],tr[x].ch[1]);
	tr[x].tag^=1; 
}

那么,这里就有一个关键的点,我们在 splay(x) 的时候,需要先下放这里的懒标记,也就是说,我们还需要一个 push(x) 下放懒标记。

push 函数的具体实现大概长这样。

push 函数
C++
inline void push(int x){//将整棵树的懒标记信息全部下放 
	if(!check(x)) push(tr[x].fa);
	pushdown(x);
}

至于 pushdown,这是和数据结构类似的,具体实现大概长这样。

pushdown 函数
C++
inline void pushdown(int x){
	if(tr[x].tag){
		tr[x].tag=0;
		reverse(tr[x].ch[0]);
		reverse(tr[x].ch[1]);
	}
}

随后就是 splay 函数了,具体实现大概长这样。

splay 函数
C++
inline void splay(int x){//LCT 中只需要转到根节点 
	push(x);//下放全部懒标记 
	while(!check(x)){
		int y=tr[x].fa,z=tr[y].fa;
		if(!check(y)){//有祖父 
			if(son(x) xor son(y)) rotate(x);//不是一字型,直接转 
			else rotate(y);//否则先把一字转开 
		}
		rotate(x);//保证 x 上升至少一步 
	}
	pushup(x);
}

你可能会想这里怎么去判 root,因为现在 root 不唯一了。

我们先前不是有刻意防止空白节点被覆盖信息吗,在这里就能派上用场。

如果一个点是根节点,其父亲就是空的,两个儿子都不是他。

即我们可以直接宏定义出来这个函数。

check 函数宏定义
C++
#define check(x) ((tr[tr[x].fa].ch[0]!=x) && (tr[tr[x].fa].ch[1]!=x))

另外,这里的 son(x) 是用来判断 xx 是其父亲的哪一个儿子的,这个也很经典了,同样宏定义出来。

son 函数宏定义
C++
#define son(x) (tr[tr[x].fa].ch[1]==x)

至于 splay 的 rotate,这里是类似的,唯一需要注意的是不要给空白节点上信息,所以需要加两个判断。

rotate 函数
C++
inline void rotate(int x){
	int y=tr[x].fa,z=tr[y].fa;int k=son(x);
	tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;//注意,因为我们必须借助根节点父亲为空的性质,所以要保证根节点的父亲的信息不会被影响 
	tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;//y 继承 x 的一个子树,但是我们要保证空节点不能有信息 
	tr[y].fa=x,tr[x].ch[k^1]=y;//x 与 y 的关系  
	pushup(y);pushup(x);//自下往上 pushup 
}

所以,结构体里面存储的信息就是这样的。

结构体信息
C++
struct node{
	int ch[2];//儿子 
	int val;//当前值 
	int fa;//父亲 
	int num;//当前 xor 和 
	int tag;//要不要反转左右子树 
}tr[N];

然后 pushup 也就简单了。

pushup 函数
C++
inline void pushup(int x){
	tr[x].num=tr[tr[x].ch[0]].num xor tr[tr[x].ch[1]].num xor tr[x].val;
}

具体功能

有了这些基本操作,我们就可以实现很多功能了。

连边

我们发现其原理就是放到根节点后连接。

link 函数
C++
inline void link(int x,int y){
  makeroot(x);splay(x);tr[x].fa=y;
}

当然,有时题目不会保证 xx 和 yy 是不连通的,我们还需要写一个 findroot 函数。

findroot 不需要对原树做操作,所以你并不需要去 reverse(x) 以保证其向右偏的性质。

所以,我们只需要 access(x) 后 splay(x)。

然后中序遍历的第一个就是根节点,也就是一直往左走。

findroot 函数
C++
inline int findroot(int x){//找到当前树根 
	access(x);splay(x);
	while(tr[x].ch[0]) x=tr[x].ch[0];
	return x;
}

但是你写一个 reverse(x) 也没问题,但是会把内存加大很多。

单独拉出一部分

你要支持查询路径,按理来说,应该要支持将 x−>yx -> y 中的信息 split 出来。

那么这个怎么做呢?

还是比较简单的,不难想出一个思路,我们先将其中一个变成根节点,然后把另一个翻上去。

也就是说:

split 函数
C++
inline void split(int x,int y){
  makeroot(x);access(y);splay(y);
}

同样这里不需要 reverse(y)。

删除一条边

根据我们上面的“单独拉出一部分”,我们可以先 split(x,y)。

这个时候 xx 一定会在 yy 左侧,因为 yy 原本的儿子应该在 yy 右侧。

然后我们将这个儿子和父亲给归零即可。

当然,为了防止不是这个连接,我们需要加一个特判。

cut 函数
C++
inline void cut(int x,int y){
	split(x,y);
	if(tr[x].ch[1] || tr[y].ch[0]!=x) return;//如果没有连接就不管 
	tr[y].ch[0]=0;tr[x].fa=0;//清空 
}

查询一个路径

这个就简单了,因为 split(x,y) 可以被 yy 放到顶上,我们直接对其处理就好。

ask 函数
C++
inline int ask(int x,int y){
  split(x,y);return tr[y].num;
}

单点更改

这个可以直接 splay 处理,毕竟只涉及一个点。

change 函数
C++
inline void change(int x,int k){
  splay(x);tr[x].val=k;pushup(x);
}

拼起来就是模板题的代码。

模板题代码
C++
#include<bits/stdc++.h>
#define check(x) ((tr[tr[x].fa].ch[0]!=x) && (tr[tr[x].fa].ch[1]!=x)) //如果 x 的父亲两个儿子都不是 x,那么 x 是根节点 
#define son(x) (tr[tr[x].fa].ch[1]==x) //x 是其父亲的哪一个儿子 
using namespace std;

const int N=1e5+5;
struct node{
	int ch[2];//儿子 
	int val;//当前值 
	int fa;//父亲 
	int num;//当前 xor 和 
	int tag;//要不要反转左右子树 
}tr[N];

inline int read(){
	int s=0;char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s;
}

inline void pushup(int x){
	tr[x].num=tr[tr[x].ch[0]].num xor tr[tr[x].ch[1]].num xor tr[x].val;
}

inline void reverse(int x){//翻转左右子树 
	if(!x) return;//注意空白节点不能有信息
	swap(tr[x].ch[0],tr[x].ch[1]);
	tr[x].tag^=1; 
}

inline void pushdown(int x){
	if(tr[x].tag){
		tr[x].tag=0;
		reverse(tr[x].ch[0]);
		reverse(tr[x].ch[1]);
	}
}

inline void rotate(int x){
	int y=tr[x].fa,z=tr[y].fa;int k=son(x);
	tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;//注意,因为我们必须借助根节点父亲为空的性质,所以要保证根节点的父亲的信息不会被影响 
	tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;//y 继承 x 的一个子树,但是我们要保证空节点不能有信息 
	tr[y].fa=x,tr[x].ch[k^1]=y;//x 与 y 的关系  
	pushup(y);pushup(x);//自下往上 pushup 
}

inline void push(int x){//将整棵树的懒标记信息全部下放 
	if(!check(x)) push(tr[x].fa);
	pushdown(x);
}

inline void splay(int x){//LCT 中只需要转到根节点 
	push(x);//下放全部懒标记 
	while(!check(x)){
		int y=tr[x].fa,z=tr[y].fa;
		if(!check(y)){//有祖父 
			if(son(x) xor son(y)) rotate(x);//不是一字型,直接转 
			else rotate(y);//否则先把一字转开 
		}
		rotate(x);//保证 x 上升至少一步 
	}
	pushup(x);
}

inline void access(int x){//从 x 打通到根节点 
	for(int son=0;x;son=x,x=tr[x].fa){//一直向上移动,把这里的虚链变成实链,相连的无效实链变成虚链 
		splay(x);tr[x].ch[1]=son;pushup(x);
	}
} 

inline void makeroot(int x){//把当前点变成根节点 
	access(x);splay(x);reverse(x);
}

inline void split(int x,int y){//把 x -> y 拉出来 
	makeroot(x);access(y);splay(y);//把 x 变成根,再把 y 拉上来 
}

inline void link(int x,int y){
	makeroot(x);tr[x].fa=y;//把 x 拉成根,接上 y 
}

inline void cut(int x,int y){
	split(x,y);
	if(tr[x].ch[1] || tr[y].ch[0]!=x) return;//如果没有连接就不管 
	tr[y].ch[0]=0;tr[x].fa=0;//清空 
}

inline int findroot(int x){//找到当前树根 
	access(x);splay(x);
	while(tr[x].ch[0]) x=tr[x].ch[0];
	return x;
} 

int main(){
	int n,m;cin>>n>>m;
	for(int i=1;i<=n;i++){
		tr[i].val=read();
		tr[i].num=tr[i].val;
	}
	while(m--){
		int opt=read(),x=read(),y=read();
		if(opt==0){
			split(x,y);
			printf("%d\n",tr[y].num);
		}else if(opt==1){
			if(findroot(x)!=findroot(y)) link(x,y);//判断不在同一块 
		}else if(opt==2){
			cut(x,y);
		}else{
			splay(x);
			tr[x].val=y;
			pushup(x);
		}
	}
	return 0;
}

经典错法

  • rotate 中要先处理 y 与其儿子的关系,再处理 x 与 y 的关系,否则就会导致 y 自己与自己连边。
C++
inline void rotate(int x){//旋转 x
	int y=tr[x].fa,z=tr[y].fa;int k1=son(x),k2=son(y);
	tr[x].fa=z;if(!check(y)) tr[z].ch[k2]=x;//x 与 z
	tr[x].ch[k1^1]=y;tr[y].fa=x;//x 与 y
    tr[y].ch[k1]=tr[x].ch[k1^1];if(tr[x].ch[k1^1]) tr[tr[x].ch[k1^1]].fa=y;//y 与其儿子
	pushup(y);pushup(x);
}

需要换成:

C++
inline void rotate(int x){//旋转 x
	int y=tr[x].fa,z=tr[y].fa;int k1=son(x),k2=son(y);
	tr[x].fa=z;if(!check(y)) tr[z].ch[k2]=x;//x 与 z
	tr[y].ch[k1]=tr[x].ch[k1^1];if(tr[x].ch[k1^1]) tr[tr[x].ch[k1^1]].fa=y;//y 与其儿子
	tr[x].ch[k1^1]=y;tr[y].fa=x;//x 与 y
	pushup(y);pushup(x);
}

简单建模

[HNOI2010] 弹飞绵羊。

题目大意:到达一个节点后会向后运动一定格数,每个地方不同,可能会更改运动的格数。

询问你从一个节点开始需要被弹多少次才会被弹出去。

将往后弹的操作进行图论建模是显然的。

这里的更改就等价于连虚边了。

我们在最后建立一个虚点 n+1n+1 代表被弹飞了

那么我们往后连,根节点一直都是 n+1n+1。

那么我们也就只需要支持 link 和 cut 就好。

代码
C++
#include<bits/stdc++.h>
#define check(x) ((tr[tr[x].fa].ch[0]!=x) && (tr[tr[x].fa].ch[1]!=x)) 
#define son(x) (tr[tr[x].fa].ch[1]==x)
using namespace std;

const int N=2e5+5;
struct node{
	int ch[2];
	int sz;
	int fa;
	int tag;
}tr[N];
int a[N];

inline void pushup(int x){tr[x].sz=tr[tr[x].ch[0]].sz+tr[tr[x].ch[1]].sz+1;}

inline void rotate(int x){
	int y=tr[x].fa,z=tr[y].fa;
	int k1=son(x),k2=son(y);
	tr[x].fa=z;if(!check(y)) tr[z].ch[k2]=x;//不能让空节点获得信息 
	tr[y].ch[k1]=tr[x].ch[k1^1];if(tr[x].ch[k1^1]) tr[tr[x].ch[k1^1]].fa=y;//不能让空节点获得信息 
	tr[y].fa=x,tr[x].ch[k1^1]=y;
	pushup(y);pushup(x);//依旧忘记 pushup 
}

inline void reverse(int x){
	if(!x) return;
	tr[x].tag^=1;
	swap(tr[x].ch[0],tr[x].ch[1]);
}

inline void pushdown(int x){
	if(!tr[x].tag) return;
	reverse(tr[x].ch[0]);
	reverse(tr[x].ch[1]);
	tr[x].tag=0;
}

inline void push(int x){
	if(!check(x)) push(tr[x].fa);
	pushdown(x);
}

inline void splay(int x){//转到根节点 
	push(x);
	while(!check(x)){
		int y=tr[x].fa,z=tr[y].fa;
		if(!check(y)){//不能让根节点获得信息 
			if(son(x) xor son(y)) rotate(x);//不是一字型 
			else rotate(y);//一字型 
		}
		rotate(x);//保证 x 上升 
	}
	pushup(x);
}

inline void access(int x){
	for(int son=0;x;son=x,x=tr[x].fa){
		splay(x);tr[x].ch[1]=son;pushup(x);
	} 
}

inline void makeroot(int x){
	access(x);splay(x);reverse(x);
}

inline void split(int x,int y){
	makeroot(x);access(y);splay(y);
}

inline void cut(int x,int y){
	split(x,y);tr[y].ch[0]=0;tr[x].fa=0;pushup(y);
}

inline void link(int x,int y){
	makeroot(x);tr[x].fa=y;
}

inline int read(){
	int s=0;char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s;
}

int main(){
	int n=read();
	for(int i=1;i<=n;i++){
		int x=read();a[i]=x;
		if(i+x<=n) link(i,i+x);
		else link(i,n+1); 
	}
	int T;cin>>T;
	while(T--){
		int opt=read();
		if(opt==1){
			int x=read()+1;//警示后人,编号是从 0 开始的(哭) 
			makeroot(x);access(n+1);splay(n+1);
			cout<<tr[n+1].sz-1<<"\n";
		}else{
			int x=read()+1,k=read();//警示后人,编号是从 0 开始的(哭) 
			int y=(x+a[x]>n?n+1:x+a[x]);cut(x,y);
			a[x]=k;y=(x+a[x]>n?n+1:x+a[x]);link(x,y);
		}
	}	
	return 0;
}

[BJOI2014] 大融合

注意到查询等价于两侧子树大小之积。

所以查询就只需要断边,查询子树大小之积,再把边重新连上就好。

但是我们直接在 LCT 上记录之能够得到实子树的子树大小,这里还需要统计虚子树的信息。

所以我们还需要定义一个 exszex_{sz},记录虚子树的字数大小。

此时需要更改 link、access、pushup。

修正 link
C++
inline void link(int x,int y){//连接 x -> y
	makeroot(x);makeroot(y);
	tr[x].fa=y;tr[y].exsz+=tr[x].sz;
	pushup(y);
}
修正 access
C++
inline void access(int x){//打通 x 到根节点的路径
	for(int son=0;x;son=x,x=tr[x].fa){
		splay(x);tr[x].exsz+=tr[tr[x].ch[1]].sz-tr[son].sz;
		tr[x].ch[1]=son;pushup(x);
	}
}
修正 pushup
C++
inline void pushup(int x){tr[x].sz=tr[tr[x].ch[0]].sz+tr[tr[x].ch[1]].sz+tr[x].exsz+1;}
代码
C++
#include<bits/stdc++.h>
#define check(x) (tr[tr[x].fa].ch[0]!=x && tr[tr[x].fa].ch[1]!=x) //当前 x 是否为根节点
#define son(x) (tr[tr[x].fa].ch[1]==x) //当前 x 是父亲的哪一个儿子
using namespace std;

const int N=1e5+5;
struct node{int ch[2],fa,sz,tag,exsz;}tr[N];

inline int read(){
	int s=0;char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s;
}

inline void pushup(int x){tr[x].sz=tr[tr[x].ch[0]].sz+tr[tr[x].ch[1]].sz+tr[x].exsz+1;}

inline void rotate(int x){//旋转 x
	int y=tr[x].fa,z=tr[y].fa;int k=son(x);
	tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;//x & z
	tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;//y & y 的新儿子
	tr[y].fa=x,tr[x].ch[k^1]=y;
	pushup(y);pushup(x);
}

inline void reverse(int x){//反转一个点
	tr[x].tag^=1;
	swap(tr[x].ch[0],tr[x].ch[1]);
}

inline void pushdown(int x){//下方当前点的懒标记
	if(tr[x].tag){
		tr[x].tag=0;
		reverse(tr[x].ch[0]);
		reverse(tr[x].ch[1]);
	}
}

inline void push(int x){//下放整棵树的懒标记
	if(tr[x].fa) push(tr[x].fa);
	pushdown(x);
}

inline void splay(int x){//将 x 转到当前链的根
	push(x);
	while(!check(x)){
		int y=tr[x].fa,z=tr[y].fa;
		if(!check(y)){
			if(son(x) xor son(y)) rotate(x);
			else rotate(y);
		}
		rotate(x);
	}
	pushup(x);
}

inline void access(int x){//打通 x 到根节点的路径
	for(int son=0;x;son=x,x=tr[x].fa){
		splay(x);tr[x].exsz+=tr[tr[x].ch[1]].sz-tr[son].sz;
		tr[x].ch[1]=son;pushup(x);
	}
}

inline void makeroot(int x){//把 x 变成整棵树的根
	access(x);splay(x);reverse(x);
}

inline void link(int x,int y){//连接 x -> y
	makeroot(x);makeroot(y);
	tr[x].fa=y;tr[y].exsz+=tr[x].sz;
	pushup(y);
}

inline void split(int x,int y){//分割出 x -> y 的路径
	makeroot(x);access(y);splay(y);
}

inline void cut(int x,int y){//断边 x -> y
	split(x,y);tr[x].fa=tr[y].ch[0]=0;
}

int main(){
	int n,m;cin>>n>>m;
	while(m--){
		char ch;cin>>ch;
		if(ch=='A'){
			int x=read(),y=read();link(x,y);
		}else{//断开这条边,查询两侧子树大小之积,再连接
			int x=read(),y=read();
			cut(x,y);makeroot(x);makeroot(y);
			cout<<1ll*tr[x].sz*tr[y].sz<<"\n";link(x,y);
		}	
	}
	return 0;
}

强强建模

[NOI2014] 魔法森林。

这题真厉害!

有两个值,要求 max⁡ai+max⁡bi\max_{a_i}+\max_{b_i}。

同时处理两个是难做的,考虑先对 aia_i 排序,再处理 bib_i。

我们将 aia_i 排序后,按照 aia_i 的大小顺序插入对应边。

这等价于固定 maxaimax_{a_i} 为当前枚举到的 aia_i。

然后就需要支持两个操作:

  • 查询 max⁡bi\max_{b_i}。

  • 连接的边成环后,删除当前的 max⁡bi\max_{b_i}。

显然可以使用 LCT 实现。

但是边权在点上,我们可以给每条边新建一个点,记录其两端。

这样的话,我们删除这条边的时候,只需要 splay(id)splay(id),然后断开父亲就好,因为其两端一定是两个端点。

代码
C++
#include<bits/stdc++.h>
#define check(x) (tr[tr[x].fa].ch[0]!=x && tr[tr[x].fa].ch[1]!=x) //当前点是否为根节点
#define son(x) (tr[tr[x].fa].ch[1]==x) //当前点是父亲的哪一个儿子
using namespace std;

const int N=2e5+5;
struct edge{int x,y,numa,numb;}b[N];
struct node{int ch[2],fa,val,maxn,id,tag;}tr[N];

inline int read(){
	int s=0;char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s;
}

inline bool operator < (edge x,edge y){return x.numa<y.numa;}

inline void pushup(int p){//更新最大值 & 编号
	tr[p].maxn=tr[p].val;tr[p].id=p;
	if(tr[p].ch[0] && tr[tr[p].ch[0]].maxn>tr[p].maxn) tr[p].maxn=tr[tr[p].ch[0]].maxn,tr[p].id=tr[tr[p].ch[0]].id;
	if(tr[p].ch[1] && tr[tr[p].ch[1]].maxn>tr[p].maxn) tr[p].maxn=tr[tr[p].ch[1]].maxn,tr[p].id=tr[tr[p].ch[1]].id;
}

inline void rotate(int x){//旋转
	int y=tr[x].fa,z=tr[y].fa;int k=son(x);
	tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;//x & z
	tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;//y 与新的儿子
	tr[y].fa=x;tr[x].ch[k^1]=y;//x 与 y
	pushup(y);pushup(x);
}

inline void reverse(int x){//反转 x
	tr[x].tag^=1;
	swap(tr[x].ch[0],tr[x].ch[1]);
}

inline void pushdown(int x){//下放 x 的懒标记
	if(tr[x].tag){
		tr[x].tag=0;
		reverse(tr[x].ch[0]);
		reverse(tr[x].ch[1]);
	}
}

inline void push(int x){//下放一条链的懒标记
	if(tr[x].fa) push(tr[x].fa);
	pushdown(x);
}

inline void splay(int x){//将 x 转到当前链的根
	push(x);
	while(!check(x)){
		int y=tr[x].fa,z=tr[y].fa;
		if(!check(y)){
			if(son(x) xor son(y)) rotate(x);
			else rotate(y);
		}
		rotate(x);
	}
	pushup(x);
}

inline void access(int x){//打通 x 到根
	for(int son=0;x;son=x,x=tr[x].fa){
		splay(x);tr[x].ch[1]=son;pushup(x);
	}
}

inline void makeroot(int x){//把 x 变成整棵树的根
	access(x);splay(x);reverse(x);
}

inline void link(int x,int y){//连接 x -> y
	makeroot(x);tr[x].fa=y;
}

inline void split(int x,int y){//只保留 x -> y 的路径
	makeroot(x);access(y);splay(y);
}

inline int findroot(int x){//找到 x 的根节点
	access(x);splay(x);
	while(tr[x].ch[0]) x=tr[x].ch[0];
	return x;
}

int main(){
	int n,m;cin>>n>>m;
	for(int i=1;i<=m;i++) b[i]={read(),read(),read(),read()};
	sort(b+1,b+m+1);int ans=2147483647;
	for(int i=1;i<=m;i++){
		tr[i+n].val=b[i].numb;
		if(findroot(b[i].x)!=findroot(b[i].y)){link(i+n,b[i].x);link(i+n,b[i].y);} //两个点不连通,直接连接
		else{//加上这条边之后成环了,可以在当前环中删除一条最劣的边
			split(b[i].x,b[i].y);int tmp=tr[b[i].y].maxn;
			if(tmp<b[i].numb) continue;//当前的这条边更劣,不加,维持原状
			int id=tr[b[i].y].id;splay(id);tr[tr[id].ch[0]].fa=tr[tr[id].ch[1]].fa=0;//清空父亲,断边
			link(i+n,b[i].x);link(i+n,b[i].y);//连上新的边
		}
		if(findroot(1)==findroot(n)){split(1,n);ans=min(ans,tr[n].maxn+b[i].numa);}//若联通,更新答案
	}
	cout<<((ans==2147483647)?-1:ans);
	return 0;
}