#include"dreaming.h"
//#include"grader.c"
#include<bits/stdc++.h>
#define fr first
#define sc second
using namespace std;
vector<pair<int,int>>g[100005];
bool vis[100005];
int mx[100005],mx2[100005],id[100005],mm,idd;
int dfs(int v,int p){
vis[v]=1;
int i=0,x;
id[v]=-1;
for(;i<g[v].size();i++)
if(g[v][i].fr!=p){
x=dfs(g[v][i].fr,v)+g[v][i].sc;
if(x>mx2[v]){
mx2[v]=x;
id[v]=g[v][i].fr;
}
}
return mx2[v];
}
void go(int v,int p,int val,int depth){
mx[v]=max(val,depth);
int m=0,i=0;
for(;i<g[v].size();i++)
if(g[v][i].fr!=p)
if(g[v][i].fr!=id[v]){
m=max(m,mx2[g[v][i].fr]+g[v][i].sc);
go(g[v][i].fr,v,max(mx2[v],val)+g[v][i].sc,depth+g[v][i].sc);
}
if(id[v]!=-1)go(id[v],v,max(m,val)+g[v][i].sc,depth+g[v][i].sc);
}
void calc(int v,int p){
if(max(mx2[v],mx[v])<mm){
mm=max(mx2[v],mx[v]);
idd=v;
}
for(auto to:g[v])if(to.fr!=p)calc(to.fr,v);
}
void diam(int v,int p,int depth){
if(depth>mm){
mm=depth;
idd=v;
}
for(auto to:g[v])if(to.fr!=p)diam(to.fr,v,depth+to.sc);
}
int travelTime(int n,int m,int l,int x[],int y[],int z[]){
for(int i=0;i<m;i++){
g[x[i]].push_back({y[i],z[i]});
g[y[i]].push_back({x[i],z[i]});
}
priority_queue<pair<int,int>>st;
g[n].push_back({n,0});
for(int i=0;i<n;i++)if(!vis[i]){
g[n][0].fr=i;
dfs(n,n);
go(n,n,0,0);
mm=2e9;
calc(n,n);
st.insert({mm,idd});
}
g[n].clear();
auto a=st.top();
st.pop();
while(!st.empty()){
auto b=st.top();
st.pop();
g[a.sc].push_back({b.sc,l});
g[b.sc].push_back({a.sc,l});
}
mm=0;
diam(0,0,0);
mm=0;
diam(idd,idd,0);
return mm;
}
Compilation message
dreaming.cpp: In function 'int dfs(int, int)':
dreaming.cpp:14:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(;i<g[v].size();i++)
| ~^~~~~~~~~~~~
dreaming.cpp: In function 'void go(int, int, int, int)':
dreaming.cpp:27:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(;i<g[v].size();i++)
| ~^~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:62:12: error: 'class std::priority_queue<std::pair<int, int> >' has no member named 'insert'
62 | st.insert({mm,idd});
| ^~~~~~