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

Advertisement

spot_img

Why Viral Kitchen Gadgets...

📢 As an Amazon Associate, I earn from qualifying...

Curt Cignetti has built...

College football is often dictated by resources, prestige, and...

I Tried Viral Kitchen...

📢 As an Amazon Associate, I earn from qualifying...

Top House Budget Dem...

In response to growing scrutiny over the reliability and...

I Tried Viral Kitchen...

📢 As an Amazon Associate, I earn from qualifying...

‘Feeling good’ – Shams...

Basketball fans can breathe a collective sigh of relief...

Why Viral Kitchen Gadgets Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Curt Cignetti has built what Lincoln Riley can’t at USC: a team that knows how to win

College football is often dictated by resources, prestige, and five-star recruiting classes. Yet, the stark contrast between Curt Cignetti’s Indiana Hoosiers and Lincoln Riley’s...

I Tried Viral Kitchen Gadgets For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Top House Budget Dem Boyle: I’d Support ‘Major Appropriation’ for FAA Overhaul

In response to growing scrutiny over the reliability and resilience of national aviation infrastructure, Representative Brendan Boyle (D-PA) has publicly signaled strong support for...

I Tried Viral Kitchen Gadgets For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

‘Feeling good’ – Shams Charania provides Luka Doncic, Austin Reaves injury updates

Basketball fans can breathe a collective sigh of relief following the latest Luka Doncic Austin Reaves injury updates delivered by trusted NBA insider Shams...

The Ultimate Viral Kitchen Gadgets Guide You Didnt Know You Needed

📢 As an Amazon Associate, I earn from qualifying purchases.Most products in this category disappoint within weeks. Cheap materials, vague instructions, and results that...

Connor Hellebuyck goes to Bass Pro Shops opening in NJ — one day after brutal Winnipeg booing

Professional sports can be an emotional rollercoaster, and few understand that reality better this week than Winnipeg Jets star goaltender Connor Hellebuyck. Just twenty-four...

Why Viral Kitchen Gadgets Is Currently Breaking the Internet

em> If you had told me six months ago that a single purchase would change how I approach my daily routine, I would not have...