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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> edges;
vector<map<int, int>> memo;
vector<pair<int, pair<int, int>>> memo2;

int solve(int curr, int prev) {
  if (memo[curr].find(prev) != memo[curr].end())
    return memo[curr][prev];

  if (memo[curr].empty()) {
    pair<int, int> ans(0, 0);
    for (int next : edges[curr]) {
      if (next != prev) {
        int d = solve(next, curr) + 1;
        if (ans.first < d)
          swap(ans.first, d);
        if (ans.second < d)
          swap(ans.second, d);
      }
    }
    memo[curr][prev] = ans.first;
    memo2[curr] = make_pair(prev, ans);
    return ans.first;
  } else {
    int prev2 = memo2[curr].first;
    pair<int, int> ans = memo2[curr].second;
    ;
    if (prev2 != -1) {
      int d = solve(prev2, curr) + 1;
      if (ans.first < d)
        swap(ans.first, d);
      if (ans.second < d)
        swap(ans.second, d);
    }

    if (prev == -1) {
      memo[curr][prev] = ans.first;
      return ans.first;
    } else {
      int d = solve(prev, curr) + 1;
      if (ans.first == d) {
        memo[curr][prev] = ans.second;
        return ans.second;
      } else {
        memo[curr][prev] = ans.first;
        return ans.first;
      }
    }
  }
}

int main() {
  int n;
  cin >> n;
  edges.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }

  memo.resize(n);
  memo2.resize(n);
  for (int i = 0; i < n; ++i) {
    int ans = 2 * (n - 1) - solve(i, -1);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> edges;
vector<map<int, pair<int, int>>> memo;

int solve(int curr, int prev) {
  if (memo[curr].find(prev) != memo[curr].end())
    return memo[curr][prev].first;

  if (memo[curr].empty()) {
    pair<int, int> ans(0, 0);
    for (int next : edges[curr]) {
      if (next != prev) {
        int d = solve(next, curr) + 1;
        if (ans.first < d)
          swap(ans.first, d);
        if (ans.second < d)
          swap(ans.second, d);
      }
    }
    memo[curr][prev] = ans;
    return ans.first;
  } else {
    int prev2 = memo[curr].begin()->first;
    pair<int, int> ans = memo[curr].begin()->second;
    if (prev2 != -1) {
      int d = solve(prev2, curr) + 1;
      if (ans.first < d)
        swap(ans.first, d);
      if (ans.second < d)
        swap(ans.second, d);
    }

    if (prev == -1) {
      return ans.first;
    } else {
      int d = solve(prev, curr) + 1;
      if (ans.first == d)
        return ans.second;
      else
        return ans.first;
    }
  }
}

int main() {
  int n;
  cin >> n;
  edges.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }

  memo.resize(n);
  for (int i = 0; i < n; ++i) {
    int ans = 2 * (n - 1) - solve(i, -1);
    cout << ans << endl;
  }

  return 0;
}