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

然后再做一次最短路

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. 用DOM动态控制表格

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  2. POJ 3671 DP or 乱搞

    思路: 1.DP f[i][j]:前i个数 最后一个数是j的最小花费 f[i][j]=min(f[i][j],f[i-1][k]+(a[i]!=j));1<=k<=j 这种做法比较有普遍性 ...

  3. PostgreSQL Replication之第一章 理解复制概念(3)

    1.3 使用分片和数据分配 本节您将了解基本可扩展性技术,例如数据库分片.分片被广泛应用于高端系统并提供一个简单而且可靠的扩展设置方式来向外扩展.近年来,分片已经成为一种扩大专业系统规模的标准方式. ...

  4. iOS——扬声器与听筒的切换

    1.扬声器模式:  NSError *error; [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPor ...

  5. Linux 常用命令:系统状态篇

    前言 Linux常用命令中,有些命令可以用于查看系统的状态,通过了解系统当前的状态,能够帮助我们更好地维护系统或定位问题.本文就简单介绍一下这些命令. 1. 查看系统运行时间--uptime 有时候我 ...

  6. 雅礼集训1-9day爆零记

    雅礼集训1-9day爆零记 先膜一下虐爆我的JEFF巨佬 Day0 我也不知道我要去干嘛,就不想搞文化科 (文化太辣鸡了.jpg) 听李总说可以去看(羡慕)各路大佬谈笑风声,我就报一个名吧,没想到还真 ...

  7. HDU 4398 Template Library Management (最优页面调度算法)

    中等偏易题.操作系统理论中的最优页面调度算法,贪心.当需要淘汰某个模版时,淘汰掉当前手中在最远的将来才会被用到(或者以后永远不再用到)的那个. 代码: #include <iostream> ...

  8. CMSIS-RTOS 时间管理之时间延迟Time Delay

    时间管理 Time Management 此RTOS除了可以把你的应用代码作为线程运行,它还可以提供一些时间服务功能,使用这些功能你就可以访问RTOS的一些系统调用. 时间延迟Time Delay 在 ...

  9. POJ 2826 An Easy Problem!(简单数论)

    Description Have you heard the fact "The base of every normal number system is 10" ? Of co ...

  10. ubuntu12.04更新软件源时出现校验和不符

    在运行update命令之后.出现系统校验和不符.网上找了一些方法,最后在大神的帮助下最终攻克了! ! 1.更改 /etc/apt/apt.conf.d/00aptitude 文件,在最后一行增加: A ...