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

然后再做一次最短路

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. bzoj1026: [SCOI2009]windy数(传说你是数位DP)

    1026: [SCOI2009]windy数 题目:传送门 题解: 其实之前年少无知的时候好像A过...表示当时并不知道什么数位DP 今天回来深造一发... 其实如果对这个算法稍有了解...看到这题的 ...

  2. 使用Java操作Redis(一)

    Redis是一款基于key-value的数据库服务器,安装完成后我们可以通过redis-cli使用Redis提供的命令完成各种操作.redis-cli实际上就是一款客户端,和redis-server建 ...

  3. PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(4)

    2.4 调整检查点和XLOG 目前为止,这一章已经提供深入洞察PostgreSQL如何写入数据,一般来说,XLOG是用来干什么的.考虑到这方面的知识,我们现在可以继续并学习我们能做些什么来使我们的数据 ...

  4. word/excel/ppt 2 PDF

    PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...

  5. 人在IT,关于计算机专业的杂谈PPT

  6. token登录验证机制

    一张图解释 token登录验证机制

  7. 修改MySQL默认字符集

    今天发现有库级字符集和表级字符集,实验了下发现,库级字符集是该库内表的默认字符集,当创建表时,如果未指定字符集,默认使用该表所属库的字符集.表也可使用不同于所属库的字符集. MySQL对于字符集的指定 ...

  8. HDU 3911 线段树区间合并

    北京赛区快了,准备袭击数据结构和图论.倒计时 18天,线段树区间合并.维护一个最长连续.. 题意:给一个01串,以下有一些操作,问区间最长的连续的1的个数 思路:非常裸的线段树区间合并 #includ ...

  9. Delphi的参数修饰const/var/output 与C++的对应关系

    delphi的const/input和默认的没有修饰, C++都是一样的 delphi的var,对应C++那边是指针,  调用方需要管理内存(负责分配内存及销毁) delphi的output , 对应 ...

  10. 流水线技术原理和Verilog HDL实现

    所谓流水线处理,如同生产装配线一样,将操作执行工作量分成若干个时间上均衡的操作段,从流水线的起点连续地输入,流水线的各操作段以重叠方式执行.这使得操作执行速度只与流水线输入的速度有关,而与处理所需的时 ...