Language: C++

#include<bits/stdc++.h>
using namespace std;
#define next Nxt
#define last Lst
#define gc getchar
#define int long long
const int N=555+5;
int n,m,ans,t,head[N],size[N],fa[N],dep[N],son[N],gs[N*10];
int seg[N],rev[N],top[N],tree[N*4],lazy[N*4],sum[N*4],mi[N*4];
struct node{
	int too,next;
}edge[N*2];
struct Node{
	int x,y;
}a[N];
//char buf[1<<21],*p1=buf,*p2=buf;
//inline int gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
inline int read()
{
    int ret=0,f=0;char c=gc();
    while(!isdigit(c)){if(c=='-')f=1;c=gc();}
    while(isdigit(c)){ret=ret*10+c-48;c=gc();}
    if(f)return -ret;return ret;
}
void add(int a,int b)
{
	edge[++t].too=b;edge[t].next=head[a];head[a]=t;
}
void dfs(int u,int f)
{
	size[u]=1;fa[u]=f;dep[u]=dep[f]+1;
	for(int i=head[u];i;i=edge[i].next)
	{
		int v=edge[i].too;
		if(v==f)continue;
		dfs(v,u);
		size[u]+=size[v];
		if(size[v]>size[son[u]])son[u]=v;
	}
}
void dfs2(int u,int f)
{
	if(son[u])
	{
		seg[son[u]]=++seg[0];
		rev[seg[0]]=son[u];
		top[son[u]]=top[u];
		dfs2(son[u],u);
	}
	for(int i=head[u];i;i=edge[i].next)
	{
		int v=edge[i].too;
		if(v==f||v==son[u])continue;
		seg[v]=++seg[0];
		rev[seg[0]]=v;
		top[v]=v;
		dfs2(v,u);
	}
}
void pushup(int nod)
{
	mi[nod]=min(mi[nod*2],mi[nod*2+1]);
	tree[nod]=tree[nod*2]+tree[nod*2+1];
	if(mi[nod*2]==mi[nod])sum[nod]=sum[nod*2];
	else sum[nod]=0;
	if(mi[nod*2+1]==mi[nod])sum[nod]+=sum[nod*2+1];
	else sum[nod]+=0;
}
void pushdown(int nod,int l,int r)
{
	mi[nod*2]+=lazy[nod];
	mi[nod*2+1]+=lazy[nod];
	if(mi[nod*2]>0)tree[nod*2]=0;
	else tree[nod*2]=sum[nod*2];
	if(mi[nod*2+1]>0)tree[nod*2+1]=0;
	else tree[nod*2+1]=sum[nod*2+1];
	lazy[nod*2]+=lazy[nod];
	lazy[nod*2+1]+=lazy[nod];
	lazy[nod]=0;
}
void change(int nod,int l,int r,int L,int R,int val)
{
	if(l==L&&r==R)
	{
		mi[nod]=mi[nod]+val;
		if(mi[nod]>0)tree[nod]=0;
		else tree[nod]=sum[nod];
		lazy[nod]+=val;
		return;
	}
	pushdown(nod,l,r);
	int mid=(l+r)/2;
	if(R<=mid)change(nod*2,l,mid,L,R,val);
	else if(L>mid)change(nod*2+1,mid+1,r,L,R,val);
	else{
		change(nod*2,l,mid,L,mid,val);
		change(nod*2+1,mid+1,r,mid+1,R,val);
	}
	pushup(nod);
}
int query(int nod,int l,int r,int L,int R)
{
	if(l==L&&r==R)return tree[nod];
	pushdown(nod,l,r);
	int mid=(l+r)/2;
	if(R<=mid)return query(nod*2,l,mid,L,R);
	else if(L>mid)return query(nod*2+1,mid+1,r,L,R);
	else return query(nod*2,l,mid,L,mid)+query(nod*2+1,mid+1,r,mid+1,R);
}
void build(int nod,int l,int r)
{
	if(l==r)
	{
		sum[nod]=1;
		return;
	}
	int mid=(l+r)/2;
	build(nod*2,l,mid);
	build(nod*2+1,mid+1,r);
	pushup(nod); 
}
void update(int x,int y,int z)
{
	int fx=top[x],fy=top[y];
	while(fx!=fy)
	{
		if(dep[fx]<dep[fy])
		{
			swap(x,y);
			swap(fx,fy);
		}
		change(1,1,n,seg[fx],seg[x],z);
		x=fa[fx];
		fx=top[x];
	}
	if(dep[x]<dep[y])swap(x,y);
	if(seg[y]<seg[x])change(1,1,n,seg[y]+1,seg[x],z);
}
int kuai(int a,int b)
{
	if(b<0)return 0;
	int res=1;
	while(b)
	{
		if(b&1)res=res*a;
		a=a*a;
		b=b/2;
	}
	return res;
}
int lowbit(int x)
{
	return x&-x;
}
signed main()
{
	n=read();
	for(int i=1;i<n;i++)
	{
		int x=read(),y=read();
		add(x,y);
		add(y,x);
	}
	dfs(1,0);
	seg[0]=seg[1]=rev[1]=top[1]=1;
	dfs2(1,0);
	build(1,1,n);
	ans=kuai(2,n-1);
	m=read();
	for(int i=1;i<=m;i++)
	{
		a[i].x=read(),a[i].y=read();
	}
	for(int i=1;i<(1<<m);i++)
	{
		gs[i]=gs[i-lowbit(i)]+1;
		for(int j=1;j<=m;j++)
			if(i&(1ll<<(j-1)))
			{
//				cout<<j<<endl;
				update(a[j].x,a[j].y,1);
			}
		if(gs[i]&1)ans-=kuai(2,tree[1]-1);
		else ans+=kuai(2,tree[1]-1);
		for(int j=1;j<=m;j++)
			if(i&(1ll<<(j-1)))
			{
				update(a[j].x,a[j].y,-1);
			}
	}
	cout<<ans;
	return 0;
}

Compiler output:
a.cc: In function 'void dfs(long long int, long long int)':
a.cc:31:9: error: reference to 'size' is ambiguous
   31 |         size[u]=1;fa[u]=f;dep[u]=dep[f]+1;
      |         ^~~~
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,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:8:23: note:                 'long long int size [560]'
    8 | int n,m,ans,t,head[N],size[N],fa[N],dep[N],son[N],gs[N*10];
      |                       ^~~~
a.cc:37:17: error: reference to 'size' is ambiguous
   37 |                 size[u]+=size[v];
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:8:23: note:                 'long long int size [560]'
    8 | int n,m,ans,t,head[N],size[N],fa[N],dep[N],son[N],gs[N*10];
      |                       ^~~~
a.cc:37:26: error: reference to 'size' is ambiguous
   37 |                 size[u]+=size[v];
      |                          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:8:23: note:                 'long long int size [560]'
    8 | int n,m,ans,t,head[N],size[N],fa[N],dep[N],son[N],gs[N*10];
      |                       ^~~~
a.cc:38:20: error: reference to 'size' is ambiguous
   38 |                 if(size[v]>size[son[u]])son[u]=v;
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:8:23: note:                 'long long int size [560]'
    8 | int n,m,ans,t,head[N],size[N],fa[N],dep[N],son[N],gs[N*10];
      |                       ^~~~
a.cc:38:28: error: reference to 'size' is ambiguous
   38 |                 if(size[v]>size[son[u]])son[u]=v;
      |                            ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:8:23: note:                 'long long int size [560]'
    8 | int n,m,ans,t,head[N],size[N],fa[N],dep[N],son[N],gs[N*10];
      |                       ^~~~