倍增求链上点最大值
inline int LCA(int x,int y)
{
int maxn=max(w[x],w[y]);
if (deep[x]>deep[y]) swap(x,y);
int k=log2(deep[y]);
for (int i=k;i>=0;i--)
if (deep[f[i][y]]>=deep[x])
{
maxn=max(maxn,maxx[i][y]);
y=f[i][y];
}
if (x==y) return max(maxn,w[x]);
for (int i=k;i>=0;i--)
if (f[i][y]!=f[i][x])
{
maxn=max(maxn,maxx[i][x]);
maxn=max(maxn,maxx[i][y]);
y=f[i][y];
x=f[i][x];
}
return max(maxn,maxx[0][x]);
}
inline void dfs(int x,int fa)
{
deep[x]=deep[fa]+1;
f[0][x]=fa;
maxx[0][x]=w[fa];
for (int i=1;(1<<i)<=deep[x];i++)
{
f[i][x]=f[i-1][f[i-1][x]];
maxx[i][x]=max(maxx[i-1][x],maxx[i-1][f[i-1][x]]);
}
for (int i=head[x];i;i=ed[i].next)
if (ed[i].e!=fa) dfs(ed[i].e,x);
return ;
}
倍增求链上边最大值
inline int LCA(int x,int y)
{
int maxx=0;
if (deep[x]>deep[y]) swap(x,y);
int k=log2(deep[y]);
for (int i=k;i>=0;i--)
if (deep[f[i][y]]>=deep[x])
{
maxx=max(maxv[i][y],maxx);
y=f[i][y];
}
if (x==y) return maxx;
for (int i=k;i>=0;i--)
if (f[i][y]!=f[i][x])
{
maxx=max(maxv[i][y],maxx);
maxx=max(maxv[i][x],maxx);
y=f[i][y];
x=f[i][x];
}
return max(maxx,max(maxv[0][x],maxv[0][y]));
}
inline void dfs2(int x,int fa)
{
f[0][x]=fa;
for (int i=1;(1<<i)<=deep[x];i++)
{
f[i][x]=f[i-1][f[i-1][x]];
maxv[i][x]=max(maxv[i-1][x],maxv[i-1][f[i-1][x]]);
}
for (int i=head[x];i;i=ed[i].net)
if (ed[i].e!=fa) dfs2(ed[i].e,x);
return ;
}
inline void dfs1(int x,int fa)
{
deep[x]=deep[fa]+1;
for (int i=head[x];i;i=ed[i].net)
if (ed[i].e!=fa) dfs1(ed[i].e,x);
else maxv[0][x]=ed[i].v;
return ;
}