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 ...
随机推荐
- PHP 去掉emoji字符
function isMatchEmoji($str) { $pattern='/./u'; $rs=preg_match_all($pattern,$str,$match); if($rs>0 ...
- 小议C#接口的隐式与显示实现
小弟不才,各位大牛嘴下留情... 一.对于继承类里头有相同方法时候,用接口方式去调用,会优先查走显式接口方法 例如 public interface IA { void Test(); } publi ...
- WinForm中的多线程
使用BeginInvoke或Invoke 作用 在自己创建的非UI线程中,进行UI操作,比如更新UI上控件的状态. Windows 窗体中的控件被绑定到特定的线程,不具备线程安全性.因此,如果从另一个 ...
- pch文件配置出现 Expected unqualified-id 和 Unkown type name 'NSString'
1.发生的现象 之前代码还是没有报错的,由于某些代码比较常用,就打算配置一个pch文件引入常用的文件 但是引入的时候就出现了报错 2.原因与解决办法 2.1 原因 你引入的文件可能使用到OC与C++混 ...
- 20165219 2017-2018-2《Java程序设计》课程总结
20165219 2017-2018-2<Java程序设计>课程总结 一.每周作业链接汇总 20165219 我期望的师生关系 20165219学习基础与C语言基础调查 20165219 ...
- 在一个java类里,private int a; 什么时候要使用integer
private Integer index; if(index == null) index = 0; else this.index = index; Integer有一个明显的好处,就是它能比in ...
- 【转】如何不让DataGridView自动生成列
源地址:https://www.cnblogs.com/hailexuexi/p/3983856.html
- oracle为IN OUT变量或OUT变量赋值时提示“表达式''不能用作赋值目标”
是因为IN OUT变量和OUT变量是要输出的,不能赋给它常量值,这样它就不能再被赋值而输出了,所以是禁止赋常量值的,比如''也是常量值,也不可以赋给这两种类型的变量,如果不能把存储过程中的其他变量赋给 ...
- 如何理解java采用Unicode编码
http://blog.csdn.net/gjb724332682/article/details/43229563 Java中字符仅以一种形式存在,那就是Unicode.由于java采用unicod ...
- docker镜像的创建
获得更多资料欢迎进入我的网站或者 csdn或者博客园 昨天讲解了docker的安装与基本使用,今天给大家讲解下docker镜像的创建的方法,以及push到Docker Hub docker安装请点击右 ...