显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,a,b,k,v,u,x,y,z;
int dis[41][41];
int main()
{
freopen("geo.in","r",stdin);freopen("geo.out","w",stdout);
scanf("%d%d",&n,&m);
for(x=1;x<=n;x++)
{
for(y=1;y<=n;y++)
{
dis[x][y]=100;
dis[y][x]=100;
}
dis[x][x]=0;
}
for(x=1;x<=m;x++)
{
scanf("%d%d",&a,&b);
dis[a][b]=1;dis[b][a]=1;
}
for(z=1;z<=n;z++)
for(x=1;x<=n;x++)
for(y=1;y<=n;y++)
if(dis[x][z]+dis[z][y]<dis[x][y])
dis[x][y]=dis[x][z]+dis[z][y];
scanf("%d",&k);
for(x=1;x<=k;x++)
{
scanf("%d%d",&v,&u);
for(y=1;y<=n;y++)
if(dis[v][y]+dis[y][u]==dis[v][u])
printf("%d ",y);
printf("\n");
}
return 0;
fclose(stdin);fclose(stdout);
}