Language: C++

#include <bits/stdc++.h>
#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];
vector<int> root[2];
vector<int> vs;
map<string,int> mp;
vector<int> vp;
int cnt=1;

char buf[3145140];
char *end;

int calc(int t,int v,int parent){
	if(G[t][v].size()==0)return 1;
	if(G[t][v].size()==1 && parent!=-1)return 1;
	vector<int> s;
	for(int i=0;i<(int)G[t][v].size();i++){
		int nv=G[t][v][i];
		if(parent!=nv){
			int val=calc(t,nv,v);
			if(val==-1)return -1;
			s.push_back(val);
		}
	}
	if(s.size()>0)sort(s.begin(),s.end());
	end=buf;
	for(int i=0;i<s.size();i++){
		end+=sprintf(end,"%d",s[i]);
		end+=sprintf(end,";");
	}
	*end=0;
	string str=buf;
	if(str.size()==0){
		str+='0';
	}
	if((int)mp.count(str)==1){
		return mp[str];
	}else{
		if(t==0)return -1;
		int siz=++cnt;
		mp.insert(map<string,int>::value_type(str,siz));
		return siz;
	}
	return -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;
		}
	}
	int maxi2=maxi;
	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);
		}
	}
	if(root[1].size()>2)return;
	mp["0"]=1;
	for(int i=0;i<root[1].size();i++){
		vp.push_back(calc(1,root[1][i],-1));
	}
	int res=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;
			}
			if(vs.size()!=n[1])continue;
			bfs(0,tgt);
			maxi=-1;
			for(int j=0;j<n[1];j++){
				int v=vs[j];
				if(dist[v]>maxi){
					maxi=dist[v];
					tgt=v;
				}
				dist2[v]=dist[v];
			}
			if(maxi!=maxi2)continue;
			bfs(0,tgt);
			for(int j=0;j<n[1];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];
				int s1=calc(0,root[0][j],-1);
				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 'int calc(int, int, int)':
a.cc:34:9: error: reference to 'end' is ambiguous
   34 |         end=buf;
      |         ^~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166,
                 from a.cc:1:
/usr/include/c++/14/valarray:1265:5: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
 1249 |     end(valarray<_Tp>& __va) noexcept
      |     ^~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
In file included from /usr/include/c++/14/bits/algorithmfwd.h:39,
                 from /usr/include/c++/14/bits/stl_algo.h:59,
                 from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:19:7: note:                 'char* end'
   19 | char *end;
      |       ^~~
a.cc:36:17: error: reference to 'end' is ambiguous
   36 |                 end+=sprintf(end,"%d",s[i]);
      |                 ^~~
/usr/include/c++/14/valarray:1265:5: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
 1249 |     end(valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:19:7: note:                 'char* end'
   19 | char *end;
      |       ^~~
a.cc:36:30: error: reference to 'end' is ambiguous
   36 |                 end+=sprintf(end,"%d",s[i]);
      |                              ^~~
/usr/include/c++/14/valarray:1265:5: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
 1249 |     end(valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:19:7: note:                 'char* end'
   19 | char *end;
      |       ^~~
a.cc:37:17: error: reference to 'end' is ambiguous
   37 |                 end+=sprintf(end,";");
      |                 ^~~
/usr/include/c++/14/valarray:1265:5: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
 1249 |     end(valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:19:7: note:                 'char* end'
   19 | char *end;
      |       ^~~
a.cc:37:30: error: reference to 'end' is ambiguous
   37 |                 end+=sprintf(end,";");
      |                              ^~~
/usr/include/c++/14/valarray:1265:5: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
 1249 |     end(valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:19:7: note:                 'char* end'
   19 | char *end;
      |       ^~~
a.cc:39:10: error: reference to 'end' is ambiguous
   39 |         *end=0;
      |          ^~~
/usr/include/c++/14/valarray:1265:5: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
 1249 |     end(valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:19:7: note:                 'char* end'
   19 | char *end;
      |       ^~~