HDOJ 4460 Friend Chains 图的最长路
类似于树的直径,从随意一个点出发,找到距离该点最远的且度数最少的点.
然后再做一次最短路
Friend Chains
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4227 Accepted Submission(s): 1359
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 .
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.
If the value of k is infinite, then print -1 instead.
3
XXX
YYY
ZZZ
2
XXX YYY
YYY ZZZ
0
2
/* ***********************************************
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 图的最长路的更多相关文章
- UVA11324 The Largest Clique (强连通缩点+DP最长路)
<题目链接> 题目大意: 给你一张有向图 G,求一个结点数最大的结点集,使得该结点集中的任意两个结点 u 和 v 满足:要么 u 可以达 v,要么 v 可以达 u(u,v相互可达也行). ...
- 「BZOJ1924」「SDOI2010」 所驼门王的宝藏 tarjan + dp(DAG 最长路)
「BZOJ1924」[SDOI2010] 所驼门王的宝藏 tarjan + dp(DAG 最长路) -------------------------------------------------- ...
- hdu 1534(差分约束+spfa求最长路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...
- POJ3592 Instantaneous Transference 强连通+最长路
题目链接: id=3592">poj3592 题意: 给出一幅n X m的二维地图,每一个格子可能是矿区,障碍,或者传送点 用不同的字符表示: 有一辆矿车从地图的左上角(0,0)出发, ...
- Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)
参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...
- HDU4514(非连通图的环判断与图中最长链)
题目:设计风景线 题意:给定一个无向图,图可能是非连通的,如果图中存在环,就输出YES,否则就输出图中最长链的长度. 分析:首先我们得考虑这是一个无向图,而且有可能是非连通的,那么就不能直接像求树那样 ...
- [USACO15JAN]草鉴定Grass Cownoisseur (分层图,最长路,$Tarjan$)
题目链接 Solution 水水的套路题. 可以考虑到一个环内的点是可以都到达的,所以 \(tajan\) 求出一个 \(DAG\) . 然后 \(DAG\) 上的点权值就是 \(scc\) 的大小. ...
- HDU 3249 Test for job (有向无环图上的最长路,DP)
解题思路: 求有向无环图上的最长路.简单的动态规划 #include <iostream> #include <cstring> #include <cstdlib ...
- POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6177 Accep ...
随机推荐
- matlab subplot(figure)如何设置使得图像最终显示出来不一样大小
1. 问题描述 figure subplot(1, 2, 1), imshow(A) subplot(1, 2, 2), imshow(B) 无论 A 和 B 的 size 是否一致,最终显示出来的 ...
- R语言写简单线性回归
library(MASS) library(ISLR) lm.fit <- lm(medv~lstat,data=Boston) attach(Boston) lm.fit = lm(medv~ ...
- Leaflet学习笔记(一)
一.简介 Leaflet是一个主要适用于移动端交互地图的领先的开源javascript库.虽然js库只有38KB左右,但是却能满足大部分开发者的所有功能需求. Leaflet拥有着简单,高效和实用的设 ...
- POJ 1631 nlogn求LIS
方法一: 二分 我们可以知道 最长上升子序列的 最后一个数的值是随序列的长度而递增的 (呃呃呃 意会意会) 然后我们就可以二分找值了(并更新) //By SiriusRen #include < ...
- 怎么去除innerHTML获得内容中的标签?
去掉innerHTML获得内容里面的标签: <body> <div id="d1"><p id="p1">hello wor ...
- IHttpHandler的学习(0)
本片文章转自网络 问题1:什么是HttpHandler?(Handler:处理者:那就是对Http请求的处理拉) 问题2:什么是HttpModule? 问题3:什么时候应该使用HttpHandler什 ...
- vue.js技巧小计
//删除数组索引方法01 del (index) { this.arr.splice(index ,1); } //删除数组索引方法01 del (index) { this.$delete(this ...
- LINUX 上源代码安装与配置samba服务,支持从windows上读写LINUX文件。
###动机###在windows编写代码文件比较方便,因为有source insight.但是需要在LINUX上编译.一种办法就是使用samba文件共享. [1] 下载samba代码.按照config ...
- Python多版本情况下四种快速进入交互式命令行的操作技巧
因为工作需求或者学习需要等原因,部分小伙伴的电脑中同时安装了Python2和Python3,相信在Python多版本的切换中常常会遇到Python傻傻分不清楚的情况,今天小编整理了四个操作技巧,以帮助 ...
- JVM监控工具介绍jstack, jconsole, jinfo, jmap, jdb, jstat(复制)
jstack -- 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程 ...