类似于树的直径,从随意一个点出发,找到距离该点最远的且度数最少的点.

然后再做一次最短路

Friend Chains

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4227    Accepted Submission(s): 1359

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
 
Source
 

/* ***********************************************
Author :CKboss
Created Time :2015年08月17日 星期一 16时35分00秒
File Name :HDOJ4460.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; const int INF=0x3f3f3f3f;
const int maxn=2100; int n,m;
int id=1;
map<string,int> msi; int getID(string name)
{
if(msi[name]==0) msi[name]=id++;
return msi[name];
} struct Edge
{
int to,next,cost;
}edge[maxn*maxn]; int Adj[maxn],Size;
int du[maxn]; void init()
{
id=1; msi.clear();
memset(du,0,sizeof(du));
memset(Adj,-1,sizeof(Adj)); Size=0;
} void Add_Edge(int u,int v)
{
edge[Size].to=v;
edge[Size].cost=1;
edge[Size].next=Adj[u];
Adj[u]=Size++;
} int dist[maxn],cq[maxn];
bool inq[maxn]; bool spfa(int st)
{
memset(dist,63,sizeof(dist));
memset(cq,0,sizeof(cq));
memset(inq,false,sizeof(inq));
dist[st]=0;
queue<int> q;
inq[st]=true;q.push(st); cq[st]=1; while(!q.empty())
{
int u=q.front();q.pop(); for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(dist[v]>dist[u]+edge[i].cost)
{
dist[v]=dist[u]+edge[i].cost;
if(!inq[v])
{
inq[v]=true;
cq[v]++;
if(cq[v]>=n+10) return false;
q.push(v);
}
}
}
inq[u]=false;
}
return true;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); while(scanf("%d",&n)!=EOF&&n)
{
init();
string name1,name2;
for(int i=1;i<=n;i++)
{
cin>>name1;
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
cin>>name1>>name2;
int id1=getID(name1);
int id2=getID(name2);
Add_Edge(id1,id2); Add_Edge(id2,id1);
du[id1]++; du[id2]++;
}
spfa(1);
int st=1;
for(int i=2;i<=n;i++)
{
if(dist[st]<dist[i]) st=i;
else if(dist[st]==dist[i])
{
if(du[st]>du[i]) st=i;
}
}
spfa(st);
int ans=0;
for(int i=1;i<=n;i++) ans=max(ans,dist[i]);
if(ans==INF) ans=-1;
cout<<ans<<endl;
} return 0;
}

最新文章

  1. WinForm开发笔记
  2. 【BZOJ 4326】【NOIP2015】运输计划
  3. Linux Native Aio 异步AIO的研究
  4. xcode设置项目图标玻璃镜效果
  5. sql从某不连续的数字中将其分段并找出缺失的数字并分段
  6. hello MemSQL 入门安装演示样例
  7. BZOJ 2588 Count on a tree (COT) 是持久的段树
  8. testlink用例转换小工具(excel转为xml,python版)
  9. Apache httpd.conf配置详解
  10. OJ题
  11. AfxBeginThread和CreateThread具体区别
  12. springmvc定时任务
  13. redi集群测试
  14. 详解C#特性和反射(一)
  15. What are the differences between Flyweight and Object Pool patterns?
  16. WIN7系统 如何上传文件到FTP服务器中
  17. RedisTemplate 分页
  18. Elasticsearch Java API—多条件查询(must)
  19. tty linux 打开和设置范例
  20. testdisk修复文件系统

热门文章

  1. JAVA学习第四十五课 — 其它对象API(一)System、Runtime、Math类
  2. FZOJ--2214--Knapsack problem(背包)
  3. ListView 适配器实现getviewtypcount() 数组越界IndexOutOfBoundException
  4. c# IndexOf()用法
  5. highGUI图形用户界面
  6. MFC的学习路线
  7. [洛谷P3929]SAC E#1 - 一道神题 Sequence1
  8. ATP自造8Gb内存颗粒供DDR3使用
  9. 三种记录 MySQL 所执行过的 SQL 语句的方法
  10. 为OLED屏添加GUI支持3:字库