#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define MAX 9999999
int dis[+][+];
int vis[+];
string name[+];
int n;
map<string,int> mymap;
vector<int> vec[+];
void spfa(int i)
{
memset(vis,,sizeof(vis));
dis[i][i]=;
vis[i]=;
queue<int> q;
q.push(i);
while(!q.empty())
{
int temp=q.front();
q.pop();
for(int j=;j<vec[temp].size();j++)
{
int to=vec[temp][j];
if(vis[to])
continue;
dis[i][to]=dis[i][temp]+;
q.push(to);
vis[to]=;
}
}
}
int main()
{
int T;
while(cin>>n,n)
{
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
{
dis[i][j]=dis[j][i]=MAX;
}
}
for(int i=;i<n;i++)
vec[i].clear();
int cou=;
string str;
for(int i=;i<n;i++)
{
cin>>str;
mymap[str]=cou++;
}
int m;
cin>>m;
string str1,str2;
for(int i=;i<m;i++)
{
cin>>str1>>str2;
int t1=mymap[str1];
int t2=mymap[str2];
vec[t1].push_back(t2);
vec[t2].push_back(t1);
}
for(int i=;i<n;i++)
{
spfa(i);
}
int ans=;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
ans=max(ans,dis[i][j]);
}
}
if(ans==MAX)
cout<<-<<endl;
else
cout<<ans<<endl;
}
return ; }

HDU4460的更多相关文章

  1. HDU-4460 Friend Chains(BFS&权为1所有最短路的最大值)

    题目: For a group of people, there is an idea that everyone is equals to or less than 6 steps away fro ...

  2. hdu4460 Friend Chains(记忆化广度优先搜索)

    题意: 任意两点间最短路中的最长距离. 思路: BFS遍历每个点能到达的最远距离. Tips: vector的clear要与resize联用. #include <bits/stdc++.h&g ...

随机推荐

  1. 【CZYZ 20160819】背包

    题目描述 蛤布斯有nn个物品和一个大小为mm的背包,每个物品有大小和价值,它希望你帮它求出背包里最多能放下多少价值的物品. 输入数据 第一行两个整数 n,mn,m. 接下来 nn 行每行两个整数 xi ...

  2. 自定义编写js格式化数字的函数

    在处理网页的时候,有时候会需要显示很长的数字,但是当数字的长度比较长的时候,就很难看一个数字到底是多大.这种情况下,一些网站在处理数字的时候,当数字的长度大于3个时,就用逗号把他们分开,这是一个比较常 ...

  3. java使用Filter过滤器对Response返回值进行修改

    转:java使用Filter过滤器对Response返回值进行修改 练习时只做了对request 的处理,这里记录一下,filter 对 response的处理. 原文地址:java使用Filter过 ...

  4. c# System.Console

    System.Console类公开了和操作控制台相关的有用的静态字段和静态方法.下面是System.Console中一些较为重要的方法. public static void Beep()该方法播放蜂 ...

  5. Create Oracle database using dbca in silent mode

    12.2.0.1 dbca.rsp文件中必须内容如下: responseFileVersion=/oracle/assistants/rspfmt_dbca_response_schema_v12.2 ...

  6. AlphaGo的前世今生(二)AlphaGo: Countdown to AI Revolution

    这是本专题的第二节,在这一节我们将以David Silver等人的Natrue论文Mastering the game of Go with deep neural networks and tree ...

  7. Spring 初学笔记

    Spring 初学笔记: https://blog.csdn.net/weixin_35909255/article/category/7470388

  8. PHP 浮点型转整型的一个奇怪现象

    起因 最近通过一个学长的题了解php弱类型的时候,发现了一个奇怪的现象. 正文 主要问题在这样一段代码: $c=(int)((0.1+$b)*10); 当$b=0.6,0.8以及其他值的时候都正常 将 ...

  9. Exp1 PC平台逆向破解 20165110 石钰

    Exp1 PC平台逆向破解 20165110 石钰 一.实践目标 1.实验背景 实践对象是pwn1的Linux可执行文件,该程序的正常该程序正常执行流程是:main调用foo函数(oo函数会简单回显任 ...

  10. Winform导入文件

    winfrom的选中文件的路径放进文本框,我还是一个新手,欢迎大家在评论里面多多指教