#include "dreaming.h"
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<pair<ll,ll> >adjlist[100000];
bool visited[100000];
pair<ll,ll> maxi={0,LLONG_MAX};
void dfs(ll node,ll dist_so_far){
if(visited[node]==false){
visited[node]=true;
if(maxi.first<dist_so_far || (maxi.first==dist_so_far && maxi.second>node)){
maxi={dist_so_far,node};
}
for(ll i=0;i<adjlist[node].size();i++){
if(visited[adjlist[node][i].first]==false){
dfs(adjlist[node][i].first,adjlist[node][i].second+dist_so_far);
}
}
}
}
vector<ll>yes;
stack<ll>hmph;
void dfs2(ll node,ll dist_so_far){
if(visited[node]==false){
visited[node]=true;
hmph.push(node);
if(maxi.first==dist_so_far && maxi.second==node){
while(hmph.size()){
yes.push_back(hmph.top());
hmph.pop();
}
reverse(yes.begin(),yes.end());
}
for(ll i=0;i<adjlist[node].size();i++){
if(visited[adjlist[node][i].first]==false){
dfs(adjlist[node][i].first,adjlist[node][i].second+dist_so_far);
}
}
hmph.pop();
}
}
ll travelTime(ll N,ll M,ll L,ll A[],ll B[],ll T[]){
map<pair<ll,ll>,ll>mp;
for(ll i=0;i<M;i++){
adjlist[A[i]].push_back({B[i],T[i]});
adjlist[B[i]].push_back({A[i],T[i]});
mp[{A[i],B[i]}]=T[i];
mp[{B[i],A[i]}]=T[i];
}
for(ll i=0;i<N;i++) visited[i]=false;
vector<ll>starting;
for(ll i=0;i<N;i++){
if(visited[i]==false){
dfs(i,0);
starting.push_back(maxi.second);
maxi={0,LLONG_MAX};
}
}
for(ll i=0;i<N;i++){
visited[i]=false;
}
vector<pair<ll,ll> > diameters;
vector<vector<ll> > paths;
vector<pair<ll,ll> > maxrecord;
for(ll i=0;i<starting.size();i++){
dfs(starting[i],0);
diameters.push_back({starting[i],maxi.second});
maxrecord.push_back(maxi);
maxi={0,LLONG_MAX};
}
for(ll i=0;i<N;i++){
visited[i]=false;
}
for(ll i=0;i<starting.size();i++){
maxi=maxrecord[i];
dfs2(starting[i],0);
paths.push_back(yes);
yes.clear();
}
ll ans=0;
vector<ll>segments;
for(ll i=0;i<paths.size();i++){
ans=max(ans,maxrecord[i].first);
ll good=LLONG_MAX;
ll sofar=0;
for(ll j=0;j<paths[i].size();j++){
if(max(maxrecord[i].first-sofar,sofar)<good){
good=max(maxrecord[i].first-sofar,sofar);
sofar+=mp[{paths[i][j],paths[i][j+1]}];
}
else break;
}
segments.push_back(good);
}
ll d=segments.size();
sort(segments.begin(),segments.end());
ans=max(ans,segments[d-1]+segments[d-2]+L);
ans=max(ans,segments[d-2]+segments[d-3]+2*L);
return ans;
}
Compilation message
dreaming.cpp: In function 'void dfs(long long int, long long int)':
dreaming.cpp:14:21: 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]
14 | for(ll i=0;i<adjlist[node].size();i++){
| ~^~~~~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfs2(long long int, long long int)':
dreaming.cpp:34:21: 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]
34 | for(ll i=0;i<adjlist[node].size();i++){
| ~^~~~~~~~~~~~~~~~~~~~~
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:65:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(ll i=0;i<starting.size();i++){
| ~^~~~~~~~~~~~~~~~
dreaming.cpp:74:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(ll i=0;i<starting.size();i++){
| ~^~~~~~~~~~~~~~~~
dreaming.cpp:82:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(ll i=0;i<paths.size();i++){
| ~^~~~~~~~~~~~~
dreaming.cpp:86:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(ll j=0;j<paths[i].size();j++){
| ~^~~~~~~~~~~~~~~~
/tmp/ccGE1uzD.o: In function `main':
grader.c:(.text.startup+0xa7): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status