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

然后再做一次最短路

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

HDOJ 4460 Friend Chains 图的最长路的更多相关文章

  1. UVA11324 The Largest Clique (强连通缩点+DP最长路)

    <题目链接> 题目大意: 给你一张有向图 G,求一个结点数最大的结点集,使得该结点集中的任意两个结点 u 和 v 满足:要么 u 可以达 v,要么 v 可以达 u(u,v相互可达也行). ...

  2. 「BZOJ1924」「SDOI2010」 所驼门王的宝藏 tarjan + dp(DAG 最长路)

    「BZOJ1924」[SDOI2010] 所驼门王的宝藏 tarjan + dp(DAG 最长路) -------------------------------------------------- ...

  3. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

  4. POJ3592 Instantaneous Transference 强连通+最长路

    题目链接: id=3592">poj3592 题意: 给出一幅n X m的二维地图,每一个格子可能是矿区,障碍,或者传送点 用不同的字符表示: 有一辆矿车从地图的左上角(0,0)出发, ...

  5. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)

    参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...

  6. HDU4514(非连通图的环判断与图中最长链)

    题目:设计风景线 题意:给定一个无向图,图可能是非连通的,如果图中存在环,就输出YES,否则就输出图中最长链的长度. 分析:首先我们得考虑这是一个无向图,而且有可能是非连通的,那么就不能直接像求树那样 ...

  7. [USACO15JAN]草鉴定Grass Cownoisseur (分层图,最长路,$Tarjan$)

    题目链接 Solution 水水的套路题. 可以考虑到一个环内的点是可以都到达的,所以 \(tajan\) 求出一个 \(DAG\) . 然后 \(DAG\) 上的点权值就是 \(scc\) 的大小. ...

  8. HDU 3249 Test for job (有向无环图上的最长路,DP)

     解题思路: 求有向无环图上的最长路.简单的动态规划 #include <iostream> #include <cstring> #include <cstdlib ...

  9. POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;&amp; SPFA求最长路 &amp;&amp; 经典】

    Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accep ...

随机推荐

  1. Struts2标签库整理【完整】

    转自:https://blog.csdn.net/chen_zw/article/details/8161230   Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,str ...

  2. 使用Chrome浏览器,让我们远离(所有)广告

    你是否还在为浏览网页时各种广告霸屏而急躁不安?这里分享一个小技巧,如何自动屏蔽各大广告. 这里使用的浏览器是Chrome,直接在Chrome网上应用商店搜索下载安装AdBlock插件(不知道其它浏览器 ...

  3. SharePoint 学习快速导航

    根据我的学习过程,会不断的增加一些学习的快速链接 . 入门篇 SharePoint入门链接,针对刚刚开始了解SharePoint 的朋友,我也是处在入门的状态,随后会慢慢的累积增加 安装 | 部署 | ...

  4. VS自定义开发向导中的vsdir文件的简单说明

    作者:朱金灿 来源:http://blog.csdn.net/clever101 VS自定义开发向导中有一个vsdir文件.这个文件指定了在VS中项目的标题.默认工程名等内容.下面对vsdir文件做一 ...

  5. ES6中常用的简写方式

    1. var foo = 'bar'; var baz = {foo}; baz // {foo: "bar"} // 等同于 var baz = {foo: foo}; 2. f ...

  6. Kubernetes1.5 集成Heapster

    Kubernetes1.5 集成Heapster Heapster是kubernetes集群监控工具.在1.2的时候,kubernetes的监控需要在node节点上运行cAdvisor作为agent收 ...

  7. bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易

    题目描述 一张P个点的无向图,C条正权路.CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌.(途中不必回家)可以先去NOI,也可以先去CMO.当然神犇CLJ肯 ...

  8. caioj 1082 动态规划入门(非常规DP6:火车票)

    f[i]表示从起点到第i个车站的最小费用 f[i] = min(f[j] + dist(i, j)), j < i 动规中设置起点为0,其他为正无穷 (貌似不用开long long也可以) #i ...

  9. 隐马尔科夫模型(HMM)

    基本概念 1Markov Models 2Hidden Markov Models 3概率计算算法前向后向算法 1-3-1直接计算 1-3-2前向算法 1-3-3后向算法 4学习问题Baum-Welc ...

  10. Ural 1303 Minimal Coverage(贪心)

    题目地址:Ural 1303 先按每一个线段的左端点排序,然后设置一个起点s.每次都从起点小于等于s的线段中找到一个右端点最大的. 并将该右端点作为新的起点s,然后继续找. 从左到右扫描一遍就可以. ...