Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.2 kB
1
Indexable
#include <bits/stdc++.h>
using namespace std;

int getMaxTime(int g_nodes, int g_from[], int g_to[]) {
  // Create a vector to store the parent of each node.
  vector<int> parent(g_nodes + 1);
  for (int i = 1; i <= g_nodes; i++) {
    parent[i] = i;
  }

  // Create a vector to store the distance of each node from the root.
  vector<int> dist(g_nodes + 1, 0);

  // Do a DFS traversal of the tree and update the parent and distance of each node.
  for (int i = 0; i < g_nodes - 1; i++) {
    int u = g_from[i];
    int v = g_to[i];

    parent[v] = u;
    dist[v] = dist[u] + 1;
  }

  // Initialize the maximum time to transfer data as 0.
  int maxTime = 0;

  // For each pair of nodes, find the maximum time taken to transfer data between them.
  for (int i = 1; i <= g_nodes; i++) {
    for (int j = i + 1; j <= g_nodes; j++) {
      // The maximum time taken to transfer data between i and j is the distance between i and j plus 1.
      int time = dist[i] + dist[j] + 1;

      // Update the maximum time if time is greater than the current maximum time.
      maxTime = max(maxTime, time);
    }
  }

  return maxTime;
}