#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int const N=1e5+10;
vector<pair<int,int>>nei[N]={};
vector<int>heads;
int vis[N]={};
int cost[N]={};
void dfs(int n)
{
vis[n]=1;
for (auto [i,w]:nei[n])
{
if (!vis[i])
{
dfs(i);
cost[n]=max(cost[i]+w,cost[n]);
}
}
}
bool w1=0;
int dist,root;
void getroot(int n,int pa,int ma)
{
int mx1=0,mx2=0;
int ind=n;
for (auto [i,w]:nei[n])
{
if(i!=pa)
{
int z=cost[i]+w;
if (z>mx1)
{
mx2=mx1;
mx1=z;
ind=i;
}
else if (z>mx2)
mx2=z;
}
}
if (max(mx1,ma)<dist)
{
dist=max(mx1,ma);
root=n;
}
for (auto [i,w]:nei[n])
{
if (i!=pa)
{
if (ind==i)
getroot(i,n,max(ma,mx2)+w);
else
getroot(i,n,max(ma,mx1)+w);
}
}
}
void bfs(int n)
{
queue<pair<int,int>>Q;
Q.push({n,-1});
while (Q.size())
{
int n,m;
tie(n,m)=Q.front();
Q.pop();
for (auto [i,w]:nei[n])
{
if (i!=m)
{
cost[i]=cost[n]+w;
Q.push({i,n});
}
}
}
}
int travelTime(int n, int m, int l, int a[], int b[], int t[])
{
for (int i=0;i<m;i++)
{
nei[a[i]].push_back({b[i],t[i]});
nei[b[i]].push_back({a[i],t[i]});
}
for (int i=0;i<n;i++)
{
if (!vis[i])
{
heads.push_back(i);
dfs(i);
}
}
for (int i=0;i<n;i++)
vis[i]={};
vector<pair<int,int>>new_head;
for (auto i:heads)
{
dist=1e9+10;
root=i;
getroot(i,-1,0);
new_head.push_back({dist,root});
}
sort(begin(new_head),end(new_head));
reverse(begin(new_head),end(new_head));
for (int i=1;i<new_head.size();i++)
{
nei[new_head[0].second].push_back({new_head[i].second,l});
nei[new_head[i].second].push_back({new_head[0].second,l});
}
for (int i=0;i<n;i++)
cost[i]=0;
bfs(new_head[0].second);
w1=0;
new_head={};
for (int i=0;i<n;i++)
{
new_head.push_back({cost[i],i});
cost[i]=0;
}
sort(begin(new_head),end(new_head));
reverse(begin(new_head),end(new_head));
bfs(new_head[0].second);
new_head={};
for (int i=0;i<n;i++)
{
new_head.push_back({cost[i],i});
cost[i]=0;
}
sort(begin(new_head),end(new_head));
reverse(begin(new_head),end(new_head));
return new_head[0].first;
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:105:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (int i=1;i<new_head.size();i++)
| ~^~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/cc81u0HV.o: in function `main':
grader.c:(.text.startup+0xd1): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status