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;
typedef vector< vector<int> > Graph;
int n[2],m[2];
vector<int> G[400001];
vector<Graph> tree;
Graph g2;
int dist[400001];
int dist2[400001];
int num[400001];
vector<int> root[2];
map<string,int> mp;
vector<int> vp;
int cnt=1;

char buf[3145140];
char *end;

void Forest_to_Trees(){
	memset(num,-1,sizeof(num));
	for(int i=0;i<n[0];i++){
		if(num[i]==-1){
			Graph gra(1);
			queue<int> q;
			q.push(i);
			int cur=0;
			num[i]=0;
			while(q.size()){
				int v=q.front();
				q.pop();
				for(int j=0;j<G[v].size();j++){
					int nv=G[v][j];
					if(num[nv]==-1){
						num[nv]=++cur;
						gra.push_back(vector<int>());
						gra[num[v]].push_back(num[nv]);
						gra[num[nv]].push_back(num[v]);
						q.push(nv);
					}
				}
			}
			tree.push_back(gra);
		}
	}
}


int calc2(Graph &G2,int v,int parent){
	if(G2[v].size()==0)return 1;
	if(G2[v].size()==1 && parent!=-1)return 1;
	vector<int> s;
	for(int i=0;i<(int)G2[v].size();i++){
		int nv=G2[v][i];
		if(parent!=nv){
			int val=calc2(G2,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{
		int siz=++cnt;
		mp.insert(map<string,int>::value_type(str,siz));
		return siz;
	}
	return -1;
}

void bfs2(Graph &G2,int s){
	queue<int> que;
	memset(dist,-1,sizeof(dist));
	que.push(s);
	dist[s]=0;
	while(que.size()){
		int v=que.front();
		que.pop();
		for(int i=0;i<G2[v].size();i++){
			if(dist[G2[v][i]]==-1){
				dist[G2[v][i]]=dist[v]+1;
				que.push(G2[v][i]);
			}
		}
	}
}

void solve(){
	bfs2(g2,0);
	int maxi=-1;
	int tgt=0;
	for(int i=0;i<n[1];i++){
		if(dist[i]>maxi){
			maxi=dist[i];
			tgt=i;
		}
	}
	bfs2(g2,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];
	}
	bfs2(g2,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(calc2(g2,root[1][i],-1));
	}
	int res=0;
	for(int i=0;i<tree.size();i++){
		if(tree[i].size()!=n[1])continue;
		root[0].clear();
		bfs2(tree[i],0);
		maxi=-1;
		tgt=0;
		for(int j=0;j<n[1];j++){
			if(dist[j]>maxi){
				maxi=dist[j];
				tgt=j;
			}
		}
		bfs2(tree[i],tgt);
		maxi=-1;
		for(int j=0;j<n[1];j++){
			if(dist[j]>maxi){
				maxi=dist[j];
				tgt=j;
			}
			dist2[j]=dist[j];
		}
		if(maxi!=maxi2)continue;
		bfs2(tree[i],tgt);
		for(int j=0;j<n[1];j++){
			if(maxi%2==0){
				if(dist[j]==maxi/2 && dist2[j]==maxi/2)root[0].push_back(j);
			}else{
				if(dist[j]==(maxi+1)/2 && dist2[j]==maxi/2)root[0].push_back(j);
				if(dist[j]==maxi/2 && dist2[j]==(maxi+1)/2)root[0].push_back(j);
			}
		}
		bool flag=false;
		for(int j=0;j<root[0].size();j++){
			if(flag)break;
			int s1=calc2(tree[i],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[f].push_back(t);
		G[t].push_back(f);
	}
	Forest_to_Trees();
	scanf("%d",&n[1]);
	m[1]=n[1]-1;
	for(int i=0;i<n[1];i++){
		g2.push_back(vector<int>());
	}
	for(int i=0;i<m[1];i++){
		int f,t;
		scanf("%d%d",&f,&t);
		f--;
		t--;
		g2[f].push_back(t);
		g2[t].push_back(f);
	}
	solve();
	return 0;
}

Compiler output:
a.cc: In function 'int calc2(Graph&, int, int)':
a.cc:77:9: error: reference to 'end' is ambiguous
   77 |         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:33:7: note:                 'char* end'
   33 | char *end;
      |       ^~~
a.cc:79:17: error: reference to 'end' is ambiguous
   79 |                 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:33:7: note:                 'char* end'
   33 | char *end;
      |       ^~~
a.cc:79:30: error: reference to 'end' is ambiguous
   79 |                 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:33:7: note:                 'char* end'
   33 | char *end;
      |       ^~~
a.cc:80:17: error: reference to 'end' is ambiguous
   80 |                 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:33:7: note:                 'char* end'
   33 | char *end;
      |       ^~~
a.cc:80:30: error: reference to 'end' is ambiguous
   80 |                 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:33:7: note:                 'char* end'
   33 | char *end;
      |       ^~~
a.cc:82:10: error: reference to 'end' is ambiguous
   82 |         *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:33:7: note:                 'char* end'
   33 | char *end;
      |       ^~~