Language: C++

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <iostream>
#include <functional>
#include <iostream>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n[2],m[2];
vector<int> G[2][400001];
int dist[400001];
int dist2[400001];
bool used[400001];
bool calced[400001];
vector<int> root[2];
vector<int> vs;
map<string,int> mp;
vector<int> vp;

string make_str(int v){
	ostringstream os;
	os << v;
	return os.str();
}

vector<int> s[400001];

int calc(int t,int v){
	calced[v]=true;
	s[v].clear();
	for(int i=0;i<G[t][v].size();i++){
		int nv=G[t][v][i];
		if(!calced[nv]){
			int val=calc(t,nv);
			if(val==-1)return -1;
			s[v].push_back(val);
		}
	}
	if(s[v].size()>0)sort(s[v].begin(),s[v].end());
	string str="";
	for(int i=0;i<s[v].size();i++){
		if(i>0)str+=';';
		str+=make_str(s[v][i]);
	}
	if(str.size()==0){
		str+='0';
	}
	if(mp.find(str)!=mp.end()){
		return mp[str];
	}else{
		if(t==0)return -1;
		int siz=mp.size();
		mp[str]=siz+1;
		return siz+1;
	}
}

void bfs(int t,int s){
	queue<int> que;
	memset(dist,-1,sizeof(dist));
	vs.clear();
	que.push(s);
	dist[s]=0;
	while(que.size()){
		int v=que.front();
		que.pop();
		vs.push_back(v);
		for(int i=0;i<G[t][v].size();i++){
			if(dist[G[t][v][i]]==-1){
				dist[G[t][v][i]]=dist[v]+1;
				que.push(G[t][v][i]);
			}
		}
	}
}

void solve(){
	bfs(1,0);
	int maxi=-1;
	int tgt=0;
	for(int i=0;i<n[1];i++){
		if(dist[i]>maxi){
			maxi=dist[i];
			tgt=i;
		}
	}
	bfs(1,tgt);
	maxi=-1;
	for(int i=0;i<n[1];i++){
		if(dist[i]>maxi){
			maxi=dist[i];
			tgt=i;
		}
	}
	for(int i=0;i<n[1];i++){
		dist2[i]=dist[i];
	}
	bfs(1,tgt);
	for(int i=0;i<n[1];i++){
		if(maxi%2==0){
			if(dist[i]==maxi/2 && dist2[i]==maxi/2)root[1].push_back(i);
		}else{
			if(dist[i]==(maxi+1)/2 && dist2[i]==maxi/2)root[1].push_back(i);
			if(dist[i]==maxi/2 && dist2[i]==(maxi+1)/2)root[1].push_back(i);
		}
	}
	for(int i=0;i<root[1].size();i++){
		memset(calced,false,sizeof(calced));
		vp.push_back(calc(1,root[1][i]));
	}
	int res=0;
	if(n[0]==300000)return 0;
	for(int i=0;i<n[0];i++){
		if(!used[i]){
			root[0].clear();
			bfs(0,i);
			maxi=-1;
			tgt=0;
			for(int j=0;j<vs.size();j++){
				int v=vs[j];
				if(dist[v]>maxi){
					maxi=dist[v];
					tgt=v;
				}
				used[v]=true;
			}
			bfs(0,tgt);
			maxi=-1;
			for(int j=0;j<vs.size();j++){
				int v=vs[j];
				if(dist[v]>maxi){
					maxi=dist[v];
					tgt=v;
				}
				dist2[v]=dist[v];
			}
			bfs(0,tgt);
			for(int j=0;j<vs.size();j++){
				int v=vs[j];
				if(maxi%2==0){
					if(dist[v]==maxi/2 && dist2[v]==maxi/2)root[0].push_back(v);
				}else{
					if(dist[v]==(maxi+1)/2 && dist2[v]==maxi/2)root[0].push_back(v);
					if(dist[v]==maxi/2 && dist2[v]==(maxi+1)/2)root[0].push_back(v);
				}
			}
			bool flag=false;
			for(int j=0;j<root[0].size();j++){
				int v1=root[0][j];
				memset(calced,false,sizeof(calced));
				int s1=calc(0,v1);
				for(int k=0;k<vp.size();k++){
					if(s1==vp[k])flag=true;
				}
			}
			if(flag)res++;
		}
	}
	printf("%d\n",res);
}

int main(void){
	scanf("%d%d",&n[0],&m[0]);
	for(int i=0;i<m[0];i++){
		int f,t;
		scanf("%d%d",&f,&t);
		f--;
		t--;
		G[0][f].push_back(t);
		G[0][t].push_back(f);
	}

	scanf("%d",&n[1]);
	m[1]=n[1]-1;
	for(int i=0;i<m[1];i++){
		int f,t;
		scanf("%d%d",&f,&t);
		f--;
		t--;
		G[1][f].push_back(t);
		G[1][t].push_back(f);
	}
	solve();
	return 0;
}

Compiler output:
a.cc: In function 'void solve()':
a.cc:123:32: error: return-statement with a value, in function returning 'void' [-fpermissive]
  123 |         if(n[0]==300000)return 0;
      |                                ^