HDU 4460 Friend Chains (BFS,最长路径)
题意:给定 n 个人,和关系,问你这个朋友圈里任意两者之间最短的距离是多少。
析:很明显的一个BFS,只要去找最长距离就好。如果不能全找到,就是-1.
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map> using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
map<string, int> id;
vector<int> G[maxn];
int getid(const string &s){
return id[s];
}
int vis[maxn];
int vvis[maxn];
int d[maxn]; int bfs(int rt){
queue<int> q;
q.push(rt);
memset(vis, 0, sizeof(vis));
memset(d, -1, sizeof(d));
vis[rt] = vvis[rt] = 1;
int ans = rt;
d[rt] = 0;
int mmax = 0;
while(!q.empty()){
int u = q.front(); q.pop();
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
if(vis[v]) continue;
vis[v] = vvis[v] = 1;
d[v] = d[u] + 1;
if(mmax < d[u] + 1){
mmax = d[u] + 1;
ans = v;
}
q.push(v);
}
}
return ans;
} int solve(int i){
int u = bfs(i);
int v = bfs(u);
return d[v];
} int main(){
while(scanf("%d", &n) == 1 && n){
id.clear();
string s, s1, s2;
for(int i = 1; i <= n; ++i){
cin >> s;
id[s] = i;
G[i].clear();
}
scanf("%d", &m);
for(int i = 0; i < m; ++i){
cin >> s1 >> s2;
int u = getid(s1);
int v = getid(s2);
G[u].push_back(v);
G[v].push_back(u);
}
if(m + 1 < n){ printf("-1\n"); continue; }
memset(vvis, 0, sizeof(vvis));
int ans = 0;
for(int i = 1; i <= n; ++i)
if(!vvis[i]) ans = max(ans, solve(i));
for(int i = 1; i <= n; ++i)
if(d[i] == -1){ ans = -1; break; }
printf("%d\n", ans);
}
return 0;
}
HDU 4460 Friend Chains (BFS,最长路径)的更多相关文章
- HDU 4460 Friend Chains --BFS
题意:问给定的一张图中,相距最远的两个点的距离为多少.解法:跟求树的直径差不多,从1 开始bfs,得到一个最远的点,然后再从该点bfs一遍,得到的最长距离即为答案. 代码: #include < ...
- 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
Problem Description For a group of people, there is an idea that everyone is equals to or less than ...
- HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)
HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径) Description A group of researchers ar ...
- Going from u to v or from v to u? POJ - 2762(强连通 有向最长路径)
In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, an ...
- hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- ubuntu 终端设置(颜色与长路径)
Linux给人最大的享受就是可以根据个人喜好去定制令自己舒服的系统配置,像终端颜色的设置就是一个典型的例子. 图1 系统默认状态下的终端显示 在没有经过自定义配置的终端下工作久了,难免容易疲劳 ...
- Codefroces Gym 100781A(树上最长路径)
http://codeforces.com/gym/100781/attachments 题意:有N个点,M条边,问对两两之间的树添加一条边之后,让整棵大树最远的点对之间的距离最近,问这个最近距离是多 ...
- 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)
Walking Race Description flymouse's sister wc is very capable at sports and her favorite event is ...
随机推荐
- ADODB.Connection 错误 '800a0e7a' 未找到提供程序。该程序可能未正确安装。解决方法!
[问题描述]调试基于ACCESS+ASP的网站程序的时候,遇到如下错误: ADODB.Connection 错误 '800a0e7a' 未找到提供程序.该程序可能未正确安装. /Inc/Functio ...
- li添加浮动时不能完全包裹的方法
<html> <head> <meta http-equiv="Content-Type" content="text/html;chars ...
- linux中find命令的使用
google找到一个很好的讲解:http://www.chinaz.com/server/2009/0807/85796.shtml
- poj 1742 Coins
// v给出N种硬币和个数,问可以取到1->M中的多少个值.// 背包 完全背包 或多 重背包(二进制优化)都可以做// #include <iostream> #include & ...
- SwipeRefreshLayout的简要说明及使用demo
在最新的 Android Support Library, revision 19.1.0 (March 2014) 添加了SwipeRefreshLayout控件. 版本发布的说明信息如下: ...
- shell 删除日志
一般线上服务的日志都是采用回滚的防止,写一定数量的日志 或是有管理工具定期去转移老旧日志 前几天删除一个测试环境的日志,只保留两天的日志,结果把正在写的日志都给删掉了,不得不重启了服务,经过这一次的错 ...
- Github 终于开始认真考虑开源项目许可证了
如今GitHub已成为全球最流行的开源项目托管平台,但也有质疑声音——“Github中的大多数项目并不算是开源项目”.这是因为Github中大多数项目并没有明确声明所使用的许可证. 根据版权法规定,如 ...
- ECshop 二次开发模板教程1
本教程适用于了解 ECshop 和 ECshop模板DIY 以及它们的日常使用,在查看前阁下需要至少会使用一种编辑器(exp:Dreamweaver, editplus, emacs, vi, ee ...
- Ch04-文字列表的设计
Ch04: 文字列表的设计 4.1 编号列表 语法: <OL> <li>编号1 <li>编号2 ...... </OL> 属性: 编号类型:type=1 ...
- 构建属于自己的ORM框架之二--IQueryable的奥秘
上篇文章标题乱起,被吐槽了,这次学乖了. 上篇文章中介绍了如何解析Expression生成对应的SQL语句,以及IQueryable的一些概念,以及我们所搭建的框架的思想等.但还没把它们结合并应用起来 ...