Wednesday, April 17, 2024

# 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;
}

## Celebrating Black History Month:...

As February unfolds, so does the annual celebration of...

## The Path to Self-Mastery:...

Embarking on a journey of self-mastery and breaking free...

## Wizards of Waverly Place...

In a spellbinding announcement, Disney has officially revealed that...

## Jim Irsay’s Reported ‘Suspected...

In a shocking turn of events last month, Jim...

Coachella Valley Music and Arts Festival, one of the...

## 2024 Taiwan Election: Pivotal...

As Taiwan gears up for its 2024 presidential election,...

### Celebrating Black History Month: Past, Future

As February unfolds, so does the annual celebration of Black History Month, a time to reflect on the profound contributions, resilience, and rich cultural...

### The Path to Self-Mastery: Lessons from Book ‘The Mountain Is You’

Embarking on a journey of self-mastery and breaking free from self-sabotage is a transformative process that requires dedication and conscious effort. Brianna Wiest's insightful...

### Wizards of Waverly Place Cast Reunites for Enchanting Revival

In a spellbinding announcement, Disney has officially revealed that the beloved fantasy series "Wizards of Waverly Place" is set for a magical comeback, featuring...

### Jim Irsay’s Reported ‘Suspected Overdose’: A Closer Look

In a shocking turn of events last month, Jim Irsay, the owner of the Indianapolis Colts, was reportedly found unresponsive at his home in...

### Coachella 2024: Iconic headliners, unforgettable musical experience!

Coachella Valley Music and Arts Festival, one of the most iconic and eagerly anticipated music festivals globally, has just dropped its highly anticipated lineup...

### 2024 Taiwan Election: Pivotal Moment in Political Landscape

As Taiwan gears up for its 2024 presidential election, the political landscape is buzzing with anticipation and fervor. With the island nation situated at...

### Michael Strahan’s Daughter’s Medulloblastoma Diagnosis

In a recent and heartbreaking revelation, Michael Strahan, former NFL star and television personality, shared the devastating news of his daughter Isabella's diagnosis with...

### Michigan vs. Washington: The 2024 National Championship Clash

In a highly anticipated matchup, the 2024 National Championship will witness a clash of football titans as the Michigan Wolverines square off against the...

### Jason Kelce: Unmasking the Unconventional NFL Icon

In the world of professional football, where conformity often takes center stage, one player stands out as a beacon of individuality, both on and...