Problem p02371 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Diameter {
public:
  int nv;
  vector<vector<pair<int, int>>> adj;
  Diameter() {}
  Diameter(int nv) : nv(nv) { adj.resize(nv); }
  void setAdj(int u, int v, int w);
  void longestPath(int &s, int &t, int &l);
  void dfs(vector<bool> &visited, vector<int> &d, int s);
};

void Diameter::setAdj(int u, int v, int w) {
  adj[u].push_back(make_pair(v, w));
  adj[v].push_back(make_pair(u, w));
  return;
}

void Diameter::dfs(vector<bool> &visited, vector<int> &d, int s) {
  for (auto it = adj[s].begin(); it != adj[s].end(); ++it) {
    int t = (*it).first;
    int w = (*it).second;
    if (!visited[t]) {
      d[t] = d[s] + w;
      visited[t] = true;
      dfs(visited, d, t);
    }
  }
}

void Diameter::longestPath(int &s, int &t, int &l) {
  const int infty = -2147483648;
  vector<bool> visited(nv, false);
  vector<int> d(nv, infty);

  d[s] = 0;
  visited[s] = true;

  dfs(visited, d, s);

  l = infty;

  for (auto i = 0; i < nv; ++i) {
    if (d[i] > l) {
      l = d[i];
      t = i;
    }
  }
}

int main() {
  int n;
  cin >> n;

  Diameter d(n);

  for (auto i = 0; i < n - 1; ++i) {
    int u, v, w;
    cin >> u >> v >> w;

    d.setAdj(u, v, w);
  }

  int x, y, l;

  x = 0;

  d.longestPath(x, y, l);
  d.longestPath(y, x, l);

  cout << l << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Diameter {
public:
  int nv;
  vector<vector<pair<int, int>>> adj;
  Diameter() {}
  Diameter(int nv) : nv(nv) { adj.resize(nv); }
  void setAdj(int u, int v, int w);
  void longestPath(int &s, int &t, int &l);
};

void Diameter::setAdj(int u, int v, int w) {
  adj[u].push_back(make_pair(v, w));
  adj[v].push_back(make_pair(u, w));
  return;
}

void Diameter::longestPath(int &s, int &t, int &l) {
  const int infty = -2147483647;
  vector<bool> visited(nv, false);
  vector<int> d(nv, infty);

  d[s] = 0;

  while (true) {
    int maxd = infty;
    int u;

    for (auto i = 0; i < nv; ++i) {
      if (!visited[i] && d[i] > maxd) {
        u = i;
        maxd = d[i];
      }
    }

    if (maxd == infty)
      break;

    visited[u] = true;

    for (auto it = adj[u].begin(); it != adj[u].end(); ++it) {
      int v = (*it).first;
      int w = (*it).second;
      if (!visited[v])
        if (d[u] + w > d[v])
          d[v] = d[u] + w;
    }
  }

  l = infty;

  for (auto i = 0; i < nv; ++i) {
    if (d[i] > l) {
      l = d[i];
      t = i;
    }
  }

  return;
}

int main() {
  int n;
  cin >> n;

  Diameter d(n);

  for (auto i = 0; i < n - 1; ++i) {
    int u, v, w;
    cin >> u >> v >> w;

    d.setAdj(u, v, w);
  }

  int x, y, l;

  x = 0;

  d.longestPath(x, y, l);
  d.longestPath(y, x, l);

  cout << l << endl;

  return 0;
}