#include "bits/stdc++.h"
#include "dreaming.h"
using namespace std;
#define endl "\n"
#define ll long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const long long MOD = 1e9+7;
const long long INF = 1e18;
int nx[8] = {0, 0, -1, 1, 1, -1, 1, -1};
int ny[8] = {1, -1, 0, 0, -1, 1, 1, -1};
void dfs(vector<pair<ll, ll>> adj[], int u, vector<bool> &visited, vector<ll> &curr_comp)
{
if(visited[u])
return;
visited[u] = true;
curr_comp.pb(u);
for(auto v: adj[u])
{
dfs(adj, v.fi, visited, curr_comp);
}
}
void dfs_dist(vector<pair<ll, ll>> adj[], ll u, ll p, vector<int> &dist)
{
for(auto v: adj[u])
{
if(v.fi!=p)
{
dist[v.fi] = dist[u] + v.se;
dfs_dist(adj, v.fi, u, dist);
}
}
}
int calculate_diameter(vector<pair<ll, ll>> adj[], int N)
{
int a = 1;
vector<int> dist_a(N+1, 0);
dfs_dist(adj, a, -1, dist_a);
int b = -1;
int maximum_a = 0;
for(int i = 1; i<=N; i++)
{
if(dist_a[i]>maximum_a)
{
maximum_a = dist_a[i];
b = i;
}
}
vector<int> dist_b(N+1, 0);
dfs_dist(adj, b, -1, dist_b);
int c = -1;
int maximum_b = 0;
for(int i = 1; i<=N; i++)
{
if(dist_b[i]>maximum_b)
{
maximum_b = dist_b[i];
c = i;
}
}
return dist_b[c];
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
//creating the graph
vector<pair<ll, ll>> adj[N+1];
for(int i = 0; i<M; i++)
{
adj[A[i]+1].pb(mp(B[i]+1, T[i]));
adj[B[i]+1].pb(mp(A[i]+1, T[i]));
}
//extracting the 2 components
vector<vector<ll>> comps;
vector<bool> visited(N+1, false);
for(int u = 1; u<=N; u++)
{
if(visited[u])
continue;
vector<ll> curr_comp;
dfs(adj, u, visited, curr_comp);
comps.pb(curr_comp);
}
int res = 1e9;
for(int i = 0; i<int(comps[0].size()); i++)
{
for(int j = 0; j<int(comps[1].size()); j++)
{
//adding the edge
vector<pair<ll, ll>> tempadj[N+1];
for(int i = 1; i<=N; i++)
{
tempadj[i] = adj[i];
}
tempadj[comps[0][i]].pb(mp(comps[1][j], L));
tempadj[comps[1][j]].pb(mp(comps[0][i], L));
//Calculating tree diameter
//int diameter = calculate_diameter(tempadj, N);
cout << diameter << endl << flush;
res = min(res, diameter);
}
}
return res;
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:121:21: error: 'diameter' was not declared in this scope
121 | cout << diameter << endl << flush;
| ^~~~~~~~