#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
#define F first
#define S second
//#define endl '\n'
//#define int long long
using namespace std;
vt<vt<pii>> g;
vt<int> dp1, dp2, dp1_v, dp2_v;
vt<int> cmp;
void dfs1(int v, int c, int p = -1)
{
cmp[v] = c;
for(pii to : g[v])
{
if (to.F == p) continue;
dfs1(to.F, c, v);
if (dp1[to.F] + to.S > dp1[v])
{
swap(dp1[v], dp2[v]), swap(dp1_v[v], dp2_v[v]);
dp1[v] = dp1[to.F] + to.S, dp1_v[v] = to.F;
}
else if (dp1[to.F] + to.S > dp2[v])
dp2[v] = dp1[to.F] + to.S, dp2_v[v] = to.F;
}
}
void dfs2(int v, int p = -1, int d = 0)
{
for(pii to : g[v])
{
if (to.F == p) continue;
if (to.F == dp1_v[v]) dfs2(to.F, v, to.S + max(d, dp2[v]));
else dfs2(to.F, v, to.S + max(d, dp1[v]));
}
dp1[v] = max(dp1[v], d);
}
int travelTime(int N, int M, int L, vt<int> A, vt<int> B, vt<int> T)
{
g.resize(N), dp1.resize(N), dp2.resize(N), dp1_v.resize(N), dp2_v.resize(N);
for(int i = 0; i < M; i++)
g[A[i]].pb({B[i], T[i]}), g[B[i]].pb({A[i], T[i]});
cmp.resize(N);
int c = 0;
for(int i = 0; i < N; i++)
if (!cmp[i]) c++, dfs1(i, c), dfs2(i);
vt<pii> min_dp(c, {INT_MAX, 0});
for(int i = 0; i < N; i++)
if (dp1[i] < min_dp[cmp[i] - 1].F) min_dp[cmp[i] - 1] = {dp1[i], i};
sort(all(min_dp));
for(int i = 0; i < c - 1; i++)
{
g[min_dp[i].S].pb({min_dp[c - 1].S, L});
g[min_dp[c - 1].S].pb({min_dp[i].S, L});
}
dp1.clear(), dp2.clear(), dp1_v.clear(), dp2_v.clear();
dp1.resize(N), dp2.resize(N), dp1_v.resize(N), dp2_v.resize(N);
dfs1(0, 0), dfs2(0);
int mx = 0;
for(int i = 0; i < N; i++) mx = max(mx, dp1[i]);
return mx;
/*
for(int i = 0; i < N; i++) cout << dp1[i] << " ";
cout << endl;
for(int i = 0; i < N; i++) cout << dp1_v[i] << " ";
cout << endl;
for(int i = 0; i < N; i++) cout << dp2[i] << " ";
cout << endl;
for(int i = 0; i < N; i++) cout << dp2_v[i] << " ";
cout << endl;
*/
}
/**
main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << travelTime(12, 8, 2, {0, 8, 2, 5, 5, 1, 1, 10}, {8, 2, 7, 11, 1, 3, 9, 6}, {4, 2, 4, 3, 7, 1, 5, 3});
}
/**/
Compilation message
dreaming.cpp:93:1: warning: "/*" within comment [-Wcomment]
93 | /**/
|
/usr/bin/ld: /tmp/cceOoM5x.o: in function `main':
grader.c:(.text.startup+0xd1): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status