Problem p01884 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define pb push_back
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define mp make_pair
#define xx first
#define yy second
#define all(v) v.begin(), v.end()

#define lt 2 * n
#define rt 2 * n + 1
#define clft 2 * n, st, mid
#define crgt 2 * n + 1, mid + 1, ed

#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a) memset(a, -1, sizeof(a))

#define eps 1e-9
#define PI acos(-1.0)
#define MAX 100010
#define INF 2000000000

/******************************************************************************************/

#define mod1 1000000007
#define mod2 1000000123
#define base 100001

vector<ll> ed[MAX];
ll has1[MAX], has2[MAX];
map<PLL, ll> M;

void dfs(ll u, ll p) {
  ll i, v, h1 = 0, h2 = 0;
  for (i = 0; i < ed[u].size(); i++) {
    v = ed[u][i];
    if (v == p)
      continue;
    dfs(v, u);
    h1 += has1[v];
    h2 += has2[v];
    h1 %= mod1;
    h2 %= mod2;
  }
  has1[u] = (h1 * base + 1) % mod1;
  has2[u] = (h2 * base + 1) % mod2;
  M[mp(h1, h2)]++;
}

int main() {
  // ios_base::sync_with_stdio(false);
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  ll n, i, u, v;
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> u >> v;
    ed[u].pb(v);
    ed[v].pb(u);
  }
  dfs(1, 0);

  ll ans = 0;
  map<PLL, ll>::iterator it = M.begin();
  while (it != M.end()) {
    ans += (it->second * (it->second - 1)) / 2;
    it++;
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mx 100002
vector<int> graph[mx + 5];
int node[mx + 5], mxdepth[mx + 5];
map<pair<int, int>, int> depth;

int dfs1(int u) {
  if (graph[u].size() == 0) {
    depth[make_pair(u, 0)] = 1;
    return 0;
  }

  int ret = 0;
  for (int i = 0; i < graph[u].size(); i++) {
    int v = graph[u][i];
    ret = max(ret, 1 + dfs1(v));
  }

  depth[make_pair(u, 0)] = 1;
  for (int i = 0; i < graph[u].size(); i++) {
    int v = graph[u][i];
    for (int k = 0; k < mxdepth[v]; k++) {
      pair<int, int> p = make_pair(v, k);
      depth[make_pair(u, k + 1)] += depth[p];
    }
  }

  return mxdepth[u] = ret;
}
int dfs2(int u) {
  if (graph[u].size() == 0) {
    node[u] = 0;
    return 0;
  }

  int ret = 0;
  for (int i = 0; i < graph[u].size(); i++) {
    int v = graph[u][i];
    int n1 = 1 + dfs2(v);
    ret += n1;
  }

  return node[u] = ret;
}
int main() {
  int n;

  while (scanf("%d", &n) == 1) {
    memset(node, 0, sizeof(node));
    for (int i = 0; i <= n; i++)
      graph[i].clear();
    depth.clear();

    for (int i = 1; i <= n - 1; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      graph[u].push_back(v);
    }

    dfs1(1);
    dfs2(1);

    for (int i = 1; i <= n; i++)
      node[i]++;

    // map<pair<int, int>, int>m;
    map<pair<int, map<pair<int, int>, int>>, int> m;

    for (int i = 1; i <= n; i++) {
      m[make_pair(node[i], depth)]++;
    }

    map<pair<int, map<pair<int, int>, int>>, int>::iterator it;
    long long ans = 0;
    for (it = m.begin(); it != m.end(); it++) {
      long long var = it->second;
      ans += ((var) * (var - 1)) / 2;
    }

    printf("%lld\\n", ans);
  }

  return 0;
}