Sponsors

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];
struct adjacency_list
{
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;
}

One to watch: Macquarie...

Macquarie Predicts Double-Digit Returns for ASX Healthcare Stock Macquarie Group,...

THE FREEPER CANTEEN –...

The Freeper Canteen: A Look That Up, It Must...

215% PENGU Rally Incoming?...

215% PENGU Rally Incoming? Analyst Predicts Imminent Breakout The cryptocurrency...

Junk is the new...

Junk is the New Punk: Why We're Falling Back...

Are weighted vests good...

Unlocking the Power of AI-Generated Content for SEO In today's...

Are weighted vests good...

Unlocking the Power of AI-Generated Content: A Boon or...

One to watch: Macquarie tips double-digit returns for this ASX healthcare stock

Macquarie Predicts Double-Digit Returns for ASX Healthcare Stock Macquarie Group, a prominent Australian financial services company, has issued a bullish prediction for a specific ASX-listed...

THE FREEPER CANTEEN – The Look That Up, It Must Be On My Bucket List Quiz – Wednesday, August 27, 2025

The Freeper Canteen: A Look That Up, It Must Be On My Bucket List The Freeper Canteen, a unique online community, has recently launched a...

215% PENGU Rally Incoming? Analyst Says Token ‘Inches’ From Next Leg Up

215% PENGU Rally Incoming? Analyst Predicts Imminent Breakout The cryptocurrency market is constantly fluctuating, and predicting the next big move is a challenge. However, one...

Junk is the new punk: Why we’re falling back in love with retro tech

Junk is the New Punk: Why We're Falling Back in Love with Retro Tech In a world dominated by sleek, minimalist designs and cutting-edge technology,...

Are weighted vests good for bones and muscle? Fact-checking a fitness trend – NPR

Unlocking the Power of AI-Generated Content for SEO In today's digital landscape, search engine optimization (SEO) is paramount for online success. But creating high-quality,...

Are weighted vests good for bones and muscle? Fact-checking a fitness trend – NPR

Unlocking the Power of AI-Generated Content: A Boon or Bane for SEO? The world of search engine optimization (SEO) is constantly evolving, and the recent...

TSMC cuts Chinese tools from cutting-edge chip production to avoid US ire – Nikkei Asia

TSMC Ditches Chinese Tools for Cutting-Edge Chip Production Taiwan Semiconductor Manufacturing Company (TSMC), the world's leading chipmaker, is reportedly phasing out Chinese-made equipment from its...

Google Messages beta rolling out QR code key verification – 9to5Google

Google Messages Adds QR Code Key Verification for Enhanced Security Google is bolstering the security of its Messages app with a new feature rolling out...

A Secretive US Space Plane Will Soon Test Quantum Navigation Technology

## US Space Plane to Test Revolutionary Quantum Navigation Technology The United States is on the cusp of a significant leap forward in space navigation....