#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const ll MOD = 1000000007;
const int MAXN = 100010;
vector<pii> adj[MAXN];
int sub[MAXN], diam, m1[MAXN], m2[MAXN], og[MAXN];
bool vis[MAXN];
void dfs(int node){
vis[node] = 1;
for(auto x: adj[node]){
if(vis[x.fi])continue;
dfs(x.fi);
sub[node] = max(sub[x.fi] + x.se,sub[node]);
}
}
int reroot(int node){
vis[node] = 1;
for(auto x: adj[node]){
if(sub[x.fi]+x.se>=m1[node]){
m2[node] = m1[node];
m1[node] = sub[x.fi]+x.se;
}else if(sub[x.fi]+x.se > m2[node]){
m2[node] = sub[x.fi]+x.se;
}
}
diam = max(diam, m1[node] + m2[node]);
int ans = m1[node]; og[node] = sub[node];
for(auto x: adj[node]){
if(vis[x.fi])continue;
if(m1[node] == sub[x.fi]+x.se)sub[node] = m2[node];
else sub[node] = m1[node];
ans = min(ans,reroot(x.fi));
sub[node] = og[node];
}
// cout << "node "<<node <<" m1 "<<m1[node]<<"\n";
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
for(int i = 0; i < M; i++){
adj[A[i]].pb({B[i],T[i]});
adj[B[i]].pb({A[i],T[i]});
}
int k,m=0,mm=0, mmm = 0;
for(int i = 0; i < N; i++)if(!vis[i])dfs(i);
for(int i = 0; i < N; i++)vis[i] = 0;
for(int i = 0; i < N; i++)if(!vis[i]){
k = reroot(i);
// cout << "i "<<i <<" k "<<k<<"\n";
if(k >= m){
mmm = mm; mm = m; m = k;
}else if(k >= mm){
mmm = mm; mm = k;
}else if(k > mmm){
mmm = k;
}
}
return max({2*L+mmm+mm, L + m+mm, diam});
}
/*
int main(){
int a,b,c;
cin >> a >> b >> c;
int k[b],j[b],l[b];
for(int i = 0; i < b; i++){
cin >> k[i] >> j[i] >> l[i];
}
cout << travelTime(a,b,c,k,j,l)<<"\n";
}
/*
12 8 2
0 8 4
8 2 2
2 7 4
5 11 3
5 1 7
1 3 1
1 9 5
10 6 3
*/
Compilation message
dreaming.cpp:88:1: warning: "/*" within comment [-Wcomment]
88 | /*
|
/tmp/ccNF8IiX.o: In function `main':
grader.c:(.text.startup+0xa7): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status