alexjercan/bugnet huggingface dataset:
Problem p01883 (C++) — buggy submission, verdict: Runtime Error

//??:2s   空?:512MB
//?意:要求求出最短的字典序最小的括号序列
// 要求移?n次可以使他合法
// 算法:通?枚???,...)))(((....,??是n*(n+1)/2的最?解
// 所以?个之?就可以将最左?的(向左移?相?的位置
// 可以??理出所有的n*(n+1)/2
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
ll f[50005];
int chuli(int n) {
  for (int i = 1; i <= 50000; i++) {
    f[i] = i * (i + 1) / 2;
    if (f[i] >= n)
      return i;
  }
}
void print(int x) {
  for (int i = 1; i <= x; i++)
    printf(")");
  for (int i = 1; i <= x; i++)
    printf("(");
  printf("\\n");
}
int main() {
  //	freopen("a.in","r",stdin);
  int n;
  scanf("%d", &n);
  int ans = chuli(n);
  if (f[ans] == n)
    print(ans);
  else {
    int delt = f[ans] - n;
    for (int i = 1; i <= ans - f[ans] + n; i++)
      printf(")");
    printf("(");
    for (int i = ans - f[ans] + n + 2; i <= ans + 1; i++)
      printf(")");
    for (int i = 1; i <= ans - 1; i++)
      printf("(");
    printf("\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>

using namespace std;
string str[5];
char s[1000005];
int main() {
  str[1] = ")(";
  str[2] = ")()(";
  str[3] = "))((";
  str[4] = ")())((";

  int a;
  cin >> a;
  if (a <= 4)
    cout << str[a] << endl;
  else {
    int num = 0;
    if (a & 1) // 奇数
    {
      s[num++] = ')';
      s[num++] = ')';
      for (int i = 0; i < (a - 2) / 2; i++) {
        s[num++] = '(';
        s[num++] = ')';
      }
      //    s += "()";
      s[num++] = '(';
      s[num++] = '(';
      // s += "((";
    } else {
      s[num++] = ')';
      s[num++] = '(';
      s[num++] = ')';
      s[num++] = ')';
      // s += ")())";
      for (int i = 0; i < (a - 3) / 2; i++) {
        s[num++] = '(';
        s[num++] = ')';
      }
      //    s += "()";
      s[num++] = '(';
      s[num++] = '(';
      // s += "((";
    }
    s[num++] = '\\0';
    cout << s << endl;
  }

  return 0;
}
Problem p01883 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define debug(x) cout << #x << "=" << x << endl
using namespace std;

int len, a;

int main() {
  cin >> a;
  while (len * (len + 1) / 2 < a)
    len++;
  // debug(len);
  int tmp = len * (len + 1) / 2 - a;
  for (int i = 1; i <= len - tmp; i++)
    putchar(')');
  putchar('(');
  for (int i = 1; i <= tmp; i++)
    putchar(')');
  for (int i = 1; i <= len - 1; i++)
    putchar('(');
  puts("");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
void work(long long n) {
  n = (n + 1) >> 1;
  for (long long i = 1; i <= n; ++i)
    printf(")");
  for (long long i = 1; i <= n; ++i)
    printf("(");
  printf("\\n");
}
int main() {
  long long n;
  scanf("%lld", &n);
  if (!(n & 1)) {
    printf(")(");
    n--;
  }

  work(n);
  return 0;
}
Problem p01883 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define maxx 2000000000
using namespace std;
int a, tot, k;
int main() {
  scanf("%d", &a);
  while (k * (k + 1) / 2 <= a)
    k++;
  if (k * (k + 1) / 2 > a)
    k--;
  for (int i = 1; i <= k; i++) {
    if ((a - k * (k + 1) / 2) == i) {
      printf(")(");
    }
    printf(")");
  }
  for (int i = 1; i <= k; i++)
    printf("(");
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int N, s, i;
char c[30000];
int main() {
  scanf("%d", &N);
  for (i = 1; i <= N; i++) {
    s += i;
    if (s >= N)
      break;
  }
  int len = i * 2;
  for (int i = 1; i <= len / 2; i++)
    c[i] = ')', c[len / 2 + i] = '(';
  if (s == N) {
    for (int i = 1; i <= len; i++)
      printf("%c", c[i]);
    printf("\\n");
    return 0;
  }
  int p = s - N;
  for (int i = len / 2; i >= 2; i--) {
    swap(c[i], c[i + 1]);
    p--;
    if (p == 0) {
      for (int i = 1; i <= len; i++)
        printf("%c", c[i]);
      printf("\\n");
      return 0;
    }
  }
  for (int i = len / 2 + 1; i <= len - 1; i++) {
    swap(c[i], c[i + 1]);
    p--;
    if (p == 0) {
      for (int i = 1; i <= len; i++)
        printf("%c", c[i]);
      printf("\\n");
      return 0;
    }
  }
}
Problem p01883 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int A, ans = 0;
int main() {
  scanf("%d", &A);
  ans = sqrt(A * 2);
  if (A - (1 + ans) * ans / 2 > 0) {
    ans++;
  }
  A = (1 + ans) * ans / 2 - A;
  for (int i = 1; i <= ans - A; i++) {
    printf(")");
  }
  printf("(");
  for (int i = 1; i <= A; i++) {
    printf(")");
  }
  for (int i = 1; i < ans; i++) {
    printf("(");
  }
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int N, s, i;
char c[30000];
int main() {
  scanf("%d", &N);
  for (i = 1; i <= N; i++) {
    s += i;
    if (s >= N)
      break;
  }
  int len = i * 2;
  for (int i = 1; i <= len / 2; i++)
    c[i] = ')', c[len / 2 + i] = '(';
  if (s == N) {
    for (int i = 1; i <= len; i++)
      printf("%c", c[i]);
    printf("\\n");
    return 0;
  }
  int p = s - N;
  for (int i = len / 2; i >= 2; i--) {
    swap(c[i], c[i + 1]);
    p--;
    if (p == 0) {
      for (int i = 1; i <= len; i++)
        printf("%c", c[i]);
      printf("\\n");
      return 0;
    }
  }
  for (int i = len / 2 + 1; i <= len - 1; i++) {
    swap(c[i], c[i + 1]);
    p--;
    if (p == 0) {
      for (int i = 1; i <= len; i++)
        printf("%c", c[i]);
      printf("\\n");
      return 0;
    }
  }
}
Problem p01883 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  int a;
  cin >> a;
  int n = 0;
  int i = 0;
  while (n * (n + 1) / 2 < a) {
    n++;
  }
  int cnt = n * (n + 1) / 2 - a;
  for (i = 0; i < n - cnt; i++)
    cout << ")"; // yuan
  cout << "(";   // duiqijinxingxiugai
  for (i = n - cnt + 1; i <= n; i++)
    cout << ")";
  for (int i = n + 1; i < n * 2; i++)
    cout << "("; /// yuan
  cout << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <string>
#define N 10000000
using namespace std;
int main() {
  long long n, a[N] = {0};
  scanf("%lld", &n);
  while (n) {
    long long sum = 0, i = 0;
    while (sum <= n) {
      i++;
      sum = i * (i + 1) / 2;
    }
    i--;
    a[++a[0]] = i;
    n -= i * (i + 1) / 2;
  }
  int i;
  for (i = a[0]; i >= 1; i--) {
    for (int j = 1; j <= a[i]; j++)
      printf(")");
    for (int j = 1; j <= a[i]; j++)
      printf("(");
  }
  printf("\\n");
  return 0;
}
Problem p01883 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int A;
int main() {
  int n = 0, k;
  scanf("%d", &A);
  while (true) {
    if (n * (n + 1) / 2 >= A)
      break;
    n++;
  }
  k = n * (n + 1) / 2 - A;
  for (int i = 0; i < n - k; i++)
    printf(")");
  printf("(");
  for (int i = 0; i < k; i++)
    printf(")");
  for (int i = 0; i < n - 1; i++)
    printf("(");
  printf("\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = n % 3; i >= 1; --i)
      printf(")(");

    if (n >= 3) {
      for (int i = n / 3; i >= 0; --i)
        printf(")");
      for (int i = n / 3; i >= 0; --i)
        printf("(");
    }

    printf("\\n");
  }
  return 0;
}
Problem p01883 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  long long a, temp;
  scanf("%I64d", &a);
  int num = 1;
  while (1) {
    temp = (num + 1) * num / 2;
    if (temp >= a)
      break;
    num++;
  }
  if (temp == a) {
    for (int i = 1; i <= num; i++)
      printf(")");
    for (int i = 1; i <= num; i++)
      printf("(");
    printf("\\n");
  } else {
    long long temp2 = temp - a - 1;
    for (int i = 1; i <= num; i++) {
      if (i == num - temp2)
        printf("(");
      printf(")");
    }
    for (int i = 1; i < num; i++) {
      printf("(");
    }
    printf("\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
  int A;
  while (~scanf("%d", &A)) {
    if (A == 1)
      printf(")(\\n");
    else if (A == 2)
      printf(")()(\\n");
    else {
      if ((A - 3) % 2 == 0) {
        for (int i = 1; i <= (A - 3) / 2; i++)
          printf(")()(");
      } else {
        for (int i = 1; i <= (A - 3) / 2; i++)
          printf(")()(");
        printf(")(");
      }
      printf("))((\\n");
    }
  }
}
Problem p01884 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define read() freopen("input.txt", "r", stdin)

#define ll64 long long
#define BASE 100000007
#define MOD 100000077

ll64 BP[300010];
bool visited[100010];
vector<int> graph[100010];
map<ll64, ll64> C_M;

ll64 dfs(int u) {
  // printf("=>%d\\n", u);
  visited[u] = true;
  ll64 hsh = 1;
  for (auto v : graph[u]) {
    if (!visited[v]) {
      hsh += (dfs(v) * BASE) % MOD;
      hsh %= MOD;
    }
  }
  C_M[hsh] += 1;
  return hsh;
}

int main() {
  // read();
  ll64 temp = 1;
  for (int i = 0; i < 200000; i++) {
    BP[i] = temp;
    temp = (temp * BASE) % MOD;
  }
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  dfs(1);
  ll64 ans = 0;
  for (auto &it : C_M) {
    ll64 x = it.second;
    ans += (x * (x - 1)) / 2;
  }
  printf("%lld\\n", ans);
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define read() freopen("input.txt", "r", stdin)

#define ll64 long long
#define BASE 100000007
#define MOD 100000037

ll64 BP[300010];
bool visited[100010];
vector<int> graph[100010];
map<ll64, ll64> C_M;
map<map<int, int>, int> M_M;

void dfs(int u, map<int, int> &PM) {
  map<int, int> M;
  visited[u] = true;
  for (auto v : graph[u]) {
    if (!visited[v]) {
      dfs(v, M);
    }
  }
  M[0] += 1;
  ll64 hsh = 0;
  for (auto &it : M) {
    PM[it.first + 1] += it.second;
    hsh += (it.first * BP[it.second]) % MOD;
    hsh %= MOD;
  }
  C_M[hsh] += 1;
  M_M[M] += 1;
}

int main() {
  // read();
  ll64 temp = 1;
  for (int i = 0; i < 200000; i++) {
    BP[i] = temp;
    temp = (temp * BASE) % MOD;
  }
  map<int, int> tempM;
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  dfs(1, tempM);
  ll64 ans = 0;
  for (auto &it : M_M) {
    ll64 x = it.second;
    ans += (x * (x - 1)) / 2;
  }
  printf("%lld\\n", ans);
}
Problem p01884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
typedef long long int LL;

vector<int> ve[N];
map<LL, int> ma;
LL a[N];
LL b = 10e2 + 7;
LL ans;
void dfs(int u) {
  a[u] = 1;
  for (int i = 0; i < ve[u].size(); i++) {
    int v = ve[u][i];
    dfs(v);
    a[u] += a[v] * b;
  }
  ans += ma[a[u]];
  ma[a[u]]++;
  return;
}

int main() {
  int n;
  cin >> n;
  int u, v;
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v;
    ve[u].push_back(v);
  }
  ans = 0;
  dfs(1);
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

const int MAX_N = 1e5 + 5;

int N;
list<int> G[MAX_N];

inline void maxi(int &x, int y) { x = max(x, y); }

const int HCT = 1;

const ULL hcb[4] = {103, 59627, 65203, 73709};
ULL hcbpow[4][MAX_N];

struct Node {
  int h;
  ULL hc[HCT];
  Node() { memset(hc, 0, sizeof hc); }
} node[MAX_N];

inline int cmp(const Node &x, const Node &y) {
  if (x.h != y.h) {
    if (x.h < y.h)
      return -1;
    else
      return 1;
  }
  for (int i = 0; i < HCT; ++i) {
    if (x.hc[i] != y.hc[i]) {
      if (x.hc[i] < y.hc[i])
        return -1;
      else
        return 1;
    }
  }
  return 0;
}

inline bool comp(const Node &x, const Node &y) { return cmp(x, y) < 1; }

inline void pre() {
  for (int i = 0; i < HCT; ++i)
    hcbpow[i][0] = 1;
  for (int i = 1; i < MAX_N; ++i) {
    for (int j = 0; j < HCT; ++j) {
      hcbpow[j][i] = hcbpow[j][i - 1] * hcb[j];
    }
  }
}

void hdfs(int u, int father) {
  for (auto &v : G[u]) {
    if (v != father) {
      hdfs(v, u);
      maxi(node[u].h, node[v].h + 1);
    }
  }
  maxi(node[u].h, 1);
}

void hcdfs(int u, int father) {
  for (auto &v : G[u]) {
    if (v != father) {
      hcdfs(v, u);
      int dh = node[u].h - 1 - node[v].h;
      for (int i = 0; i < HCT; ++i) {
        node[u].hc[i] += node[v].hc[i] * hcbpow[i][dh];
      }
    }
  }
  for (int i = 0; i < HCT; ++i) {
    node[u].hc[i] += hcbpow[i][node[u].h];
  }
}

LL solve() {
  hdfs(1, 0);
  hcdfs(1, 0);
  sort(node + 1, node + 1 + N, comp);
  LL ret = 0, cnt = 1;
  for (int i = 1; i <= N + 1; ++i) {
    if (cmp(node[i], node[i - 1]) == 0)
      ++cnt;
    else {
      ret += (cnt - 1) * cnt / 2;
      cnt = 1;
    }
  }
  return ret;
}

int main() {
  pre();
  scanf("%d", &N);
  for (int i = 1; i < N; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  LL ans = solve();
  printf("%lld\\n", ans);
  return 0;
}
Problem p01884 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
const LL maxn = 1e6 + 10, p = 9901, mod = 1e9 + 7;
vector<LL> G[maxn];
LL hash_v[maxn];
map<LL, LL> mp;
map<LL, LL>::iterator it;

void dfs(LL u) {
  hash_v[u] = 1;
  for (LL i = 0; i < G[u].size(); i++) {
    LL v = G[u][i];
    dfs(v);
    hash_v[u] = (hash_v[u] + hash_v[v] * p) % mod;
  }
  mp[hash_v[u]]++;
}
int main() {
  /// freopen("in.txt","r",stdin);
  LL n;
  LL u, v;
  while (scanf("%lld", &n) != -1) {
    for (LL i = 0; i <= n; i++)
      G[i].clear();
    mp.clear();
    for (LL i = 1; i < n; i++) {
      scanf("%lld%lld", &u, &v);
      G[u].push_back(v);
    }
    dfs(1);
    LL ans = 0;
    for (it = mp.begin(); it != mp.end(); it++)
      ans += it->second * (it->second - 1) / 2;
    printf("%lld\\n", ans);
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>
const int maxn = 100050;
using namespace std;
vector<int> son[maxn];
struct D {
  vector<int> d;
  int de;
  D() {
    d.clear();
    de = 0;
  }
  bool operator<(const D &b) const {
    int len1 = d.size();
    int len2 = b.d.size();
    if (len1 != len2)
      return len1 < len2; // 深度
    else {
      for (int i = 0; i < len1; ++i) {
        if (d[i] == b.d[i])
          continue;
        return d[i] < b.d[i];
      }
      return false;
    }
  }
} dep[maxn];
int yz = 0;
map<D, int> T;
void dfs(int cur) {
  if (son[cur].empty()) {
    yz++;
    return;
  }
  int i, j;
  int len = son[cur].size();
  dep[cur].d.push_back(len); // 把儿子这一层加入
  dep[cur].de = 1;
  for (i = 0; i < len; ++i) {
    int s = son[cur][i];
    dfs(s); // 操作儿子

    if (dep[cur].de <= dep[s].de + 1) {
      for (j = 1; j < dep[cur].de; ++j)
        dep[cur].d[j] += dep[s].d[j - 1]; // 跟新原有
      for (j; j < dep[s].de + 1; ++j)
        dep[cur].d.push_back(dep[s].d[j - 1]); // 需要拓展
      dep[cur].de = dep[s].de + 1;             // 更新深度
    } else {
      for (j = 1; j <= dep[s].de; ++j) {
        dep[cur].d[j] += dep[s].d[j - 1];
      }
    }
    dep[s].d.clear();
  } // 当前结点操作完毕
  if (T.find(dep[cur]) == T.end()) {
    T[dep[cur]] = 1;
  } else
    ++T[dep[cur]];
  return;
}
long long C[100050];
int main() {
  int n, i, a, b;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    son[i].clear();
  C[1] = 0;
  for (i = 2; i < 100050; ++i)
    C[i] = C[i - 1] + i - 1;
  for (i = 1; i < n; ++i) {
    scanf("%d%d", &a, &b);
    son[a].push_back(b);
  }
  T.clear();
  dfs(1);
  map<D, int>::iterator it;
  long long ans = 0;
  ans += C[yz];
  for (it = T.begin(); it != T.end(); ++it) {
    ans += C[it->second];
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p01884 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 1000010, p = 100001, MOD = 1e9 + 7;
vector<ll> G[N];
ll Hash[N];
map<ll, ll> m;
map<ll, ll>::iterator it;
void dfs(ll u) {
  Hash[u] = 1;
  for (int i = 0; i < G[u].size(); i++) {
    ll v = G[u][i];
    dfs(v);
    Hash[u] = (Hash[u] + Hash[v] * p) %
              MOD; // 哈希方程，p取一个6到8位的素数，mod一般取1e9+7
  }
  m[Hash[u]]++;
}
int main() { /*
            int size = 256 << 20; // 256MB
            char *p = (char*)malloc(size) + size;
            __asm__("movl %0, %%esp\\n" :: "r"(p));*/

  int n, u, v;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
  }
  dfs(1);
  ll ans = 0;
  for (it = m.begin(); it != m.end(); it++)
    ans += it->second * (it->second - 1) / 2;
  printf("%lld\\n", ans);
  return 0;
}
/*
https://blog.csdn.net/huatian5/article/details/70305845
*/


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
const int x1 = 1e5 + 3;
const int x2 = 1e5 + 19;
int dep[N], hd;
struct node {
  int v, nxt;
} p[N];
int head[N], son[N], tot;
ull a[N], p2[N], p3[N], b[N];
void add(int x, int y) {
  p[tot].v = y;
  p[tot].nxt = head[x];
  head[x] = tot++;
}
void init() {
  tot = 0;
  p3[0] = p2[0] = 1;
  memset(head, -1, sizeof(head));
  for (int i = 1; i < N; i++) {
    p2[i] = x1 * p2[i - 1];
    p3[i] = x2 * p3[i - 1];
  }
}
map<pair<ull, ull>, int> mp;
map<pair<ull, ull>, int>::iterator it;
void dfs(int x, int fa) {
  for (int i = head[x]; ~i; i = p[i].nxt) {
    int y = p[i].v;
    if (y == fa)
      continue;
    dfs(y, x);
    dep[x] = max(dep[y], dep[x]);
  }
  for (int i = head[x]; ~i; i = p[i].nxt) {
    int y = p[i].v;
    if (y == fa)
      continue;
    a[x] = a[x] + a[y] * p2[dep[x] - dep[y]];
    b[x] = b[x] + b[y] * p3[dep[x] - dep[y]];
  }
  dep[x]++;
  a[x] = a[x] + p2[dep[x]];
  b[x] = b[x] + p3[dep[x]];
  // cout<<x<<' '<<a[x]<<' '<<dep[x]<<endl;
  mp[make_pair(b[x], a[x])]++;
  hd++;
}
int main() {

  init();
  int n, x, y;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  dfs(1, 0);
  ll ans = 0;
  for (it = mp.begin(); it != mp.end(); it++) {
    int t = it->second;
    // cout<<t<<endl;
    ans += 1ll * t * (t - 1) / 2;
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p01884 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

#define int long long
const int M = 1000000007;

int r;
vector<int> g[100005];
map<int, int> m;

int dfs(int cur) {

  int res = 0;
  for (auto to : g[cur]) {
    res += dfs(to);
    res %= M;
  }
  res = res * r + 1;
  res %= M;
  m[res]++;
  return res;
}

signed main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
  }

  r = 8731873;
  dfs(0);

  int ans = 0;
  for (auto i : m) {
    // show(i.second)
    ans += (i.second * (i.second - 1)) / 2;
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

#define int long long
const int M = 1000000007;

int r;
vector<int> g[100005];
map<int, int> m;

int dfs(int cur, vector<bool> used) {
  used[cur] = true;

  int res = 0;
  for (auto to : g[cur]) {
    if (used[to])
      continue;
    res += dfs(to, used);
    res %= M;
  }
  res = res * r + 1;
  res %= M;
  m[res]++;
  return res;
}

signed main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  r = 8731873;
  dfs(0, vector<bool>(n, 0));

  int ans = 0;
  for (auto i : m) {
    // show(i.second)
    ans += (i.second * (i.second - 1)) / 2;
  }
  cout << ans << endl;
}
Problem p01884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, (n))
#define ALL(a) (a.begin(), a.end())
typedef vector<int> vi;

int64_t h(const vector<vi> &t, int i, int p, vi &v) {
  int64_t res = 1;
  for (int c : t[i]) {
    if (c == p)
      continue;
    res += 1000000009 * h(t, c, i, v);
  }
  v[i] = res;
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<vi> t(n);
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    t[a].push_back(b);
    t[b].push_back(a);
  }
  vi v(n);
  h(t, 0, -1, v);
  map<int64_t, int> m;
  int64_t cnt = 0;
  REP(i, n) {
    cnt += m[v[i]];
    ++m[v[i]];
  }
  cout << cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, (n))
#define ALL(a) (a.begin(), a.end())
typedef vector<int> vi;

int64_t h(const vector<vi> &t, int i, int p, vi &v) {
  int64_t res = 1;
  for (int c : t[i]) {
    if (c == p)
      continue;
    res += 1000000009 * h(t, c, i, v);
  }
  v[i] = res;
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<vi> t(n);
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    t[a].push_back(b);
    t[b].push_back(a);
  }
  vi v(n);
  h(t, 0, -1, v);
  multiset<int64_t> m;
  int64_t cnt = 0;
  REP(i, n) {
    cnt += m.count(v[i]);
    m.insert(v[i]);
  }
  cout << cnt << endl;
  return 0;
}