# Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals), problem: (F) Best Edge Weight Solution in C/C++

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <map>
#include <ctime>
#include <vector>
#define INF 2000000000
#define N 200010

using namespace std;
bool in_MST[N];
int jump[N][18] , maxl[N][18] , fa[N];
int ans[N] , fir[N] , tot , rep[N] , n , m , depth[N];
struct EDGE
{
int u , v , val , id;
}edge[N];
{
int end , len , next , id;
}AL , alis[2 * N];
bool comp (const EDGE & a , const EDGE & b){return a.val < b.val;}

void build(int u , int v , int l , int p)
{
alis[++ tot].next = fir[u];
fir[u] = tot;
alis[tot].end = v;
alis[tot].len = l;
alis[tot].id = p;
}
int query(int u , int v)
{
int ret = -1;
if(depth[u] < depth[v])swap(u , v);
for(int i = 17;i >= 0;i –)if(depth[jump[u][i]] >= depth[v])
{
ret = max(ret , maxl[u][i]);
u = jump[u][i];
}
if(u == v)return ret;
for(int i = 17;i >= 0;i –)if(jump[u][i] != jump[v][i])
{
ret = max(ret , maxl[u][i]);
ret = max(ret , maxl[v][i]);
u = jump[u][i];
v = jump[v][i];
}
ret = max(ret , maxl[u][0]);
ret = max(ret , maxl[v][0]);
return ret;
}
void DFS_multiplication(int x)
{
for(int u = fir[x];u;u = alis[u].next)if(!depth[alis[u].end])
{
depth[alis[u].end] = depth[x] + 1;
jump[alis[u].end][0] = x;
maxl[alis[u].end][0] = alis[u].len;
rep[alis[u].end] = alis[u].id;
DFS_multiplication(alis[u].end);
}
}
int getfa(int x)
{
if(fa[x] == x)return x;
return fa[x] = getfa(fa[x]);
}
void init()
{
//freopen(“cs.in”,”r”,stdin);
//freopen(“cs.out”,”w”,stdout);

scanf(“%d%d”,&n,&m);
for(int i = 1;i <= m;i ++)scanf(“%d%d%d”,&edge[i].u,&edge[i].v,&edge[i].val) , edge[i].id = i;
for(int i = 1;i <= n;i ++)fa[i] = i;
for(int i = 0;i <= 17;i ++)for(int j = 0;j <= n;j ++)maxl[j][i] = -1;
}
void MST()
{
sort(edge + 1 , edge + 1 + m , comp);

for(int i = 1;i <= n;i ++)fa[i] = i;

for(int i = 1;i <= m;i ++)
if(getfa(edge[i].u) != getfa(edge[i].v))
{
fa[getfa(edge[i].u)] = getfa(edge[i].v);
in_MST[i] = 1;
build(edge[i].u , edge[i].v , edge[i].val , edge[i].id);
build(edge[i].v , edge[i].u , edge[i].val , edge[i].id);
}

}
void multiplication()
{
depth[1] = 1;
DFS_multiplication(1);

for(int i = 1 ;i <= 17;i ++)for(int j = 1;j <= n;j ++)
{
jump[j][i] = jump[jump[j][i – 1]][i – 1];
maxl[j][i] = max(maxl[j][i – 1] , maxl[jump[j][i – 1]][i – 1]);
}
}
void update(int u , int v , int val)
{
int lca , ori_u = u , ori_v = v;
if(depth[u] < depth[v])swap(u , v);
for(int i = 17;i >= 0;i –)if(depth[jump[u][i]] >= depth[v])u = jump[u][i];
for(int i = 17;i >= 0;i –)if(jump[u][i] != jump[v][i])u = jump[u][i] , v = jump[v][i];
if(u != v)lca = jump[u][0];
else lca = u;
u = getfa(ori_u) , v = getfa(ori_v);
for(;depth[u] > depth[lca];u = getfa(u))
{
ans[rep[u]] = val – 1;
fa[u] = jump[u][0];
}
for(;depth[v] > depth[lca];v = getfa(v))
{
ans[rep[v]] = val – 1;
fa[v] = jump[v][0];
}

}
void solve()
{
for(int i = 1;i <= m;i ++)ans[i] = -1;
for(int i = 0;i <= n;i ++)fa[i] = i;
for(int i = 1;i <= m;i ++)if(!in_MST[i])
{
ans[edge[i].id] = query(edge[i].u , edge[i].v) – 1;
update(edge[i].u , edge[i].v , edge[i].val);
}
}
int main ()
{
init();

MST();

multiplication();

solve();

for(int i = 1;i <= m;i ++)printf(“%d “,ans[i]);
return 0;
}

(Visited 18 times, 1 visits today)