#include<bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define int long long
const int maxN = (int)1e5 + 5;
vector<vector<pair<int,int>>>adj(maxN);
int vis[maxN], dist[maxN], parent[maxN];
int mx = 0, source = -1;
int ans = 0;
void dfs(int u, int p = -1, int d = 0){
vis[u] = 1;
parent[u] = p;
dist[u] = d;
ans = max(ans, d);
if (mx < d){
mx = d;
source = u;
}
for (auto x : adj[u]){
if (x.first == p){
continue;
}
dfs(x.first, u, x.second + d);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
for (int i = 0; i < M; i++){
//cout<<A[i]<<" "<<B[i]<<endl;
adj[A[i]].push_back({B[i], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
vector<pair<int,int>>centroids;
for (int i = 0; i < N; i++){
if (vis[i])continue;
mx = -1, source = -1;
dfs(i);
int S = source;
mx = -1;
dfs(S);
int E = source;
vector<pair<int,int>>path;
// cout<<S<<" "<<E<<endl;
while(E != S){
path.push_back({E, dist[E]});
E = parent[E];
}
path.push_back({E, dist[E]});
reverse(path.begin(), path.end());
int diameter = path.back().second;
int best_node = path[0].first, best_dist = 1e18;
for(int j = 0; j < path.size(); j++){
if(max(path[j].second, diameter - path[j].second) < best_dist){
best_node = path[j].first;
best_dist = max(path[j].second, diameter - path[j].second);
}
}
centroids.push_back({best_dist, best_node});
}
// for (auto j : centroids)cout<<j.first<<" "<<j.second<<endl;
sort(centroids.rbegin(), centroids.rend());
if (centroids.size() > 2)ans = max(ans, L + centroids[0].first + centroids[1].first);
if (centroids.size() > 3)ans = max(ans, L * 2 + centroids[1].first + centroids[2].first);
return ans;
}
/*
signed main(){
int N = 12;
int M = 8;
int L = 2;
int A[8] = {0,8,2,5,5,1,1,10};
int B[8] = {8,2,7,11,1,3,9,6};
int T[8] = {4,2,4,3,7,1,5,3};
cout<<travelTime(N,M,L,A,B,T);
}
*/
Compilation message
dreaming.cpp: In function 'long long int travelTime(long long int, long long int, long long int, long long int*, long long int*, long long int*)':
dreaming.cpp:52:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int j = 0; j < path.size(); j++){
| ~~^~~~~~~~~~~~~
/usr/bin/ld: /tmp/cc7VESSr.o: in function `main':
grader.c:(.text.startup+0xd1): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status