#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. [leetcode]332. Reconstruct Itinerary

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  2. jsonp现实跨域Ajax CORS

    浏览器有一个很重要的概念——同源策略(Same-Origin Policy).所谓同源是指,域名,协议,端口相同.不同源的客户端脚本(javascript.ActionScript)在没明确授权的情况 ...

  3. CSS设计中的错误大整理!

    如果有人发明时间机器,那应该将这些错误纠正,不然可把前端程序猿们给还惨了.大家一起看看都有哪些CSS规则应该完善. (CSS 代码) white-space: nowrap 应该 white-spac ...

  4. 第五篇、Python之迭代器与生成器

    1.迭代和递归等概念 循环(loop):指的是在满足条件的情况下,重复执行同一段代码.比如,while语句,for循环. 迭代(iterate):指的是按照某种顺序逐个访问列表中的每一项.比如,for ...

  5. python报以下错误:TypeError: 'int' object is not subscriptable

    原因:数组忘了写下标,数组某项赋值成了 数组对象=数字: 查询其它人的博客,发现仍有其它可能引发此错误,无非是不可以相互操作的对象进行了操作或者是访问对象的方式不对,如:给数字加了下标,对一维数组加了 ...

  6. JAVA_maven 配置

    前人种树: https://www.cnblogs.com/supiaopiao/p/7276805.html?utm_source=itdadao&utm_medium=referral

  7. MySql TIMEDIFF做计算之后,后台报Illegal hour value '24' for java.sql.Time type 问题

    页面需要显示这种格式:31:01:20 但是后台Springboot会提示Illegal hour value '24' for java.sql.Time type in value '24:00: ...

  8. Python连接Access数据库遇到问题'ADODB.Connection', '未找到提供程序。该程序可能未正确安装。'的处理办法

    环境Windows7+python3.6.4 x64位+AccessDatabaseEngine_X64.exe,执行代码: import win32com.client conn = win32co ...

  9. nginx warn an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/ while reading upstream

    最近管理的nginx发现大量的error log,log内容如下: an upstream response is buffered to a temporary file /var/cache/ng ...

  10. java课程之团队开发冲刺1.4

    一.总结昨天进度 1.昨天任务全部完成 二.遇到的问题 1.对数据库的使用陌生 2.使用sqlite有些困难 3.对如何解决查询课程问题暂时没有找到好的解决方案 三.今日任务 1.由于周一的课程比较紧 ...