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];
vector<int> root[2];
vector<int> vs;
map<string,int> mp;
vector<int> vp;
int cnt=0;

char buf[3145140];
char *end;

int calc(int t,int v,int parent){
	calced[v]=true;

	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);
			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+1));
		return siz+1;
	}
	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);
		}
	}
	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<vs.size();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<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];
				//if(n[0]==300000)return;
				int s1=calc(0,v1,-1);
				for(int k=0;k<vp.size();k++){
					if(s1==vp[k])flag=true;
				}
				//if(n[0]==300000)return;
			}
			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:35:9: error: 'calced' was not declared in this scope; did you mean 'calc'?
   35 |         calced[v]=true;
      |         ^~~~~~
      |         calc
a.cc:41:37: error: too few arguments to function 'int calc(int, int, int)'
   41 |                         int val=calc(t,nv);
      |                                 ~~~~^~~~~~
a.cc:34:5: note: declared here
   34 | int calc(int t,int v,int parent){
      |     ^~~~
a.cc:47:9: error: reference to 'end' is ambiguous
   47 |         end=buf;
      |         ^~~
In file included from /usr/include/c++/14/vector:69,
                 from a.cc:3:
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) 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())
      |     ^~~
In file included from /usr/include/c++/14/bits/stl_vector.h:63,
                 from /usr/include/c++/14/vector:66:
/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:32:7: note:                 'char* end'
   32 | char *end;
      |       ^~~
a.cc:49:17: error: reference to 'end' is ambiguous
   49 |                 end+=sprintf(end,"%d",s[i]);
      |                 ^~~
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) 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:32:7: note:                 'char* end'
   32 | char *end;
      |       ^~~
a.cc:49:30: error: reference to 'end' is ambiguous
   49 |                 end+=sprintf(end,"%d",s[i]);
      |                              ^~~
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) 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:32:7: note:                 'char* end'
   32 | char *end;
      |       ^~~
a.cc:50:17: error: reference to 'end' is ambiguous
   50 |                 end+=sprintf(end,";");
      |                 ^~~
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) 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:32:7: note:                 'char* end'
   32 | char *end;
      |       ^~~
a.cc:50:30: error: reference to 'end' is ambiguous
   50 |                 end+=sprintf(end,";");
      |                              ^~~
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) 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:32:7: note:                 'char* end'
   32 | char *end;
      |       ^~~
a.cc:52:10: error: reference to 'end' is ambiguous
   52 |         *end=0;
      |          ^~~
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) 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:32:7: note:                 'char* end'
   32 | char *end;
      |       ^~~