Friend Chains

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

For a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of introduction. So that a chain of "a friend of a friend" can be made to connect any 2 persons and it contains no more than 7 persons.
For example, if XXX is YYY’s friend and YYY is ZZZ’s friend, but XXX is not ZZZ's friend, then there is a friend chain of length 2 between XXX and ZZZ. The length of a friend chain is one less than the number of persons in the chain.
Note that if XXX is YYY’s friend, then YYY is XXX’s friend. Give the group of people and the friend relationship between them. You want to know the minimum value k, which for any two persons in the group, there is a friend chain connecting them and the chain's length is no more than k .

Input

There are multiple cases. 
For each case, there is an integer N (2<= N <= 1000) which represents the number of people in the group. 
Each of the next N lines contains a string which represents the name of one people. The string consists of alphabet letters and the length of it is no more than 10. 
Then there is a number M (0<= M <= 10000) which represents the number of friend relationships in the group. 
Each of the next M lines contains two names which are separated by a space ,and they are friends. 
Input ends with N = 0.

Output

For each case, print the minimum value k in one line. 
If the value of k is infinite, then print -1 instead.

Sample Input

3
XXX
YYY
ZZZ
2
XXX YYY
YYY ZZZ
0

Sample Output

2
/*
题意:给出一个无向图,若联通则输出任意一对点之间最短路径中的最大值,
若有孤立一点,则输出-1。
*/
#include <iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
using namespace std;
const int inf=;
int n,m,maxn;
map<string,int> mp;
vector<int> s[];
int dis[];
bool vis[];
void spfa(int st)
{
for(int i=;i<=n;i++) dis[i]=inf,vis[i]=;
queue<int> Q;
Q.push(st);
vis[st]=;
dis[st]=;
while(!Q.empty())
{
int u=Q.front();
vis[u]=;
Q.pop();
for(int i=;i<s[u].size();i++)
{
if (dis[s[u][i]]<=dis[u]+) continue;
dis[s[u][i]]=dis[u]+;
maxn=dis[s[u][i]]>maxn?dis[s[u][i]]:maxn;
if (!vis[s[u][i]])
{
vis[s[u][i]]=;
Q.push(s[u][i]);
}
}
}
return;
}
int main()
{
while(scanf("%d",&n) && n)
{
for(int i=;i<=n;i++)
{
char ch[];
scanf("%s",&ch);
mp[ch]=i;
s[i].clear();
} scanf("%d",&m);
for(int i=;i<=m;i++)
{
char ch1[],ch2[];
scanf("%s %s",&ch1,&ch2);
s[mp[ch1]].push_back(mp[ch2]);
s[mp[ch2]].push_back(mp[ch1]);
}
int ans=;
for(int i=;i<=n;i++)
{
maxn=;
spfa(i);
if (maxn==) ans=-;//本来想直接退出,输出-1,但是考虑到现在以i为起点的最短路径计算出来的并不一定是最长的长度,可能是最短路径中的一个中间点
if (ans>= && maxn>ans) ans=maxn;
} printf("%d\n",ans);
}
return ;
}

最新文章

  1. Java IO之字符流和文件
  2. [网络技术][转]网卡的offload概念
  3. css选择器([class*=&quot; icon-&quot;], [class^=icon-] 的区别)
  4. 关于JavaScript中的创建对象的学习总结
  5. css取消input、select默认样式(手机端)
  6. 简单好用的Toast封装类——EasyToast
  7. Oracle11g空表导出方法
  8. seafile
  9. Jenkins-测试自动化环境搭建(Python+RobotFramework+selenium)
  10. tcpdump抓SQL
  11. 关于MATLAB中的tic toc的问题
  12. 16个不错的git别名
  13. gdb篇
  14. JQuery EasyUI的常用组件
  15. POI读取excel工具类 返回实体bean集合(xls,xlsx通用)
  16. TensorFlow LSTM 注意力机制图解
  17. SQL学习笔记---非select操作
  18. kubernetes之flannel
  19. MapReduce的工作原理
  20. Java演算法-「雞兔同籠問題」

热门文章

  1. 使用freerdp远程连接Windows桌面
  2. LeetCode OJ 55. Jump Game
  3. 推荐两个好用的Xcode插件(提供下载链接)
  4. android service文章转载
  5. erlang dets
  6. F - 小晴天老师系列——苹果大丰收
  7. Struts2之checkboxlist 设置默认值和结果回显
  8. python crypto
  9. 关于Linode、Digitalocean、Vultr三款美国VPS服务商的用户体验
  10. HDU 5775 Bubble Sort