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 ;
}

HDU 4460 Friend Chains(map + spfa)的更多相关文章

  1. HDU 4460 Friend Chains --BFS

    题意:问给定的一张图中,相距最远的两个点的距离为多少.解法:跟求树的直径差不多,从1 开始bfs,得到一个最远的点,然后再从该点bfs一遍,得到的最长距离即为答案. 代码: #include < ...

  2. HDU 4460 Friend Chains (BFS,最长路径)

    题意:给定 n 个人,和关系,问你这个朋友圈里任意两者之间最短的距离是多少. 析:很明显的一个BFS,只要去找最长距离就好.如果不能全找到,就是-1. 代码如下: #pragma comment(li ...

  3. HDU 4460 Friend Chains

    Problem Description For a group of people, there is an idea that everyone is equals to or less than ...

  4. ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)

    Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...

  5. HDU 4460

    http://acm.hdu.edu.cn/showproblem.php?pid=4460 水题一道,oj时间卡的非常奇怪,把spfa的queue开成全局卡线过,别的全挂了,迪杰斯特拉的手写堆都超时 ...

  6. HDOJ 4460 Friend Chains 图的最长路

    类似于树的直径,从随意一个点出发,找到距离该点最远的且度数最少的点. 然后再做一次最短路 Friend Chains Time Limit: 2000/1000 MS (Java/Others)    ...

  7. HDU 1874 畅通工程续 SPFA || dijkstra||floyd

    http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目大意: 给你一些点,让你求S到T的最短路径. 我只是来练习一下SPFA的 dijkstra+邻接矩阵 ...

  8. hdu 2112 (最短路+map)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)  ...

  9. POJ 3481 &amp; HDU 1908 Double Queue (map运用)

    题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...

随机推荐

  1. Excel教程(5) - 日期与时间函数

    DATE 用途:返回代表特定日期的序列号. 语法:DATE(year,month,day) 参数:year 为一到四位,根据使用的日期系统解释该参 数.默认情况下,Excel for Windows ...

  2. MFC HTTP

    CInternetSession m_winet(NULL,,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,); CHttpConnection *pConnection; ...

  3. 显示进度条tqdm

    http://www.open-open.com/lib/view/open1451794925808.html

  4. word异常关闭,找到丢失的word

    Word模板路径位置 XP系统用户默认模板路径一般在 C:\Documents and Settings\Administrator\Application Data\Microsoft\Templa ...

  5. 工作中遇到的http返回码

    普通常见的200.404.500,工作中遇到的还有206 .302 .304.400.403. 206----服务器返回部分数据 302----请求跳转 304----not modify 服务器内容 ...

  6. Oracle 获取当天数据

    where trunc(to_date(DATETIME,'yyyy-MM-dd hh24:mi:ss'))=trunc(sysdate)

  7. Student s = new Student();在内存中做了哪些事情?

    [Student s = new Student();在内存中做了哪些事情?] 1加载Student.class文件进内存 2在栈内存为s开辟空间 3在堆内存为学生对象开辟空间 4对学生对象的成员变量 ...

  8. javaweb学习总结——Filter高级开发

    在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...

  9. wmic应用实例

    实例应用 1.磁盘管理 查看磁盘的属性 wmic logicaldisk list brief ::caption=标题.driveID=驱动器ID号.model=产品型号.Partitions=分区 ...

  10. 如何将excel导入到数据库中并在gridview中显示

    在页面上导入个excel文件,将该excel中的数据导入到数据库中,并且在页面的gridview中把数据显示出来. .在Asp.net中怎样将Excel文件中的数据导入到GridView中呢? 首先我 ...