Sponsors

Codeforces Round #427 (Div. 2), problem: (F) Roads in the Kingdom Solution In C/C++

#include <bits/stdc++.h>

using namespace std;

int n;

int to[500000], pre[500000], cost[500000], last[250000], en;

long long wxs;

void addedge(int f, int t, int w)
{
to[++en] = t;
cost[en] = w;
pre[en] = last[f];
last[f] = en;
}

int Stack[250000], vis[250000], top;

int ring[250000], rs;

long long val[250000], edge[250000];

bool dfs(int now, int p)
{
Stack[++top] = now;
vis[now] = top;
for (int i = last[now]; i; i = pre[i])
{
if (to[i] == p)
{
continue;
}
if (vis[to[i]] > 0)
{
for (int j = vis[to[i]]; j <= top; ++j)
{
ring[++rs] = Stack[j];
}
return 1;
}
if (dfs(to[i], now))
{
return 1;
}
}
–top;
vis[now] = 2147483647;
return 0;
}

int taboo1, taboo2;

long long dfs2(int now, int p)
{
long long fir = 0, sec = 0;
for (int i = last[now]; i; i = pre[i])
{
if (to[i] == p)
{
continue;
}
if (to[i] == taboo1)
{
continue;
}
if (to[i] == taboo2)
{
continue;
}
long long th = dfs2(to[i], now) + cost[i];
if (th >= fir)
{
sec = fir;
fir = th;
} else if (th > sec)
{
sec = th;
}
}
wxs = max(wxs, fir + sec);
return fir;
}

long long to_l[250000], to_r[250000];
long long mx_l[250000], mx_r[250000];

long long ans;

int main()
{
scanf(“%d”, &n);
for (int i = 1; i <= n; ++i)
{
int u, v, w;
scanf(“%d%d%d”, &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
dfs(1, 0);
ring[0] = ring[rs];
ring[rs + 1] = ring[1];
long long sum = 0;
for (int i = 1; i <= rs; ++i)
{
taboo1 = ring[i – 1];
taboo2 = ring[i + 1];
val[i] = dfs2(ring[i], 0);
for (int j = last[ring[i]]; j; j = pre[j])
{
if (to[j] == ring[i – 1])
{
edge[i – 1] = cost[j];
sum += cost[j];
}
}
}
edge[rs] = edge[0];
val[0] = val[rs];
val[rs + 1] = val[1];
long long tmp = 0, tmp2 = 0;
for (int i = 1; i <= rs; ++i)
{
to_l[i] = max(to_l[i – 1], tmp + val[i]);
mx_l[i] = max(mx_l[i – 1], tmp2 + val[i]);
tmp += edge[i];
tmp2 = max(tmp2, val[i]) + edge[i];
}
tmp = tmp2 = 0;
for (int i = rs; i >= 1; –i)
{
to_r[i] = max(to_r[i + 1], tmp + val[i]);
mx_r[i] = max(mx_r[i + 1], tmp2 + val[i]);
tmp += edge[i – 1];
tmp2 = max(tmp2, val[i]) + edge[i – 1];
}
ans = mx_l[rs];
for (int i = 1; i < rs; ++i)
{
long long nans = max(to_l[i] + to_r[i + 1] + edge[0], max(mx_l[i], mx_r[i + 1]));
ans = min(ans, nans);
}
ans = max(ans, wxs);
cout << ans << endl;
}

Do ferrets cough up...

Understanding Ferret Health: Do Ferrets Cough Up Hairballs? For many...

Lenovo goes all in...

Lenovo's AI Leap at CES 2026: The Future Is...

Why 99% of scientists...

The Unanimous Truth: Why 99% of Scientists Believe in...

Charles Cross agrees to...

Charles Cross Secures Future with Massive Four-Year Extension with...

Samsung Display Unveils New...

Samsung Display Prepares to Dazzle CES 2026 with Next-Gen...

What do male octopus...

The Fateful Finale: What Happens to Male Octopuses After...

Do ferrets cough up hairballs?

Understanding Ferret Health: Do Ferrets Cough Up Hairballs? For many small pet owners, the sight or sound of a cat coughing up a hairball is...

Lenovo goes all in on AI with concepts at CES 2026

Lenovo's AI Leap at CES 2026: The Future Is Conceptual CES is renowned for launching the must-have gadgets of the year, but the annual tech...

Why 99% of scientists believe in evolution

The Unanimous Truth: Why 99% of Scientists Believe in Evolution In the public discourse, the debate between evolutionary theory and creationism often appears balanced. However,...

Charles Cross agrees to four-year extension with Seahawks

Charles Cross Secures Future with Massive Four-Year Extension with Seahawks The Seattle Seahawks organization demonstrated its commitment to building a formidable foundation by securing one...

Samsung Display Unveils New OLED Tech for Robots & Wearables at CES 2026

Samsung Display Prepares to Dazzle CES 2026 with Next-Gen OLED Innovations As the tech world gears up for CES 2026, all eyes are turning toward...

What do male octopus do after mating?

The Fateful Finale: What Happens to Male Octopuses After Mating? The ocean is home to countless wonders, but few natural phenomena are as dramatically tragic...

From Broadway to Banana Ball! Derek Klena Joins Savannah Bananas in Viral Baseball League

The Ultimate Crossover: Broadway Star Derek Klena Joins the Savannah Bananas In a surprising twist that perfectly captures the unique spirit of the viral baseball...

Stock Market Highlights 1 January 2026: Sensex, Nifty close flat on first trading day of new year

A Flat Start to the New Year: Sensex and Nifty End January 1, 2026 Trading Muted The dawn of the new year, January 1, 2026,...

Why the Mineral Resources share price is up 10% in a month

The Forces Behind Mineral Resources’ 10% Share Price Surge Shareholders of Mineral Resources Ltd (ASX: MIN) are breathing a collective sigh of relief, if not...