HDU 4460 Friend Chains
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.
YYY
ZZZ
求最短路径的最长路,直接上floyd果断超时。改用bfs,卡过。
#include <stdio.h>
#include <map>
#include <string>
#include <queue>
#include <iostream>
#define inf 0x3f3f3f3f
#define MAXN 1005
using namespace std; int cnt;
map< string,int > M;
int head[MAXN];
bool visited[MAXN];
int dist[MAXN][MAXN];
struct EdgeNode{
int to;
int next;
}edges[MAXN*]; void addedge(int u, int v){
edges[cnt].to=v;
edges[cnt].next=head[u];
head[u]=cnt++;
} void bfs(int u){
queue<int> Q;
Q.push(u);
dist[u][u]=;
memset(visited,,sizeof(visited));
visited[u]=;
while( !Q.empty() ){
int now=Q.front();
Q.pop();
for(int i=head[now]; i!=-; i=edges[i].next){
int to=edges[i].to;
if(!visited[to]){
dist[u][to]=dist[u][now]+;
Q.push(to);
visited[to]=;
}
}
}
} int main(int argc, char *argv[])
{
int n,k;
string a,b;
while(scanf("%d",&n)!=EOF && n){
M.clear();
memset(head,-,sizeof(head));
for(int i=; i<=n; i++){
for(int j=i+; j<=n; j++){
dist[i][j]=dist[j][i]=inf;
}
}
for(int i=; i<=n; i++){
string name;
cin>>name;
M[name]=i;
}
cnt=;
scanf("%d",&k);
while(k--){
cin>>a>>b;
addedge(M[a],M[b]);
addedge(M[b],M[a]);
}
for(int i=; i<=n; i++)bfs(i);
int ans=;
for(int i=; i<=n; i++){
for(int j=i+; j<=n; j++){
if(dist[i][j]>ans)
ans=dist[i][j];
}
}
if(ans==inf)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}
HDU 4460 Friend Chains的更多相关文章
- HDU 4460 Friend Chains(map + spfa)
Friend Chains Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU 4460 Friend Chains --BFS
题意:问给定的一张图中,相距最远的两个点的距离为多少.解法:跟求树的直径差不多,从1 开始bfs,得到一个最远的点,然后再从该点bfs一遍,得到的最长距离即为答案. 代码: #include < ...
- HDU 4460 Friend Chains (BFS,最长路径)
题意:给定 n 个人,和关系,问你这个朋友圈里任意两者之间最短的距离是多少. 析:很明显的一个BFS,只要去找最长距离就好.如果不能全找到,就是-1. 代码如下: #pragma comment(li ...
- HDU 4460
http://acm.hdu.edu.cn/showproblem.php?pid=4460 水题一道,oj时间卡的非常奇怪,把spfa的queue开成全局卡线过,别的全挂了,迪杰斯特拉的手写堆都超时 ...
- HDOJ 4460 Friend Chains 图的最长路
类似于树的直径,从随意一个点出发,找到距离该点最远的且度数最少的点. 然后再做一次最短路 Friend Chains Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 4460 第37届ACM/ICPC杭州赛区H题 STL+bfs
题意:一些小伙伴之间有朋友关系,比如a和b是朋友,b和c是朋友,a和c不是朋友,则a和c之间存在朋友链,且大小为2,给出一些关系,求出这些关系中最大的链是多少? 求最短路的最大距离 #include& ...
- HDU 3746:Cyclic Nacklace
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3487 Play with Chain
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 YaoYao is fond of playing his chains. He has a c ...
- hdu 3746 Cyclic Nacklace(kmp最小循环节)
Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...
随机推荐
- mobiscroll_2.15.1
var opt_sex = { theme: 'ios', lang: 'zh', formatValue: function (d) { return d.join(','); }, customW ...
- 【转】Android ActionBar完全解析,使用官方推荐的最佳导航栏(上)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/18234477 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...
- android和.net webservice中的DES加密算法
也是看了一堆的例子,本身并不会写加密算法,好在只要会用就行了,我们把在app中使用的参数加密,然后在.net端的webservice中进行解密,本身并没有什么问题,但是android下和.net下的d ...
- WPF中在摄像头视频上叠加控件的解决方案
一.视频呈现 前段时间,在一个wpf的项目中需要实时显示ip摄像头,对此的解决方案想必大家都应该知道很多.在winform中,我们可以将一个控件(一般用panel或者pictruebox)的句柄丢给摄 ...
- c++基类指针指向继承类调用继承类函数
类里面重载运算符>>, 需要使用友元函数,而友元函数,不能作为虚函数. 所以,基类指针无法直接调用继承类里重构的 >> ; 使用类转换,能解决掉,基类指针 调用 继承类 ...
- 苹果开发者账号提示“Unable to verify mobile phone number”的解决方案
在注册苹果开发者账号时,会提示:"Unable to verify mobile phone number.".顾名思义,没有有效的手机号码. 解决方案: 进入到Your Appl ...
- shell和matlab之间的参数传递
shell和matlab之间的参数传递比shell和Python之间的参数传递要简单,在matlab程序中(以.m脚本文件为例,其他程序如函数等未测试)不需要进行任何配置,直接使用即可,见下面 ...
- linux 常用端口【转载】
原文地址: http://blog.csdn.net/u013943420/article/details/65938696 一个计算机最多有65535个端口,端口不能重复.这65536个端口被分为两 ...
- spring 学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试
spring学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试 ioc 概念 控制反转(Inversion of Control,缩写为IOC),是面向 ...
- 关于执行webdriver.Chrome; 报错WebDriverException: Message: unknown error: Element is not clickable at point (1085, 103)
from selenium import webdriverfrom time import sleep dr = webdriver.Chrome() dr.get("http://pj1 ...