Language: C++

#include <iostream>
#include <string>
#include <vector>


#define rep(i, n) for (int i = 0; i < int(n); ++i)

using namespace std;
typedef vector<int> VI;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 300001;
ll var[N];

void init_hash(void) {
  rep(i, N) {
    var[i] = rand() % mod;
  }
}

ll get_hash(int v, const vector<VI> &edges, int par = -1, int depth = 0) {
  ll prod = 1;
  rep(i, edges[v].size()) {
    int w = edges[v][i];
    if (par == w) { continue; }
    ll s = get_hash(w, edges, v, depth + 1);
    prod = prod * ((var[depth] + s) % mod) % mod;
  }
  return prod;
}

void dfs(int v,  const vector<VI> &edges, VI &dist, int par = -1, int d = 0) {
  dist[v] = d;
  rep(i, edges[v].size()) {
    int w = edges[v][i];
    if (w == par) {
      continue;
    }
    dfs(w, edges, dist, v, d + 1);
  }
}

vector<int> get_centre(int v, const vector<VI> &edges, vector<bool> &vis) {
  int n = edges.size();
  VI dist(n, -1);
  dfs(v, edges, dist);
  int maxi1 = v;
  rep(i, n) {
    if (dist[i] >= 0) {
      vis[i] = true;
    }
    if (dist[maxi1] < dist[i]) {
      maxi1 = i;
    }
  }
  dfs(maxi1, edges, dist);
  int maxi2 = maxi1;
  rep(i, n) {
    if (dist[maxi2] < dist[i]) {
      maxi2 = i;
    }
  }
  // max1 --- max2 is a diameter
  int diam = dist[maxi2];
  if (diam % 2 == 0) {
    // The center is unique
    int cur = maxi2;
    while (dist[cur] > diam / 2) {
      rep(i, edges[cur].size()) {
	int w = edges[cur][i];
	if (dist[w] == dist[cur] - 1) {
	  cur = w;
	  break;
	}
      }
    }
    return VI(1, cur);
  }
  // Two centers
  int cur = maxi2;
  int tmp = -1;
  while (dist[cur] > diam / 2) {
    rep(i, edges[cur].size()) {
      int w = edges[cur][i];
      if (dist[w] == dist[cur] - 1) {
	cur = w;
	if (dist[w] == diam / 2 + 1) {
	  tmp = w;
	}
	break;
      }
    }
  }
  VI ret(2);
  ret[0] = tmp;
  ret[1] = cur;
  return ret;
}

int main() {
  init_hash();
  int n1, m1;
  cin >> n1 >> m1;
  vector<VI> edges1(n1);
  rep(i, m1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    edges1[u].push_back(v);
    edges1[v].push_back(u);
  }
  int n2;
  cin >> n2;
  vector<VI> edges2(n2);
  rep(i, n2 - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    edges2[u].push_back(v);
    edges2[v].push_back(u);
  }
  int cnt = 0;
  vector<bool> dummy(n2);
  VI centre2 = get_centre(0, edges2, dummy);
  vector<ll> hash2;
  rep(i, centre2.size()) {
    int c = centre2[i];
    hash2.push_back(get_hash(c, edges2));
  }
  sort(hash2.begin(), hash2.end());
  vector<bool> vis(n1, false);
  rep(i, n1) {
    if (vis[i]) { continue; }
    VI centre1 = get_centre(i, edges1, vis);
    if (centre1.size() != centre2.size()) { continue; }
    vector<ll> hash1;
    rep(i, centre1.size()) {
      int c = centre1[i];
      hash1.push_back(get_hash(c, edges1));
    }
    sort(hash1.begin(), hash1.end());
    bool ok = false;
    rep(i, hash1.size()) {
      ok |= hash1[i] == hash2[i];
    }
    if (ok) {
      cnt += 1;
    }
  }
  cout << cnt << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:130:3: error: 'sort' was not declared in this scope; did you mean 'short'?
  130 |   sort(hash2.begin(), hash2.end());
      |   ^~~~
      |   short