题面

先圆方树然后建虚树,答案就是虚树大小。虚树没必要建出来,把原来的点的点权设为1,直接dfs序排序后相邻点求距离加上首尾两个点的距离,最后除以二(画一下可以发现是正反算了两遍),注意还要去掉询问点和补上首尾两个点的LCA

 #include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define vint vector<int>
#define vit vector<int> ::iterator
using namespace std;
const int N=,M=;
int T,n,m,c,q,o,t1,t2,rt,pt,cnt,Cnt,tot;
int dfn[N],low[N],stk[N],isc[N],col[N];
int p[N],noww[M],goal[M],P[N],Noww[M],Goal[M];
int siz[N],dep[N],fth[N],imp[N],top[N],qry[M],dis[N]; vint pbc[N];
void Link(int f,int t)
{
noww[++cnt]=p[f];
goal[cnt]=t,p[f]=cnt;
noww[++cnt]=p[t];
goal[cnt]=f,p[t]=cnt;
}
void Linka(int f,int t)
{
Noww[++Cnt]=P[f];
Goal[Cnt]=t,P[f]=Cnt;
Noww[++Cnt]=P[t];
Goal[Cnt]=f,P[t]=Cnt;
}
bool cmp(int a,int b)
{
return dfn[a]<dfn[b];
}
void Init()
{
memset(p,,sizeof p);
memset(P,,sizeof P);
memset(dfn,,sizeof dfn);
memset(low,,sizeof low);
memset(imp,,sizeof imp);
memset(top,,sizeof top);
for(int i=;i<=c;i++) pbc[i].clear();
cnt=Cnt=tot=c=;
}
void RTPBC(int nde)
{
int tmp=;
dfn[nde]=low[nde]=++tot,stk[++pt]=nde;
for(int i=p[nde],g;i;i=noww[i])
if(!dfn[g=goal[i]])
{
RTPBC(g),low[nde]=min(low[nde],low[g]);
if(dfn[nde]<=low[g])
{
if(nde!=rt||++tmp>) isc[nde]=true;
int tep; c++;
do
{
tep=stk[pt--],col[tep]=c;
pbc[c].push_back(tep);
}while(tep!=g);
pbc[c].push_back(nde);
}
}
else low[nde]=min(low[nde],dfn[g]);
}
void DFS(int nde,int far,int dth)
{
int tmp=;
siz[nde]=,fth[nde]=far,dep[nde]=dth;
for(int i=P[nde],g;i;i=Noww[i])
if((g=Goal[i])!=far)
{
dis[g]=dis[nde]+(g<=n);
DFS(g,nde,dth+),siz[nde]+=siz[g];
if(siz[g]>tmp) tmp=siz[g],imp[nde]=g;
}
}
void Mark(int nde,int upt)
{
top[nde]=upt,dfn[nde]=++tot;
if(imp[nde])
{
Mark(imp[nde],upt);
for(int i=P[nde],g;i;i=Noww[i])
if((g=Goal[i])!=fth[nde]&&g!=imp[nde]) Mark(g,g);
}
}
int LCA(int x,int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])
swap(x,y); x=fth[top[x]];
}
return dep[x]<dep[y]?x:y;
}
int Dist(int x,int y)
{
int lca=LCA(x,y);
return dis[x]+dis[y]-*dis[lca];
}
int main()
{
scanf("%d",&T);
while(T--)
{
Init();
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d",&t1,&t2),Link(t1,t2);
RTPBC(rt=);
for(int i=;i<=c;i++)
for(vit it=pbc[i].begin();it!=pbc[i].end();it++) Linka(*it,n+i);
tot=,DFS(,,),Mark(,);
scanf("%d",&q);
while(q--)
{
scanf("%d",&o); int ans=;
for(int i=;i<=o;i++) scanf("%d",&qry[i]);
sort(qry+,qry++o,cmp),qry[++o]=qry[];
for(int i=;i<=o;i++) ans+=Dist(qry[i],qry[i-]);
printf("%d\n",(ans>>)-o++(LCA(qry[o],qry[o-])<=n));
}
}
return ;
}
04-24 20:46