题解 \(by\;zj\varphi\)
一道树剖的题
先对于原图求出一棵最小生成树,求出来的这棵树中的边定为树边,其它边叫非树边
那么对于一条非树边,它要成为最小生成树上的边,权值只能为连接它两个端点的树上路径最小值减 \(1\),因为,他和这些树边构成了一个环,最小生成树要在环上去掉最大的一条边,所以只要这条边比树上路径最大值小即可
而对于树边,发现它要比它参与构成的所有环中的非树边权值要小,因为非树边在其环中权职一定为最大的,若树边比它大,就不会在最小生成树中了
对于这两种操作,我们都可以用树剖来做,就是一个板子
注意:维护最小值的线段树维护的是边下放到它终点后的点权,记得在 \(lca\) 处不要多修改
Code
#include<bits/stdc++.h>
#define ri register signed
#define p(i) ++i
using namespace std;
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++
template<typename T>inline void read(T &x) {
ri f=1;x=0;register char ch=gc();
while(ch<'0'||ch>'9') {if (ch=='-') f=0;ch=gc();}
while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
x=f?x:-x;
}
}
using IO::read;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
static const int N=7e4+7,M=1e5+7,INF=2e9+7;
int first[N],fa[N],W[N],hs[N],siz[N],dfn[N],top[N],bc[N],ans[M],dep[N],vis[M],tot,t=1,n,m,opt,num;
struct edge{int v,nxt,w;}e[N<<1];
struct edgei{int u,v,w,id;}G[M],AS[M];
inline int cmp(edgei e1,edgei e2) {return e1.w<e2.w;}
inline void add(int u,int v,int w) {
e[t].v=v,e[t].w=w,e[t].nxt=first[u],first[u]=t++;
e[t].v=u,e[t].w=w,e[t].nxt=first[v],first[v]=t++;
}
int find(int x) {return fa[x]==x?x:fa[x]=find(fa[x]);}
inline void kruskal() {
sort(G+1,G+m+1,cmp);
for (ri i(1);i<=m;p(i)) {
int u=G[i].u,v=G[i].v,w=G[i].w;
if (find(u)==find(v)) continue;
vis[G[i].id]=1;
fa[find(u)]=find(v);
add(u,v,w);
}
}
struct Segmx{
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define up(x) T[x].mx=cmax(T[ls(x)].mx,T[rs(x)].mx);
struct segmenttree{int mx;}T[N<<2];
void build(int x,int l,int r) {
if (l==r) return (void)(T[x].mx=W[bc[l]]);
int mid(l+r>>1);
build(ls(x),l,mid);
build(rs(x),mid+1,r);
up(x);
}
int querymx(int x,int l,int r,int lt,int rt) {
if (l<=lt&&rt<=r) return T[x].mx;
int mid(lt+rt>>1),res(0);
if (l<=mid) res=cmax(res,querymx(ls(x),l,r,lt,mid));
if (r>mid) res=cmax(res,querymx(rs(x),l,r,mid+1,rt));
return res;
}
}Tx;
struct Segmn{
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
struct segmenttree{int mn,lz;segmenttree(){mn=INF;}}T[N<<2];
inline void down(int x) {
if (!T[x].lz) return;
T[ls(x)].mn=cmin(T[x].mn,T[ls(x)].mn);
T[rs(x)].mn=cmin(T[x].mn,T[rs(x)].mn);
T[ls(x)].lz=T[rs(x)].lz=1;
T[x].lz=0;
}
void update(int x,int k,int l,int r,int lt,int rt) {
if (l<=lt&&rt<=r) {
T[x].mn=cmin(T[x].mn,k);
return (void)(T[x].lz=1);
}
down(x);
int mid(lt+rt>>1);
if (l<=mid) update(ls(x),k,l,r,lt,mid);
if (r>mid) update(rs(x),k,l,r,mid+1,rt);
}
int querymn(int x,int k,int l,int r) {
if (l==r) return T[x].mn;
int mid(l+r>>1);
down(x);
if (k<=mid) return querymn(ls(x),k,l,mid);
else return querymn(rs(x),k,mid+1,r);
}
}Tn;
void dfs1(int x,int f) {
fa[x]=f;
siz[x]=1;
for (ri i(first[x]),v;i;i=e[i].nxt) {
if ((v=e[i].v)==f) continue;
W[v]=e[i].w,dep[v]=dep[x]+1;
dfs1(v,x);
siz[x]+=siz[v];
if (siz[v]>siz[hs[x]]) hs[x]=v;
}
}
void dfs2(int x,int tp) {
bc[dfn[x]=p(tot)]=x;
top[x]=tp;
if (hs[x]) dfs2(hs[x],tp);
for (ri i(first[x]),v;i;i=e[i].nxt) {
if ((v=e[i].v)==fa[x]||v==hs[x]) continue;
dfs2(v,v);
}
}
inline int solve(int u,int v,int w) {
ri res(0);
while(top[u]!=top[v]) {
if (dep[top[u]]<dep[top[v]]) swap(u,v);
res=cmax(res,Tx.querymx(1,dfn[top[u]],dfn[u],1,n));
Tn.update(1,w,dfn[top[u]],dfn[u],1,n);
u=fa[top[u]];
}
if (dep[u]<dep[v]) swap(u,v);
if (dfn[u]>dfn[v]) {
res=cmax(res,Tx.querymx(1,dfn[v]+1,dfn[u],1,n));
Tn.update(1,w,dfn[v]+1,dfn[u],1,n);
}
return res-1;
}
inline int main() {
// FI=freopen("nanfeng.in","r",stdin);
// FO=freopen("nanfeng.out","w",stdout);
read(n),read(m),read(opt);
for (ri i(1);i<=n;p(i)) fa[i]=i;
for (ri i(1);i<=m;p(i))
read(G[i].u),read(G[i].v),read(G[i].w),G[i].id=i,AS[i]=G[i];
kruskal();
dfs1(1,0),dfs2(1,1);
Tx.build(1,1,n);
for (ri i(1);i<=m;p(i)) {
int u=AS[i].u,v=AS[i].v,w=AS[i].w;
if (!dfn[u]) continue;
if (vis[i]) continue;
ans[i]=solve(u,v,w);
}
for (ri i(1);i<=m;p(i)) {
int u=AS[i].u,v=AS[i].v,w=AS[i].w,lw=dep[u]<dep[v]?v:u;
if (!dfn[u]) continue;
if (vis[i]) ans[i]=Tn.querymn(1,dfn[lw],1,n)-1;
}
for (ri i(1);i<=m;p(i)) printf("%d ",ans[i]==INF-1?-1:ans[i]);
return 0;
}
}
int main() {return nanfeng::main();}