#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;
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
adj[comps[0][i]].pb(mp(comps[1][j], L));
adj[comps[1][j]].pb(mp(comps[0][i], L));
//Calculating tree diameter
int diameter = calculate_diameter(adj, N);
res = min(res, diameter);
}
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
11344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
11344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
6936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
11344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |