只要把数据稍微弄弄就是比较裸的算法题,就不解释了
#include <iostream>
#include <map>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
const int Max = 20;
int Edge[Max][Max];
void Floyd(int n)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
for(int k=1; k<=n; k++)
{
if(i==k || k==j) continue;
if(Edge[i][j] > Edge[i][k] + Edge[k][j])
Edge[i][j] = Edge[i][k] + Edge[k][j];
}
}
}
}
int main()
{
// freopen("1016.txt","r",stdin);
map<string , int> q;
int n;
while(~scanf("%d",&n))
{
int index = 1;
memset(Edge, 0x6, sizeof(Edge));
for(int i=1; i<=n; i++)
{
string tem;
int num;
cin>>tem>>num;
int u0;
if(!q[tem])
{
u0 = index;
q[tem] = index++;
}
else
u0 = q[tem];
while(num--)
{
int v;
string tem2;
cin>>tem2;
if(!q[tem2])
{
v = index;
q[tem2] = index++;
Edge[u0][v] = 1;
}
else
{
v = q[tem2];
Edge[u0][v] = 1;
}
}
}
Floyd(n);
int m;
cin>>m;
while(m--)
{
string a,b;
cin>>a>>b;
int u = q[a];
int v = q[b];
if(Edge[u][v]>1e5)
cout<<a<<" has no connection to "<<b<<"."<<endl;
else
cout<<a<<" is separated from "<<b<<" by "<<Edge[u][v]-1<<" degrees.\n";
}
}
return 0;
}